aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c118
-rw-r--r--ldblib.c17
-rw-r--r--liolib.c75
-rw-r--r--lstate.c21
-rw-r--r--lstrlib.c4
-rw-r--r--ltests.c30
-rw-r--r--ltm.c17
-rw-r--r--ltm.h4
-rw-r--r--lua.c17
-rw-r--r--lua.h24
-rw-r--r--lualib.h23
-rw-r--r--lvm.c126
-rw-r--r--lvm.h11
13 files changed, 262 insertions, 225 deletions
diff --git a/lapi.c b/lapi.c
index fb22b3df..4a80796a 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.92 2000/08/31 20:23:40 roberto Exp roberto $ 2** $Id: lapi.c,v 1.93 2000/08/31 21:01:43 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*/
@@ -69,25 +69,24 @@ void lua_settop (lua_State *L, int index) {
69} 69}
70 70
71 71
72void lua_move (lua_State *L, int index) { 72void lua_remove (lua_State *L, int index) {
73 TObject *p = Index(L, index); 73 StkId p = Index(L, index);
74 TObject temp = *p;
75 while (++p < L->top) *(p-1) = *p; 74 while (++p < L->top) *(p-1) = *p;
76 *(L->top-1) = temp; 75 L->top--;
77} 76}
78 77
79 78
80void lua_insert (lua_State *L, int index) { 79void lua_insert (lua_State *L, int index) {
81 TObject temp = *(L->top-1); 80 TObject temp = *(L->top-1);
82 TObject *p = Index(L, index); 81 StkId p = Index(L, index);
83 TObject *q; 82 StkId q;
84 for (q = L->top-1; q>p; q--) 83 for (q = L->top-1; q>p; q--)
85 *q = *(q-1); 84 *q = *(q-1);
86 *p = temp; 85 *p = temp;
87} 86}
88 87
89 88
90void lua_pushobject (lua_State *L, int index) { 89void lua_pushvalue (lua_State *L, int index) {
91 *L->top = *Index(L, index); 90 *L->top = *Index(L, index);
92 api_incr_top(L); 91 api_incr_top(L);
93} 92}
@@ -133,17 +132,24 @@ int lua_isnumber (lua_State *L, int index) {
133 132
134int lua_tag (lua_State *L, int index) { 133int lua_tag (lua_State *L, int index) {
135 btest(L, index, 134 btest(L, index,
136 ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag : luaT_effectivetag(L, o)), 135 ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
137 -1); 136 luaT_effectivetag(L, o)), -1);
138} 137}
139 138
140int lua_equal(lua_State *L, int index1, int index2) { 139int lua_equal (lua_State *L, int index1, int index2) {
141 StkId o1 = Index(L, index1); 140 StkId o1 = Index(L, index1);
142 StkId o2 = Index(L, index2); 141 StkId o2 = Index(L, index2);
143 if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */ 142 if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
144 else return luaO_equalObj(o1, o2); 143 else return luaO_equalObj(o1, o2);
145} 144}
146 145
146int lua_lessthan (lua_State *L, int index1, int index2) {
147 StkId o1 = Index(L, index1);
148 StkId o2 = Index(L, index2);
149 if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
150 else return luaV_lessthan(L, o1, o2, L->top);
151}
152
147 153
148 154
149double lua_tonumber (lua_State *L, int index) { 155double lua_tonumber (lua_State *L, int index) {
@@ -168,14 +174,13 @@ void *lua_touserdata (lua_State *L, int index) {
168} 174}
169 175
170const void *lua_topointer (lua_State *L, int index) { 176const void *lua_topointer (lua_State *L, int index) {
171 const TObject *o = Index(L, index); 177 StkId o = Index(L, index);
172 switch (ttype(o)) { 178 switch (ttype(o)) {
173 case TAG_NUMBER: case TAG_NIL: 179 case TAG_NUMBER: case TAG_NIL:
174 return NULL; 180 return NULL;
175 case TAG_STRING: 181 case TAG_STRING:
176 return tsvalue(o)->str;
177 case TAG_USERDATA: 182 case TAG_USERDATA:
178 return tsvalue(o)->u.d.value; 183 return tsvalue(o);
179 case TAG_TABLE: 184 case TAG_TABLE:
180 return hvalue(o); 185 return hvalue(o);
181 case TAG_CCLOSURE: case TAG_LCLOSURE: 186 case TAG_CCLOSURE: case TAG_LCLOSURE:
@@ -243,31 +248,38 @@ void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
243 248
244 249
245void lua_getglobal (lua_State *L, const char *name) { 250void lua_getglobal (lua_State *L, const char *name) {
246 luaV_getglobal(L, luaS_new(L, name), L->top++); 251 StkId top = L->top;
252 *top = *luaV_getglobal(L, luaS_new(L, name));
253 L->top = top+1;
247} 254}
248 255
249 256
250void lua_gettable (lua_State *L) { 257void lua_gettable (lua_State *L, int tableindex) {
251 luaV_gettable(L, L->top--); 258 StkId t = Index(L, tableindex);
259 StkId top = L->top;
260 *(top-1) = *luaV_gettable(L, t);
261 L->top = top; /* tag method may change top */
252} 262}
253 263
254 264
255void lua_rawget (lua_State *L) { 265void lua_rawget (lua_State *L, int tableindex) {
256 LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "table expected"); 266 StkId t = Index(L, tableindex);
257 *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1); 267 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
258 L->top--; 268 *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
259} 269}
260 270
261 271
262void lua_getglobals (lua_State *L) { 272void lua_rawgeti (lua_State *L, int index, int n) {
263 hvalue(L->top) = L->gt; 273 StkId o = Index(L, index);
264 ttype(L->top) = TAG_TABLE; 274 LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
275 *L->top = *luaH_getnum(hvalue(o), n);
265 api_incr_top(L); 276 api_incr_top(L);
266} 277}
267 278
268 279
269void lua_gettagmethod (lua_State *L, int tag, const char *event) { 280void lua_getglobals (lua_State *L) {
270 *L->top = *luaT_gettagmethod(L, tag, event); 281 hvalue(L->top) = L->gt;
282 ttype(L->top) = TAG_TABLE;
271 api_incr_top(L); 283 api_incr_top(L);
272} 284}
273 285
@@ -300,38 +312,40 @@ void lua_newtable (lua_State *L) {
300 312
301 313
302void lua_setglobal (lua_State *L, const char *name) { 314void lua_setglobal (lua_State *L, const char *name) {
303 luaV_setglobal(L, luaS_new(L, name), L->top--); 315 StkId top = L->top;
316 luaV_setglobal(L, luaS_new(L, name));
317 L->top = top-1; /* remove element from the top */
304} 318}
305 319
306 320
307void lua_settable (lua_State *L) { 321void lua_settable (lua_State *L, int tableindex) {
322 StkId t = Index(L, tableindex);
308 StkId top = L->top; 323 StkId top = L->top;
309 luaV_settable(L, top-3, top); 324 luaV_settable(L, t, top-2);
310 L->top = top-3; /* pop table, index, and value */ 325 L->top = top-2; /* pop index and value */
311} 326}
312 327
313 328
314void lua_rawset (lua_State *L) { 329void lua_rawset (lua_State *L, int tableindex) {
315 LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "table expected"); 330 StkId t = Index(L, tableindex);
316 *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1); 331 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
317 L->top -= 3; 332 *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
333 L->top -= 2;
318} 334}
319 335
320 336
321void lua_setglobals (lua_State *L) { 337void lua_rawseti (lua_State *L, int index, int n) {
322 TObject *newtable = --L->top; 338 StkId o = Index(L, index);
323 LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); 339 LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
324 L->gt = hvalue(newtable); 340 *luaH_setint(L, hvalue(o), n) = *(L->top-1);
341 L->top--;
325} 342}
326 343
327 344
328void lua_settagmethod (lua_State *L, int tag, const char *event) { 345void lua_setglobals (lua_State *L) {
329 TObject *method = L->top - 1; 346 StkId newtable = --L->top;
330 if (ttype(method) != TAG_NIL && 347 LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
331 ttype(method) != TAG_CCLOSURE && 348 L->gt = hvalue(newtable);
332 ttype(method) != TAG_LCLOSURE)
333 lua_error(L, "Lua API error - tag method must be a function or nil");
334 luaT_settagmethod(L, tag, event, method);
335} 349}
336 350
337 351
@@ -362,7 +376,6 @@ int lua_ref (lua_State *L, int lock) {
362** miscellaneous functions 376** miscellaneous functions
363*/ 377*/
364 378
365
366void lua_settag (lua_State *L, int tag) { 379void lua_settag (lua_State *L, int tag) {
367 luaT_realtag(L, tag); 380 luaT_realtag(L, tag);
368 switch (ttype(L->top-1)) { 381 switch (ttype(L->top-1)) {
@@ -389,8 +402,8 @@ void lua_unref (lua_State *L, int ref) {
389} 402}
390 403
391 404
392int lua_next (lua_State *L) { 405int lua_next (lua_State *L, int tableindex) {
393 const TObject *t = Index(L, -2); 406 StkId t = Index(L, tableindex);
394 Node *n; 407 Node *n;
395 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); 408 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
396 n = luaH_next(L, hvalue(t), Index(L, -1)); 409 n = luaH_next(L, hvalue(t), Index(L, -1));
@@ -401,7 +414,7 @@ int lua_next (lua_State *L) {
401 return 1; 414 return 1;
402 } 415 }
403 else { /* no more elements */ 416 else { /* no more elements */
404 L->top -= 2; /* remove key and table */ 417 L->top -= 1; /* remove key */
405 return 0; 418 return 0;
406 } 419 }
407} 420}
@@ -427,3 +440,10 @@ int lua_getn (lua_State *L, int index) {
427 } 440 }
428} 441}
429 442
443
444void lua_concat (lua_State *L, int n) {
445 StkId top = L->top;
446 luaV_strconc(L, n, top);
447 L->top = top-(n-1);
448}
449
diff --git a/ldblib.c b/ldblib.c
index 17edcda5..2d8c555f 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.18 2000/08/09 19:16:57 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.19 2000/08/28 17:57:04 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,18 +18,16 @@
18 18
19 19
20static void settabss (lua_State *L, const char *i, const char *v) { 20static void settabss (lua_State *L, const char *i, const char *v) {
21 lua_pushobject(L, -1);
22 lua_pushstring(L, i); 21 lua_pushstring(L, i);
23 lua_pushstring(L, v); 22 lua_pushstring(L, v);
24 lua_settable(L); 23 lua_settable(L, -3);
25} 24}
26 25
27 26
28static void settabsi (lua_State *L, const char *i, int v) { 27static void settabsi (lua_State *L, const char *i, int v) {
29 lua_pushobject(L, -1);
30 lua_pushstring(L, i); 28 lua_pushstring(L, i);
31 lua_pushnumber(L, v); 29 lua_pushnumber(L, v);
32 lua_settable(L); 30 lua_settable(L, -3);
33} 31}
34 32
35 33
@@ -44,7 +42,7 @@ static int getinfo (lua_State *L) {
44 } 42 }
45 } 43 }
46 else if (lua_isfunction(L, 1)) { 44 else if (lua_isfunction(L, 1)) {
47 lua_pushobject(L, 1); 45 lua_pushvalue(L, 1);
48 sprintf(buff, ">%.10s", options); 46 sprintf(buff, ">%.10s", options);
49 options = buff; 47 options = buff;
50 } 48 }
@@ -71,10 +69,9 @@ static int getinfo (lua_State *L) {
71 settabss(L, "namewhat", ar.namewhat); 69 settabss(L, "namewhat", ar.namewhat);
72 break; 70 break;
73 case 'f': 71 case 'f':
74 lua_pushobject(L, -1);
75 lua_pushstring(L, "func"); 72 lua_pushstring(L, "func");
76 lua_pushobject(L, -4); 73 lua_pushvalue(L, -3);
77 lua_settable(L); 74 lua_settable(L, -3);
78 break; 75 break;
79 } 76 }
80 } 77 }
@@ -90,7 +87,7 @@ static int getlocal (lua_State *L) {
90 name = lua_getlocal(L, &ar, luaL_check_int(L, 2)); 87 name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
91 if (name) { 88 if (name) {
92 lua_pushstring(L, name); 89 lua_pushstring(L, name);
93 lua_pushobject(L, -2); 90 lua_pushvalue(L, -2);
94 return 2; 91 return 2;
95 } 92 }
96 else { 93 else {
diff --git a/liolib.c b/liolib.c
index 46d619c1..885db9bd 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.75 2000/08/31 13:30:10 roberto Exp roberto $ 2** $Id: liolib.c,v 1.76 2000/08/31 20:23:40 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*/
@@ -107,7 +107,7 @@ static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) {
107 FILE *f; 107 FILE *f;
108 lua_getglobals(L); 108 lua_getglobals(L);
109 lua_getref(L, ctrl->ref[inout]); 109 lua_getref(L, ctrl->ref[inout]);
110 lua_rawget(L); 110 lua_rawget(L, -2);
111 f = gethandle(L, ctrl, -1); 111 f = gethandle(L, ctrl, -1);
112 if (f == NULL) 112 if (f == NULL)
113 luaL_verror(L, "global variable `%.10s' is not a file handle", 113 luaL_verror(L, "global variable `%.10s' is not a file handle",
@@ -564,58 +564,77 @@ static int io_debug (lua_State *L) {
564} 564}
565 565
566 566
567 567#define LEVELS1 12 /* size of the first part of the stack */
568#define MESSAGESIZE 150 568#define LEVELS2 10 /* size of the second part of the stack */
569#define MAXMESSAGE (MESSAGESIZE*10)
570
571 569
572static int errorfb (lua_State *L) { 570static int errorfb (lua_State *L) {
573 char buff[MAXMESSAGE];
574 int level = 1; /* skip level 0 (it's this function) */ 571 int level = 1; /* skip level 0 (it's this function) */
572 int firstpart = 1; /* still before eventual `...' */
575 lua_Debug ar; 573 lua_Debug ar;
576 sprintf(buff, "error: %.200s\n", lua_tostring(L, 1)); 574 lua_settop(L, 1);
575 luaL_checktype(L, 1, "string");
576 lua_pushstring(L, "error: ");
577 lua_insert(L, 1);
578 lua_pushstring(L, "\nstack traceback:\n");
579 lua_concat(L, 3);
577 while (lua_getstack(L, level++, &ar)) { 580 while (lua_getstack(L, level++, &ar)) {
581 char buff[120]; /* enough to fit following `sprintf's */
578 char buffchunk[60]; 582 char buffchunk[60];
583 int toconcat = 1; /* number of strings in the stack to concat */
584 if (level > LEVELS1 && firstpart) {
585 /* no more than `LEVELS2' more levels? */
586 if (!lua_getstack(L, level+LEVELS2, &ar))
587 level--; /* keep going */
588 else {
589 lua_pushstring(L, " ...\n"); /* too many levels */
590 lua_concat(L, 2);
591 while (lua_getstack(L, level+LEVELS2, &ar)) /* get last levels */
592 level++;
593 }
594 firstpart = 0;
595 continue;
596 }
597 sprintf(buff, "%4d: ", level-1);
598 lua_pushstring(L, buff); toconcat++;
579 lua_getinfo(L, "Snl", &ar); 599 lua_getinfo(L, "Snl", &ar);
580 luaL_chunkid(buffchunk, ar.source, sizeof(buffchunk)); 600 luaL_chunkid(buffchunk, ar.source, sizeof(buffchunk));
581 if (level == 2) strcat(buff, "stack traceback:\n");
582 strcat(buff, " ");
583 if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) {
584 strcat(buff, "...\n");
585 break; /* buffer is full */
586 }
587 switch (*ar.namewhat) { 601 switch (*ar.namewhat) {
588 case 'g': case 'l': /* global, local */ 602 case 'g': case 'l': /* global, local */
589 sprintf(buff+strlen(buff), "function `%.50s'", ar.name); 603 sprintf(buff, "function `%.50s'", ar.name);
590 break; 604 break;
591 case 'f': /* field */ 605 case 'f': /* field */
592 sprintf(buff+strlen(buff), "method `%.50s'", ar.name); 606 sprintf(buff, "method `%.50s'", ar.name);
593 break; 607 break;
594 case 't': /* tag method */ 608 case 't': /* tag method */
595 sprintf(buff+strlen(buff), "`%.50s' tag method", ar.name); 609 sprintf(buff, "`%.50s' tag method", ar.name);
596 break; 610 break;
597 default: { 611 default: {
598 if (*ar.what == 'm') /* main? */ 612 if (*ar.what == 'm') /* main? */
599 sprintf(buff+strlen(buff), "main of %.70s", buffchunk); 613 sprintf(buff, "main of %.70s", buffchunk);
600 else if (*ar.what == 'C') /* C function? */ 614 else if (*ar.what == 'C') /* C function? */
601 sprintf(buff+strlen(buff), "%.70s", buffchunk); 615 sprintf(buff, "%.70s", buffchunk);
602 else 616 else
603 sprintf(buff+strlen(buff), "function <%d:%.70s>", 617 sprintf(buff, "function <%d:%.70s>", ar.linedefined, buffchunk);
604 ar.linedefined, buffchunk);
605 ar.source = NULL; 618 ar.source = NULL;
606 } 619 }
607 } 620 }
608 if (ar.currentline > 0) 621 lua_pushstring(L, buff); toconcat++;
609 sprintf(buff+strlen(buff), " at line %d", ar.currentline); 622 if (ar.currentline > 0) {
610 if (ar.source) 623 sprintf(buff, " at line %d", ar.currentline);
611 sprintf(buff+strlen(buff), " [%.70s]", buffchunk); 624 lua_pushstring(L, buff); toconcat++;
612 strcat(buff, "\n"); 625 }
626 if (ar.source) {
627 sprintf(buff, " [%.70s]", buffchunk);
628 lua_pushstring(L, buff); toconcat++;
629 }
630 lua_pushstring(L, "\n"); toconcat++;
631 lua_concat(L, toconcat);
613 } 632 }
614 lua_getglobals(L); 633 lua_getglobals(L);
615 lua_pushstring(L, LUA_ALERT); 634 lua_pushstring(L, LUA_ALERT);
616 lua_rawget(L); 635 lua_rawget(L, -2);
617 if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ 636 if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
618 lua_pushstring(L, buff); 637 lua_pushvalue(L, -3); /* error message */
619 lua_call(L, 1, 0); 638 lua_call(L, 1, 0);
620 } 639 }
621 return 0; 640 return 0;
diff --git a/lstate.c b/lstate.c
index 7e264565..f5d5b67c 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.34 2000/08/28 17:57:04 roberto Exp roberto $ 2** $Id: lstate.c,v 1.35 2000/08/31 13:30:39 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -9,7 +9,6 @@
9 9
10#include "lua.h" 10#include "lua.h"
11 11
12#include "lbuiltin.h"
13#include "ldo.h" 12#include "ldo.h"
14#include "lgc.h" 13#include "lgc.h"
15#include "llex.h" 14#include "llex.h"
@@ -20,7 +19,13 @@
20#include "ltm.h" 19#include "ltm.h"
21 20
22 21
23lua_State *lua_newstate (int stacksize, int put_builtin) { 22#ifdef DEBUG
23extern lua_State *lua_state;
24void luaB_opentests (lua_State *L);
25#endif
26
27
28lua_State *lua_newstate (int stacksize) {
24 struct lua_longjmp myErrorJmp; 29 struct lua_longjmp myErrorJmp;
25 lua_State *L = luaM_new(NULL, lua_State); 30 lua_State *L = luaM_new(NULL, lua_State);
26 if (L == NULL) return NULL; /* memory allocation error */ 31 if (L == NULL) return NULL; /* memory allocation error */
@@ -54,8 +59,9 @@ lua_State *lua_newstate (int stacksize, int put_builtin) {
54 luaS_init(L); 59 luaS_init(L);
55 luaX_init(L); 60 luaX_init(L);
56 luaT_init(L); 61 luaT_init(L);
57 if (put_builtin) 62#ifdef DEBUG
58 luaB_predefine(L); 63 luaB_opentests(L);
64#endif
59 L->GCthreshold = L->nblocks*4; 65 L->GCthreshold = L->nblocks*4;
60 L->errorJmp = NULL; 66 L->errorJmp = NULL;
61 return L; 67 return L;
@@ -67,10 +73,6 @@ lua_State *lua_newstate (int stacksize, int put_builtin) {
67} 73}
68 74
69 75
70#ifdef DEBUG
71extern lua_State *lua_state;
72#endif
73
74void lua_close (lua_State *L) { 76void lua_close (lua_State *L) {
75 luaC_collect(L, 1); /* collect all elements */ 77 luaC_collect(L, 1); /* collect all elements */
76 LUA_ASSERT(L->rootproto == NULL, "list should be empty"); 78 LUA_ASSERT(L->rootproto == NULL, "list should be empty");
@@ -83,6 +85,7 @@ void lua_close (lua_State *L) {
83 luaM_free(L, L->Mbuffer); 85 luaM_free(L, L->Mbuffer);
84 LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks"); 86 LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
85 luaM_free(L, L); 87 luaM_free(L, L);
88 LUA_ASSERT(L->Cbase == L->stack, "stack not empty");
86 LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!"); 89 LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
87 LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!"); 90 LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
88} 91}
diff --git a/lstrlib.c b/lstrlib.c
index 827a59ff..6cdd87da 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.49 2000/08/31 13:30:22 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.50 2000/08/31 20:23:40 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -469,7 +469,7 @@ static void add_s (lua_State *L, struct Capture *cap) {
469 size_t oldbuff; 469 size_t oldbuff;
470 int n; 470 int n;
471 const char *s; 471 const char *s;
472 lua_pushobject(L, 3); 472 lua_pushvalue(L, 3);
473 n = push_captures(L, cap); 473 n = push_captures(L, cap);
474 /* function may use buffer, so save it and create a new one */ 474 /* function may use buffer, so save it and create a new one */
475 oldbuff = luaL_newbuffer(L, 0); 475 oldbuff = luaL_newbuffer(L, 0);
diff --git a/ltests.c b/ltests.c
index 79b6d970..92c543d6 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.38 2000/08/31 13:29:47 roberto Exp roberto $ 2** $Id: ltests.c,v 1.39 2000/08/31 20:23:40 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -38,10 +38,9 @@ void luaB_opentests (lua_State *L);
38 38
39 39
40static void setnameval (lua_State *L, const char *name, int val) { 40static void setnameval (lua_State *L, const char *name, int val) {
41 lua_pushobject(L, -1);
42 lua_pushstring(L, name); 41 lua_pushstring(L, name);
43 lua_pushnumber(L, val); 42 lua_pushnumber(L, val);
44 lua_settable(L); 43 lua_settable(L, -3);
45} 44}
46 45
47 46
@@ -100,10 +99,9 @@ static int listcode (lua_State *L) {
100 setnameval(L, "numparams", p->numparams); 99 setnameval(L, "numparams", p->numparams);
101 pc = 0; 100 pc = 0;
102 do { 101 do {
103 lua_pushobject(L, -1);
104 lua_pushnumber(L, pc+1); 102 lua_pushnumber(L, pc+1);
105 res = pushop(L, p, pc++); 103 res = pushop(L, p, pc++);
106 lua_settable(L); 104 lua_settable(L, -3);
107 } while (res); 105 } while (res);
108 return 1; 106 return 1;
109} 107}
@@ -116,10 +114,9 @@ static int liststrings (lua_State *L) {
116 p = clvalue(luaA_index(L, 1))->f.l; 114 p = clvalue(luaA_index(L, 1))->f.l;
117 lua_newtable(L); 115 lua_newtable(L);
118 for (i=0; i<p->nkstr; i++) { 116 for (i=0; i<p->nkstr; i++) {
119 lua_pushobject(L, -1);
120 lua_pushnumber(L, i+1); 117 lua_pushnumber(L, i+1);
121 lua_pushstring(L, p->kstr[i]->str); 118 lua_pushstring(L, p->kstr[i]->str);
122 lua_settable(L); 119 lua_settable(L, -3);
123 } 120 }
124 return 1; 121 return 1;
125} 122}
@@ -241,7 +238,7 @@ static int string_query (lua_State *L) {
241 238
242static int tref (lua_State *L) { 239static int tref (lua_State *L) {
243 luaL_checktype(L, 1, "any"); 240 luaL_checktype(L, 1, "any");
244 lua_pushobject(L, 1); 241 lua_pushvalue(L, 1);
245 lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1))); 242 lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
246 return 1; 243 return 1;
247} 244}
@@ -270,7 +267,7 @@ static int udataval (lua_State *L) {
270} 267}
271 268
272static int newstate (lua_State *L) { 269static int newstate (lua_State *L) {
273 lua_State *L1 = lua_newstate(luaL_check_int(L, 1), luaL_check_int(L, 2)); 270 lua_State *L1 = lua_newstate(luaL_check_int(L, 1));
274 if (L1) 271 if (L1)
275 lua_pushuserdata(L, L1); 272 lua_pushuserdata(L, L1);
276 else 273 else
@@ -390,17 +387,20 @@ static int testC (lua_State *L) {
390 else if EQ("pushnum") { 387 else if EQ("pushnum") {
391 lua_pushnumber(L, getnum); 388 lua_pushnumber(L, getnum);
392 } 389 }
393 else if EQ("pushobject") { 390 else if EQ("pushvalue") {
394 lua_pushobject(L, getnum); 391 lua_pushvalue(L, getnum);
395 } 392 }
396 else if EQ("move") { 393 else if EQ("remove") {
397 lua_move(L, getnum); 394 lua_remove(L, getnum);
398 } 395 }
399 else if EQ("insert") { 396 else if EQ("insert") {
400 lua_insert(L, getnum); 397 lua_insert(L, getnum);
401 } 398 }
402 else if EQ("next") { 399 else if EQ("next") {
403 lua_next(L); 400 lua_next(L, -2);
401 }
402 else if EQ("concat") {
403 lua_concat(L, getnum);
404 } 404 }
405 else if EQ("call") { 405 else if EQ("call") {
406 int narg = getnum; 406 int narg = getnum;
@@ -445,7 +445,7 @@ static const struct luaL_reg tests_funcs[] = {
445void luaB_opentests (lua_State *L) { 445void luaB_opentests (lua_State *L) {
446 lua_newtable(L); 446 lua_newtable(L);
447 lua_getglobals(L); 447 lua_getglobals(L);
448 lua_pushobject(L, -2); 448 lua_pushvalue(L, -2);
449 lua_setglobals(L); 449 lua_setglobals(L);
450 luaL_openl(L, tests_funcs); /* open functions inside new table */ 450 luaL_openl(L, tests_funcs); /* open functions inside new table */
451 lua_setglobals(L); /* restore old table of globals */ 451 lua_setglobals(L); /* restore old table of globals */
diff --git a/ltm.c b/ltm.c
index 1cb7647f..3d59b9a0 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.45 2000/08/07 20:21:34 roberto Exp roberto $ 2** $Id: ltm.c,v 1.46 2000/08/09 19:16:57 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*/
@@ -119,20 +119,23 @@ int luaT_effectivetag (lua_State *L, const TObject *o) {
119} 119}
120 120
121 121
122const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) { 122void lua_gettagmethod (lua_State *L, int t, const char *event) {
123 int e; 123 int e;
124 e = luaI_checkevent(L, event, t); 124 e = luaI_checkevent(L, event, t);
125 checktag(L, t); 125 checktag(L, t);
126 if (luaT_validevent(t, e)) 126 if (luaT_validevent(t, e))
127 return luaT_getim(L, t,e); 127 *L->top = *luaT_getim(L, t,e);
128 else 128 else
129 return &luaO_nilobject; 129 ttype(L->top) = TAG_NIL;
130 L->top++;
130} 131}
131 132
132 133
133void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) { 134void lua_settagmethod (lua_State *L, int t, const char *event) {
134 TObject temp; 135 TObject temp;
135 int e; 136 int e;
137 LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
138 "function or nil expected");
136 e = luaI_checkevent(L, event, t); 139 e = luaI_checkevent(L, event, t);
137 checktag(L, t); 140 checktag(L, t);
138 if (!luaT_validevent(t, e)) 141 if (!luaT_validevent(t, e))
@@ -140,8 +143,8 @@ void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
140 luaT_eventname[e], luaO_typenames[t], 143 luaT_eventname[e], luaO_typenames[t],
141 (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag" 144 (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
142 : ""); 145 : "");
143 temp = *func; 146 temp = *(L->top - 1);
144 *func = *luaT_getim(L, t,e); 147 *(L->top - 1) = *luaT_getim(L, t,e);
145 *luaT_getim(L, t, e) = temp; 148 *luaT_getim(L, t, e) = temp;
146} 149}
147 150
diff --git a/ltm.h b/ltm.h
index 2a4510a0..2d15b3ef 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 1.13 2000/05/30 18:54:49 roberto Exp roberto $ 2** $Id: ltm.h,v 1.14 2000/08/07 20:21:34 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*/
@@ -50,8 +50,6 @@ extern const char *const luaT_eventname[];
50void luaT_init (lua_State *L); 50void luaT_init (lua_State *L);
51void luaT_realtag (lua_State *L, int tag); 51void luaT_realtag (lua_State *L, int tag);
52int luaT_effectivetag (lua_State *L, const TObject *o); 52int luaT_effectivetag (lua_State *L, const TObject *o);
53void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func);
54const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event);
55int luaT_validevent (int t, int e); /* used by compatibility module */ 53int luaT_validevent (int t, int e); /* used by compatibility module */
56 54
57 55
diff --git a/lua.c b/lua.c
index 577ada06..05da8bfb 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.48 2000/08/31 14:28:17 roberto Exp roberto $ 2** $Id: lua.c,v 1.49 2000/08/31 20:23:40 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*/
@@ -54,6 +54,7 @@ extern void USERINIT (void);
54#else 54#else
55#define USERINIT userinit 55#define USERINIT userinit
56static void userinit (void) { 56static void userinit (void) {
57 lua_baselibopen(L);
57 lua_iolibopen(L); 58 lua_iolibopen(L);
58 lua_strlibopen(L); 59 lua_strlibopen(L);
59 lua_mathlibopen(L); 60 lua_mathlibopen(L);
@@ -103,7 +104,7 @@ static void print_message (void) {
103 fprintf(stderr, 104 fprintf(stderr,
104 "usage: lua [options]. Available options are:\n" 105 "usage: lua [options]. Available options are:\n"
105 " - execute stdin as a file\n" 106 " - execute stdin as a file\n"
106 " -c close lua when exiting\n" 107 " -c close Lua when exiting\n"
107 " -e stat execute string `stat'\n" 108 " -e stat execute string `stat'\n"
108 " -f name execute file `name' with remaining arguments in table `arg'\n" 109 " -f name execute file `name' with remaining arguments in table `arg'\n"
109 " -i enter interactive mode with prompt\n" 110 " -i enter interactive mode with prompt\n"
@@ -134,12 +135,14 @@ static void getargs (char *argv[]) {
134 lua_newtable(L); 135 lua_newtable(L);
135 for (i=0; argv[i]; i++) { 136 for (i=0; argv[i]; i++) {
136 /* arg[i] = argv[i] */ 137 /* arg[i] = argv[i] */
137 lua_pushobject(L, -1); lua_pushnumber(L, i); 138 lua_pushnumber(L, i);
138 lua_pushstring(L, argv[i]); lua_settable(L); 139 lua_pushstring(L, argv[i]);
140 lua_settable(L, -3);
139 } 141 }
140 /* arg.n = maximum index in table `arg' */ 142 /* arg.n = maximum index in table `arg' */
141 lua_pushobject(L, -1); lua_pushstring(L, "n"); 143 lua_pushstring(L, "n");
142 lua_pushnumber(L, i-1); lua_settable(L); 144 lua_pushnumber(L, i-1);
145 lua_settable(L, -3);
143} 146}
144 147
145 148
@@ -311,7 +314,7 @@ int main (int argc, char *argv[]) {
311 int status; 314 int status;
312 opt.toclose = 0; 315 opt.toclose = 0;
313 getstacksize(argc, argv, &opt); /* handle option `-s' */ 316 getstacksize(argc, argv, &opt); /* handle option `-s' */
314 L = lua_newstate(opt.stacksize, 1); /* create state */ 317 L = lua_newstate(opt.stacksize); /* create state */
315 USERINIT(); /* open libraries */ 318 USERINIT(); /* open libraries */
316 register_getargs(argv); /* create `getargs' function */ 319 register_getargs(argv); /* create `getargs' function */
317 status = handle_argv(argv+1, &opt); 320 status = handle_argv(argv+1, &opt);
diff --git a/lua.h b/lua.h
index e81030d3..2b51deab 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.64 2000/08/31 20:23:40 roberto Exp roberto $ 2** $Id: lua.h,v 1.65 2000/08/31 21:01:43 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
@@ -52,7 +52,7 @@ typedef int (*lua_CFunction) (lua_State *L);
52/* 52/*
53** state manipulation 53** state manipulation
54*/ 54*/
55lua_State *lua_newstate (int stacksize, int builtin); 55lua_State *lua_newstate (int stacksize);
56void lua_close (lua_State *L); 56void lua_close (lua_State *L);
57 57
58 58
@@ -61,8 +61,8 @@ void lua_close (lua_State *L);
61*/ 61*/
62int lua_gettop (lua_State *L); 62int lua_gettop (lua_State *L);
63void lua_settop (lua_State *L, int index); 63void lua_settop (lua_State *L, int index);
64void lua_pushobject (lua_State *L, int index); 64void lua_pushvalue (lua_State *L, int index);
65void lua_move (lua_State *L, int index); 65void lua_remove (lua_State *L, int index);
66void lua_insert (lua_State *L, int index); 66void lua_insert (lua_State *L, int index);
67int lua_stackspace (lua_State *L); 67int lua_stackspace (lua_State *L);
68 68
@@ -77,6 +77,7 @@ int lua_iscfunction (lua_State *L, int index);
77int lua_tag (lua_State *L, int index); 77int lua_tag (lua_State *L, int index);
78 78
79int lua_equal (lua_State *L, int index1, int index2); 79int lua_equal (lua_State *L, int index1, int index2);
80int lua_lessthan (lua_State *L, int index1, int index2);
80 81
81double lua_tonumber (lua_State *L, int index); 82double lua_tonumber (lua_State *L, int index);
82const char *lua_tostring (lua_State *L, int index); 83const char *lua_tostring (lua_State *L, int index);
@@ -101,8 +102,9 @@ void lua_pushusertag (lua_State *L, void *u, int tag);
101** get functions (Lua -> stack) 102** get functions (Lua -> stack)
102*/ 103*/
103void lua_getglobal (lua_State *L, const char *name); 104void lua_getglobal (lua_State *L, const char *name);
104void lua_gettable (lua_State *L); 105void lua_gettable (lua_State *L, int tableindex);
105void lua_rawget (lua_State *L); 106void lua_rawget (lua_State *L, int tableindex);
107void lua_rawgeti (lua_State *L, int tableindex, int n);
106void lua_getglobals (lua_State *L); 108void lua_getglobals (lua_State *L);
107void lua_gettagmethod (lua_State *L, int tag, const char *event); 109void lua_gettagmethod (lua_State *L, int tag, const char *event);
108 110
@@ -115,8 +117,9 @@ void lua_newtable (lua_State *L);
115** set functions (stack -> Lua) 117** set functions (stack -> Lua)
116*/ 118*/
117void lua_setglobal (lua_State *L, const char *name); 119void lua_setglobal (lua_State *L, const char *name);
118void lua_settable (lua_State *L); 120void lua_settable (lua_State *L, int tableindex);
119void lua_rawset (lua_State *L); 121void lua_rawset (lua_State *L, int tableindex);
122void lua_rawseti (lua_State *L, int tableindex, int n);
120void lua_setglobals (lua_State *L); 123void lua_setglobals (lua_State *L);
121void lua_settagmethod (lua_State *L, int tag, const char *event); 124void lua_settagmethod (lua_State *L, int tag, const char *event);
122int lua_ref (lua_State *L, int lock); 125int lua_ref (lua_State *L, int lock);
@@ -145,9 +148,10 @@ void lua_unref (lua_State *L, int ref);
145 148
146long lua_collectgarbage (lua_State *L, long limit); 149long lua_collectgarbage (lua_State *L, long limit);
147 150
148int lua_next (lua_State *L); 151int lua_next (lua_State *L, int tableindex);
149int lua_getn (lua_State *L, int index); 152int lua_getn (lua_State *L, int tableindex);
150 153
154void lua_concat (lua_State *L, int n);
151 155
152 156
153/* 157/*
diff --git a/lualib.h b/lualib.h
index 0a0fbe24..463951ab 100644
--- a/lualib.h
+++ b/lualib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lualib.h,v 1.9 2000/06/16 17:22:43 roberto Exp roberto $ 2** $Id: lualib.h,v 1.10 2000/08/09 19:16:57 roberto Exp roberto $
3** Lua standard libraries 3** Lua standard libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,6 +10,7 @@
10 10
11#include "lua.h" 11#include "lua.h"
12 12
13void lua_baselibopen (lua_State *L);
13void lua_iolibopen (lua_State *L); 14void lua_iolibopen (lua_State *L);
14void lua_strlibopen (lua_State *L); 15void lua_strlibopen (lua_State *L);
15void lua_mathlibopen (lua_State *L); 16void lua_mathlibopen (lua_State *L);
@@ -17,26 +18,6 @@ void lua_dblibopen (lua_State *L);
17 18
18 19
19 20
20/*
21** ===============================================================
22** Macros (and functions) for single-state use
23** ===============================================================
24*/
25
26#ifdef LUA_SINGLESTATE
27
28#define lua_iolibopen() (lua_iolibopen)(lua_state)
29#define lua_strlibopen() (lua_strlibopen)(lua_state)
30#define lua_mathlibopen() (lua_mathlibopen)(lua_state)
31#define lua_dblibopen() (lua_dblibopen)(lua_state)
32
33/* this function should be used only in single-state mode */
34void lua_userinit (void);
35
36#endif
37
38
39
40/* Auxiliary functions (private) */ 21/* Auxiliary functions (private) */
41 22
42const char *luaI_classend (lua_State *L, const char *p); 23const char *luaI_classend (lua_State *L, const char *p);
diff --git a/lvm.c b/lvm.c
index a2aa456e..a8e81dd5 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.132 2000/08/31 14:08:27 roberto Exp roberto $ 2** $Id: lvm.c,v 1.133 2000/08/31 21:02:55 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*/
@@ -112,10 +112,10 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
112 112
113/* 113/*
114** Function to index a table. 114** Function to index a table.
115** Receives the table at top-2 and the index at top-1. 115** Receives the table at `t' and the key at top.
116*/ 116*/
117void luaV_gettable (lua_State *L, StkId top) { 117const TObject *luaV_gettable (lua_State *L, StkId t) {
118 StkId t = top-2; 118 const TObject *im;
119 int tg; 119 int tg;
120 if (ttype(t) == TAG_TABLE && /* `t' is a table? */ 120 if (ttype(t) == TAG_TABLE && /* `t' is a table? */
121 ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ 121 ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
@@ -123,46 +123,49 @@ void luaV_gettable (lua_State *L, StkId top) {
123 /* do a primitive get */ 123 /* do a primitive get */
124 const TObject *h = luaH_get(L, hvalue(t), t+1); 124 const TObject *h = luaH_get(L, hvalue(t), t+1);
125 /* result is no nil or there is no `index' tag method? */ 125 /* result is no nil or there is no `index' tag method? */
126 const TObject *im;
127 if (ttype(h) != TAG_NIL || 126 if (ttype(h) != TAG_NIL ||
128 (ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL)) 127 (ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL))
129 *t = *h; /* put result into table position */ 128 return h; /* return result */
130 else { /* call `index' tag method */ 129 /* else call `index' tag method */
131 L->top = top;
132 luaD_callTM(L, im, 2, 1);
133 }
134 } 130 }
135 else { /* try a 'gettable' TM */ 131 else { /* try a 'gettable' TM */
136 const TObject *im = luaT_getimbyObj(L, t, IM_GETTABLE); 132 im = luaT_getimbyObj(L, t, IM_GETTABLE);
137 L->top = top; 133 }
138 if (ttype(im) != TAG_NIL) /* call `gettable' tag method */ 134 if (ttype(im) != TAG_NIL) { /* is there a tag method? */
139 luaD_callTM(L, im, 2, 1); 135 luaD_checkstack(L, 2);
140 else /* no tag method */ 136 *(L->top+1) = *(L->top-1); /* key */
141 luaG_typeerror(L, t, "index"); 137 *L->top = *t; /* table */
138 *(L->top-1) = *im; /* tag method */
139 L->top += 2;
140 luaD_call(L, L->top - 3, 1);
141 return L->top - 1; /* call result */
142 }
143 else { /* no tag method */
144 luaG_typeerror(L, t, "index");
145 return NULL; /* to avoid warnings */
142 } 146 }
143} 147}
144 148
145 149
146/* 150/*
147** Receives table at *t, index at *(t+1) and value at `top'. 151** Receives table at `t', key at `key' and value at top.
148*/ 152*/
149void luaV_settable (lua_State *L, StkId t, StkId top) { 153void luaV_settable (lua_State *L, StkId t, StkId key) {
150 int tg; 154 int tg;
151 if (ttype(t) == TAG_TABLE && /* `t' is a table? */ 155 if (ttype(t) == TAG_TABLE && /* `t' is a table? */
152 ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ 156 ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
153 ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */ 157 ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */
154 *luaH_set(L, hvalue(t), t+1) = *(top-1); /* do a primitive set */ 158 *luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */
155 else { /* try a `settable' tag method */ 159 else { /* try a `settable' tag method */
156 const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE); 160 const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE);
157 L->top = top;
158 if (ttype(im) != TAG_NIL) { 161 if (ttype(im) != TAG_NIL) {
159 luaD_checkstack(L, 3); 162 luaD_checkstack(L, 3);
160 *(top+2) = *(top-1); 163 *(L->top+2) = *(L->top-1);
161 *(top+1) = *(t+1); 164 *(L->top+1) = *key;
162 *(top) = *t; 165 *(L->top) = *t;
163 *(top-1) = *im; 166 *(L->top-1) = *im;
164 L->top = top+3; 167 L->top += 3;
165 luaD_call(L, top-1, 0); /* call `settable' tag method */ 168 luaD_call(L, L->top - 4, 0); /* call `settable' tag method */
166 } 169 }
167 else /* no tag method... */ 170 else /* no tag method... */
168 luaG_typeerror(L, t, "index"); 171 luaG_typeerror(L, t, "index");
@@ -170,49 +173,48 @@ void luaV_settable (lua_State *L, StkId t, StkId top) {
170} 173}
171 174
172 175
173void luaV_getglobal (lua_State *L, TString *s, StkId top) { 176const TObject *luaV_getglobal (lua_State *L, TString *s) {
174 const TObject *value = luaH_getstr(L->gt, s); 177 const TObject *value = luaH_getstr(L->gt, s);
175 TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); 178 TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
176 if (ttype(im) == TAG_NIL) /* is there a tag method? */ 179 if (ttype(im) == TAG_NIL) /* is there a tag method? */
177 *top = *value; /* default behavior */ 180 return value; /* default behavior */
178 else { /* tag method */ 181 else { /* tag method */
179 L->top = top;
180 luaD_checkstack(L, 3); 182 luaD_checkstack(L, 3);
181 *top = *im; 183 *L->top = *im;
182 ttype(top+1) = TAG_STRING; 184 ttype(L->top+1) = TAG_STRING;
183 tsvalue(top+1) = s; /* global name */ 185 tsvalue(L->top+1) = s; /* global name */
184 *(top+2) = *value; 186 *(L->top+2) = *value;
185 L->top = top+3; 187 L->top += 3;
186 luaD_call(L, top, 1); 188 luaD_call(L, L->top - 3, 1);
189 return L->top - 1;
187 } 190 }
188} 191}
189 192
190 193
191void luaV_setglobal (lua_State *L, TString *s, StkId top) { 194void luaV_setglobal (lua_State *L, TString *s) {
192 const TObject *oldvalue = luaH_getstr(L->gt, s); 195 const TObject *oldvalue = luaH_getstr(L->gt, s);
193 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); 196 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
194 if (ttype(im) == TAG_NIL) { /* is there a tag method? */ 197 if (ttype(im) == TAG_NIL) { /* is there a tag method? */
195 if (oldvalue != &luaO_nilobject) { 198 if (oldvalue != &luaO_nilobject) {
196 /* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */ 199 /* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
197 *(TObject *)oldvalue = *(top-1); 200 *(TObject *)oldvalue = *(L->top - 1);
198 } 201 }
199 else { 202 else {
200 TObject key; 203 TObject key;
201 ttype(&key) = TAG_STRING; 204 ttype(&key) = TAG_STRING;
202 tsvalue(&key) = s; 205 tsvalue(&key) = s;
203 *luaH_set(L, L->gt, &key) = *(top-1); 206 *luaH_set(L, L->gt, &key) = *(L->top - 1);
204 } 207 }
205 } 208 }
206 else { 209 else {
207 L->top = top;
208 luaD_checkstack(L, 3); 210 luaD_checkstack(L, 3);
209 *(top+2) = *(top-1); /* new value */ 211 *(L->top+2) = *(L->top-1); /* new value */
210 *(top+1) = *oldvalue; 212 *(L->top+1) = *oldvalue;
211 ttype(top) = TAG_STRING; 213 ttype(L->top) = TAG_STRING;
212 tsvalue(top) = s; 214 tsvalue(L->top) = s;
213 *(top-1) = *im; 215 *(L->top-1) = *im;
214 L->top = top+3; 216 L->top += 3;
215 luaD_call(L, top-1, 0); 217 luaD_call(L, L->top - 4, 0);
216 } 218 }
217} 219}
218 220
@@ -280,7 +282,7 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top)
280} 282}
281 283
282 284
283static void strconc (lua_State *L, int total, StkId top) { 285void luaV_strconc (lua_State *L, int total, StkId top) {
284 do { 286 do {
285 int n = 2; /* number of elements handled in this pass (at least 2) */ 287 int n = 2; /* number of elements handled in this pass (at least 2) */
286 if (tostring(L, top-2) || tostring(L, top-1)) { 288 if (tostring(L, top-2) || tostring(L, top-1)) {
@@ -425,26 +427,28 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
425 break; 427 break;
426 } 428 }
427 case OP_GETGLOBAL: { 429 case OP_GETGLOBAL: {
428 luaV_getglobal(L, kstr[GETARG_U(i)], top); 430 L->top = top;
431 *top = *luaV_getglobal(L, kstr[GETARG_U(i)]);
429 top++; 432 top++;
430 break; 433 break;
431 } 434 }
432 case OP_GETTABLE: { 435 case OP_GETTABLE: {
433 luaV_gettable(L, top); 436 L->top = top;
434 top--; 437 top--;
438 *(top-1) = *luaV_gettable(L, top-1);
435 break; 439 break;
436 } 440 }
437 case OP_GETDOTTED: { 441 case OP_GETDOTTED: {
438 ttype(top) = TAG_STRING; 442 ttype(top) = TAG_STRING;
439 tsvalue(top++) = kstr[GETARG_U(i)]; 443 tsvalue(top) = kstr[GETARG_U(i)];
440 luaV_gettable(L, top); 444 L->top = top+1;
441 top--; 445 *(top-1) = *luaV_gettable(L, top-1);
442 break; 446 break;
443 } 447 }
444 case OP_GETINDEXED: { 448 case OP_GETINDEXED: {
445 *top++ = *(base+GETARG_U(i)); 449 *top = *(base+GETARG_U(i));
446 luaV_gettable(L, top); 450 L->top = top+1;
447 top--; 451 *(top-1) = *luaV_gettable(L, top-1);
448 break; 452 break;
449 } 453 }
450 case OP_PUSHSELF: { 454 case OP_PUSHSELF: {
@@ -452,7 +456,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
452 receiver = *(top-1); 456 receiver = *(top-1);
453 ttype(top) = TAG_STRING; 457 ttype(top) = TAG_STRING;
454 tsvalue(top++) = kstr[GETARG_U(i)]; 458 tsvalue(top++) = kstr[GETARG_U(i)];
455 luaV_gettable(L, top); 459 L->top = top;
460 *(top-2) = *luaV_gettable(L, top-2);
456 *(top-1) = receiver; 461 *(top-1) = receiver;
457 break; 462 break;
458 } 463 }
@@ -469,12 +474,15 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
469 break; 474 break;
470 } 475 }
471 case OP_SETGLOBAL: { 476 case OP_SETGLOBAL: {
472 luaV_setglobal(L, kstr[GETARG_U(i)], top); 477 L->top = top;
478 luaV_setglobal(L, kstr[GETARG_U(i)]);
473 top--; 479 top--;
474 break; 480 break;
475 } 481 }
476 case OP_SETTABLE: { 482 case OP_SETTABLE: {
477 luaV_settable(L, top-GETARG_A(i), top); 483 StkId t = top-GETARG_A(i);
484 L->top = top;
485 luaV_settable(L, t, t+1);
478 top -= GETARG_B(i); /* pop values */ 486 top -= GETARG_B(i); /* pop values */
479 break; 487 break;
480 } 488 }
@@ -548,7 +556,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
548 } 556 }
549 case OP_CONCAT: { 557 case OP_CONCAT: {
550 int n = GETARG_U(i); 558 int n = GETARG_U(i);
551 strconc(L, n, top); 559 luaV_strconc(L, n, top);
552 top -= n-1; 560 top -= n-1;
553 L->top = top; 561 L->top = top;
554 luaC_checkGC(L); 562 luaC_checkGC(L);
diff --git a/lvm.h b/lvm.h
index 5783c209..e74eaf30 100644
--- a/lvm.h
+++ b/lvm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.h,v 1.24 2000/08/29 14:41:56 roberto Exp roberto $ 2** $Id: lvm.h,v 1.25 2000/08/31 21:02:55 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*/
@@ -19,13 +19,14 @@
19 19
20int luaV_tonumber (TObject *obj); 20int luaV_tonumber (TObject *obj);
21int luaV_tostring (lua_State *L, TObject *obj); 21int luaV_tostring (lua_State *L, TObject *obj);
22void luaV_gettable (lua_State *L, StkId top); 22const TObject *luaV_gettable (lua_State *L, StkId t);
23void luaV_settable (lua_State *L, StkId t, StkId top); 23void luaV_settable (lua_State *L, StkId t, StkId key);
24void luaV_getglobal (lua_State *L, TString *s, StkId top); 24const TObject *luaV_getglobal (lua_State *L, TString *s);
25void luaV_setglobal (lua_State *L, TString *s, StkId top); 25void luaV_setglobal (lua_State *L, TString *s);
26StkId luaV_execute (lua_State *L, const Closure *cl, StkId base); 26StkId luaV_execute (lua_State *L, const Closure *cl, StkId base);
27void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems); 27void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
28void luaV_Lclosure (lua_State *L, Proto *l, int nelems); 28void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
29int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top); 29int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top);
30void luaV_strconc (lua_State *L, int total, StkId top);
30 31
31#endif 32#endif