aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-31 17:23:40 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-31 17:23:40 -0300
commitf0b3cd1d6f35ba34091450d5e3057269114a17b6 (patch)
treeceffcf31f1823709b7ab4e63801e5e483abf8490
parentfb5e6d5ac498649b8e1b6bf068d18078ef70d523 (diff)
downloadlua-f0b3cd1d6f35ba34091450d5e3057269114a17b6.tar.gz
lua-f0b3cd1d6f35ba34091450d5e3057269114a17b6.tar.bz2
lua-f0b3cd1d6f35ba34091450d5e3057269114a17b6.zip
new API functions `pop', `insert', and `move'
-rw-r--r--lapi.c38
-rw-r--r--lbuiltin.c8
-rw-r--r--liolib.c18
-rw-r--r--lmathlib.c4
-rw-r--r--lstrlib.c4
-rw-r--r--ltests.c13
-rw-r--r--lua.c6
-rw-r--r--lua.h6
8 files changed, 62 insertions, 35 deletions
diff --git a/lapi.c b/lapi.c
index 75159082..5364c040 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.90 2000/08/29 20:43:28 roberto Exp roberto $ 2** $Id: lapi.c,v 1.91 2000/08/31 14:08:27 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*/
@@ -65,7 +65,25 @@ void lua_settop (lua_State *L, int index) {
65 if (index >= 0) 65 if (index >= 0)
66 luaD_adjusttop(L, L->Cbase, index); 66 luaD_adjusttop(L, L->Cbase, index);
67 else 67 else
68 L->top += index; /* index is negative */ 68 L->top = L->top+index+1; /* index is negative */
69}
70
71
72void lua_move (lua_State *L, int index) {
73 TObject *p = Index(L, index);
74 TObject temp = *p;
75 while (++p < L->top) *(p-1) = *p;
76 *(L->top-1) = temp;
77}
78
79
80void lua_insert (lua_State *L, int index) {
81 TObject temp = *(L->top-1);
82 TObject *p = Index(L, index);
83 TObject *q;
84 for (q = L->top-1; q>p; q--)
85 *q = *(q-1);
86 *p = temp;
69} 87}
70 88
71 89
@@ -218,8 +236,7 @@ void lua_gettable (lua_State *L) {
218 236
219 237
220void lua_rawget (lua_State *L) { 238void lua_rawget (lua_State *L) {
221 if (ttype(L->top - 2) != TAG_TABLE) 239 LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "not a table");
222 lua_error(L, "indexed expression not a table");
223 *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1); 240 *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1);
224 L->top--; 241 L->top--;
225} 242}
@@ -278,8 +295,7 @@ void lua_settable (lua_State *L) {
278 295
279 296
280void lua_rawset (lua_State *L) { 297void lua_rawset (lua_State *L) {
281 if (ttype(L->top-3) != TAG_TABLE) 298 LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "not a table");
282 lua_error(L, "indexed expression not a table");
283 *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1); 299 *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1);
284 L->top -= 3; 300 L->top -= 3;
285} 301}
@@ -287,8 +303,7 @@ void lua_rawset (lua_State *L) {
287 303
288void lua_setglobals (lua_State *L) { 304void lua_setglobals (lua_State *L) {
289 TObject *newtable = --L->top; 305 TObject *newtable = --L->top;
290 if (ttype(newtable) != TAG_TABLE) 306 LUA_ASSERT(ttype(newtable) == TAG_TABLE, "not a table");
291 lua_error(L, "Lua API error - invalid value for global table");
292 L->gt = hvalue(newtable); 307 L->gt = hvalue(newtable);
293} 308}
294 309
@@ -350,9 +365,7 @@ void lua_settag (lua_State *L, int tag) {
350 365
351void lua_unref (lua_State *L, int ref) { 366void lua_unref (lua_State *L, int ref) {
352 if (ref >= 0) { 367 if (ref >= 0) {
353 if (ref >= L->refSize || L->refArray[ref].st >= 0) 368 LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
354 lua_error(L, "Lua API error - "
355 "invalid argument for function `lua_unref'");
356 L->refArray[ref].st = L->refFree; 369 L->refArray[ref].st = L->refFree;
357 L->refFree = ref; 370 L->refFree = ref;
358 } 371 }
@@ -362,8 +375,7 @@ void lua_unref (lua_State *L, int ref) {
362int lua_next (lua_State *L) { 375int lua_next (lua_State *L) {
363 const TObject *t = Index(L, -2); 376 const TObject *t = Index(L, -2);
364 Node *n; 377 Node *n;
365 if (ttype(t) != TAG_TABLE) 378 LUA_ASSERT(ttype(t) == TAG_TABLE, "object is not a table in `lua_next'");
366 lua_error(L, "Lua API error - object is not a table in `lua_next'");
367 n = luaH_next(L, hvalue(t), Index(L, -1)); 379 n = luaH_next(L, hvalue(t), Index(L, -1));
368 if (n) { 380 if (n) {
369 *(L->top-1) = *key(n); 381 *(L->top-1) = *key(n);
diff --git a/lbuiltin.c b/lbuiltin.c
index f4368f56..348214e4 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.125 2000/08/31 14:08:27 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.126 2000/08/31 16:52:06 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*/
@@ -92,7 +92,7 @@ int luaB_print (lua_State *L) {
92 lua_error(L, "`tostring' must return a string to `print'"); 92 lua_error(L, "`tostring' must return a string to `print'");
93 if (i>1) fputs("\t", stdout); 93 if (i>1) fputs("\t", stdout);
94 fputs(s, stdout); 94 fputs(s, stdout);
95 lua_settop(L, -1); /* pop result */ 95 lua_pop(L, 1); /* pop result */
96 } 96 }
97 fputs("\n", stdout); 97 fputs("\n", stdout);
98 return 0; 98 return 0;
@@ -201,7 +201,7 @@ int luaB_settagmethod (lua_State *L) {
201 lua_pushnil(L); /* to get its tag */ 201 lua_pushnil(L); /* to get its tag */
202 if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1)) 202 if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1))
203 lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua"); 203 lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
204 lua_settop(L, -1); /* remove the nil */ 204 lua_pop(L, 1); /* remove the nil */
205 lua_settagmethod(L, tag, event); 205 lua_settagmethod(L, tag, event);
206 return 1; 206 return 1;
207} 207}
@@ -501,7 +501,7 @@ static int luaB_foreach (lua_State *L) {
501 if (lua_call(L, 2, 1) != 0) lua_error(L, NULL); 501 if (lua_call(L, 2, 1) != 0) lua_error(L, NULL);
502 if (!lua_isnil(L, -1)) 502 if (!lua_isnil(L, -1))
503 return 1; 503 return 1;
504 lua_settop(L, -2); /* remove value and result */ 504 lua_pop(L, 2); /* remove value and result */
505 } 505 }
506} 506}
507 507
diff --git a/liolib.c b/liolib.c
index 33a15a81..46d619c1 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.74 2000/08/29 20:43:28 roberto Exp roberto $ 2** $Id: liolib.c,v 1.75 2000/08/31 13:30:10 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*/
@@ -150,14 +150,14 @@ static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) {
150 150
151static int io_close (lua_State *L) { 151static int io_close (lua_State *L) {
152 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 152 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
153 lua_settop(L, -1); /* remove upvalue */ 153 lua_pop(L, 1); /* remove upvalue */
154 return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1))); 154 return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1)));
155} 155}
156 156
157 157
158static int file_collect (lua_State *L) { 158static int file_collect (lua_State *L) {
159 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 159 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
160 lua_settop(L, -1); /* remove upvalue */ 160 lua_pop(L, 1); /* remove upvalue */
161 if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) { 161 if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) {
162 /* collecting `ctrl' itself */ 162 /* collecting `ctrl' itself */
163 lua_unref(L, ctrl->ref[INFILE]); 163 lua_unref(L, ctrl->ref[INFILE]);
@@ -176,7 +176,7 @@ static int file_collect (lua_State *L) {
176static int io_open (lua_State *L) { 176static int io_open (lua_State *L) {
177 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 177 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
178 FILE *f; 178 FILE *f;
179 lua_settop(L, -1); /* remove upvalue */ 179 lua_pop(L, 1); /* remove upvalue */
180 f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); 180 f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
181 if (f) { 181 if (f) {
182 lua_pushusertag(L, f, ctrl->iotag); 182 lua_pushusertag(L, f, ctrl->iotag);
@@ -191,7 +191,7 @@ static int io_open (lua_State *L) {
191static int io_fromto (lua_State *L, int inout, const char *mode) { 191static int io_fromto (lua_State *L, int inout, const char *mode) {
192 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 192 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
193 FILE *current; 193 FILE *current;
194 lua_settop(L, -1); /* remove upvalue */ 194 lua_pop(L, 1); /* remove upvalue */
195 if (lua_isnull(L, 1)) { 195 if (lua_isnull(L, 1)) {
196 closefile(L, ctrl, getfilebyref(L, ctrl, inout)); 196 closefile(L, ctrl, getfilebyref(L, ctrl, inout));
197 current = (inout == 0) ? stdin : stdout; 197 current = (inout == 0) ? stdin : stdout;
@@ -219,7 +219,7 @@ static int io_writeto (lua_State *L) {
219static int io_appendto (lua_State *L) { 219static int io_appendto (lua_State *L) {
220 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 220 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
221 FILE *current; 221 FILE *current;
222 lua_settop(L, -1); /* remove upvalue */ 222 lua_pop(L, 1); /* remove upvalue */
223 current = fopen(luaL_check_string(L, 1), "a"); 223 current = fopen(luaL_check_string(L, 1), "a");
224 return setreturn(L, ctrl, current, OUTFILE); 224 return setreturn(L, ctrl, current, OUTFILE);
225} 225}
@@ -366,7 +366,7 @@ static int io_read (lua_State *L) {
366 int n; 366 int n;
367 if (f) firstarg++; 367 if (f) firstarg++;
368 else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */ 368 else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */
369 lua_settop(L, -1); 369 lua_pop(L, 1);
370 if (firstarg > lastarg) { /* no arguments? */ 370 if (firstarg > lastarg) { /* no arguments? */
371 lua_settop(L, 0); /* erase upvalue and other eventual garbage */ 371 lua_settop(L, 0); /* erase upvalue and other eventual garbage */
372 firstarg = lastarg = 1; /* correct indices */ 372 firstarg = lastarg = 1; /* correct indices */
@@ -447,7 +447,7 @@ static int io_seek (lua_State *L) {
447 FILE *f; 447 FILE *f;
448 int op; 448 int op;
449 long offset; 449 long offset;
450 lua_settop(L, -1); /* remove upvalue */ 450 lua_pop(L, 1); /* remove upvalue */
451 f = getnonullfile(L, ctrl, 1); 451 f = getnonullfile(L, ctrl, 1);
452 op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); 452 op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
453 offset = luaL_opt_long(L, 3, 0); 453 offset = luaL_opt_long(L, 3, 0);
@@ -465,7 +465,7 @@ static int io_seek (lua_State *L) {
465static int io_flush (lua_State *L) { 465static int io_flush (lua_State *L) {
466 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 466 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
467 FILE *f; 467 FILE *f;
468 lua_settop(L, -1); /* remove upvalue */ 468 lua_pop(L, 1); /* remove upvalue */
469 f = gethandle(L, ctrl, 1); 469 f = gethandle(L, ctrl, 1);
470 luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle"); 470 luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
471 return pushresult(L, fflush(f) == 0); 471 return pushresult(L, fflush(f) == 0);
diff --git a/lmathlib.c b/lmathlib.c
index d6957e76..ca14ee8c 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.26 2000/08/09 19:16:57 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.27 2000/08/28 17:57:04 roberto Exp roberto $
3** Standard mathematical library 3** Standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -233,7 +233,7 @@ void lua_mathlibopen (lua_State *L) {
233 lua_pushnumber(L, 0); /* to get its tag */ 233 lua_pushnumber(L, 0); /* to get its tag */
234 lua_pushcfunction(L, math_pow); 234 lua_pushcfunction(L, math_pow);
235 lua_settagmethod(L, lua_tag(L, -2), "pow"); 235 lua_settagmethod(L, lua_tag(L, -2), "pow");
236 lua_settop(L, -1); /* remove number */ 236 lua_pop(L, 1); /* remove number */
237 lua_pushnumber(L, PI); lua_setglobal(L, "PI"); 237 lua_pushnumber(L, PI); lua_setglobal(L, "PI");
238} 238}
239 239
diff --git a/lstrlib.c b/lstrlib.c
index 86cb90ed..827a59ff 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.48 2000/08/29 20:43:28 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.49 2000/08/31 13:30:22 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*/
@@ -481,7 +481,7 @@ static void add_s (lua_State *L, struct Capture *cap) {
481 s = lua_tostring(L, -1); 481 s = lua_tostring(L, -1);
482 if (s) 482 if (s)
483 addnchar(L, lua_tostring(L, -1), lua_strlen(L, -1)); 483 addnchar(L, lua_tostring(L, -1), lua_strlen(L, -1));
484 lua_settop(L, -1); /* pop function result */ 484 lua_pop(L, 1); /* pop function result */
485 } 485 }
486} 486}
487 487
diff --git a/ltests.c b/ltests.c
index 45b1f1c0..79b6d970 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.37 2000/08/29 19:05:11 roberto Exp roberto $ 2** $Id: ltests.c,v 1.38 2000/08/31 13:29:47 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*/
@@ -341,7 +341,7 @@ static int getnum (lua_State *L, const char **pc) {
341 skip(pc); 341 skip(pc);
342 if (**pc == '.') { 342 if (**pc == '.') {
343 res = (int)lua_tonumber(L, -1); 343 res = (int)lua_tonumber(L, -1);
344 lua_settop(L, -1); 344 lua_pop(L, 1);
345 (*pc)++; 345 (*pc)++;
346 return res; 346 return res;
347 } 347 }
@@ -384,12 +384,21 @@ static int testC (lua_State *L) {
384 else if EQ("settop") { 384 else if EQ("settop") {
385 lua_settop(L, getnum); 385 lua_settop(L, getnum);
386 } 386 }
387 else if EQ("pop") {
388 lua_pop(L, getnum);
389 }
387 else if EQ("pushnum") { 390 else if EQ("pushnum") {
388 lua_pushnumber(L, getnum); 391 lua_pushnumber(L, getnum);
389 } 392 }
390 else if EQ("pushobject") { 393 else if EQ("pushobject") {
391 lua_pushobject(L, getnum); 394 lua_pushobject(L, getnum);
392 } 395 }
396 else if EQ("move") {
397 lua_move(L, getnum);
398 }
399 else if EQ("insert") {
400 lua_insert(L, getnum);
401 }
393 else if EQ("next") { 402 else if EQ("next") {
394 lua_next(L); 403 lua_next(L);
395 } 404 }
diff --git a/lua.c b/lua.c
index 8fd014d1..577ada06 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.47 2000/08/29 14:33:31 roberto Exp roberto $ 2** $Id: lua.c,v 1.48 2000/08/31 14:28:17 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*/
@@ -87,7 +87,9 @@ static void laction (int i) {
87static int ldo (int (*f)(lua_State *l, const char *), const char *name) { 87static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
88 int res; 88 int res;
89 handler h = lreset(); 89 handler h = lreset();
90 int top = lua_gettop(L);
90 res = f(L, name); /* dostring | dofile */ 91 res = f(L, name); /* dostring | dofile */
92 lua_settop(L, top); /* remove eventual results */
91 signal(SIGINT, h); /* restore old action */ 93 signal(SIGINT, h); /* restore old action */
92 if (res == LUA_ERRMEM) { 94 if (res == LUA_ERRMEM) {
93 /* Lua gives no message in such case, so lua.c provides one */ 95 /* Lua gives no message in such case, so lua.c provides one */
@@ -177,9 +179,9 @@ static void manual_input (int version, int prompt) {
177 const char *s; 179 const char *s;
178 lua_getglobal(L, "_PROMPT"); 180 lua_getglobal(L, "_PROMPT");
179 s = lua_tostring(L, -1); 181 s = lua_tostring(L, -1);
180 lua_settop(L, -1); /* remove global */
181 if (!s) s = PROMPT; 182 if (!s) s = PROMPT;
182 fputs(s, stdout); 183 fputs(s, stdout);
184 lua_pop(L, 1); /* remove global */
183 } 185 }
184 for(;;) { 186 for(;;) {
185 int c = getchar(); 187 int c = getchar();
diff --git a/lua.h b/lua.h
index 0ad37ad6..a5862ba5 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.62 2000/08/29 20:43:28 roberto Exp roberto $ 2** $Id: lua.h,v 1.63 2000/08/31 14:08:27 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
@@ -62,6 +62,8 @@ void lua_close (lua_State *L);
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_pushobject (lua_State *L, int index);
65void lua_move (lua_State *L, int index);
66void lua_insert (lua_State *L, int index);
65int lua_stackspace (lua_State *L); 67int lua_stackspace (lua_State *L);
66 68
67 69
@@ -152,6 +154,8 @@ int lua_next (lua_State *L);
152** =============================================================== 154** ===============================================================
153*/ 155*/
154 156
157#define lua_pop(L,n) lua_settop(L, -(n)-1)
158
155#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n)) 159#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
156#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0) 160#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0)
157#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0) 161#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)