functions `for...' and `raw...' are obsolete now

This commit is contained in:
Roberto Ierusalimschy 2000-05-26 16:17:57 -03:00
parent c39345fba3
commit 8e617985fa
6 changed files with 178 additions and 103 deletions

52
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.80 2000/05/08 20:49:05 roberto Exp roberto $
** $Id: lapi.c,v 1.81 2000/05/24 13:54:49 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -120,11 +120,11 @@ lua_Object lua_gettable (lua_State *L) {
}
lua_Object lua_rawgettable (lua_State *L) {
lua_Object lua_rawget (lua_State *L) {
lua_Object res;
luaA_checkCargs(L, 2);
if (ttype(L->top-2) != TAG_TABLE)
lua_error(L, "indexed expression not a table in rawgettable");
lua_error(L, "indexed expression not a table");
res = luaA_putluaObject(L, luaH_get(L, avalue(L->top-2), L->top-1));
L->top -= 2;
return res;
@ -140,7 +140,7 @@ void lua_settable (lua_State *L) {
}
void lua_rawsettable (lua_State *L) {
void lua_rawset (lua_State *L) {
luaA_checkCargs(L, 3);
if (ttype(L->top-3) != TAG_TABLE)
lua_error(L, "indexed expression not a table");
@ -170,24 +170,6 @@ void lua_setglobal (lua_State *L, const char *name) {
}
/* deprecated */
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
lua_pushglobaltable(L);
lua_pushstring(L, name);
return lua_rawgettable(L);
}
/* deprecated */
void lua_rawsetglobal (lua_State *L, const char *name) {
TObject key;
luaA_checkCargs(L, 1);
ttype(&key) = TAG_STRING;
tsvalue(&key) = luaS_new(L, name);
luaH_set(L, L->gt, &key, --L->top);
}
const char *lua_type (lua_State *L, lua_Object o) {
UNUSED(L);
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
@ -376,3 +358,29 @@ int lua_next (lua_State *L, lua_Object t, int i) {
}
#if LUA_DEPRECATETFUNCS
/*
** obsolete functions
*/
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
lua_pushglobaltable(L);
lua_pushstring(L, name);
return lua_rawget(L);
}
void lua_rawsetglobal (lua_State *L, const char *name) {
lua_Object value;
lua_beginblock(L);
value = lua_pop(L);
lua_pushglobaltable(L);
lua_pushstring(L, name);
lua_pushobject(L, value);
lua_rawset(L);
lua_endblock(L);
}
#endif

View File

@ -1,5 +1,5 @@
/*
** $Id: lbuiltin.c,v 1.109 2000/05/09 14:50:16 roberto Exp roberto $
** $Id: lbuiltin.c,v 1.110 2000/05/24 13:54:49 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/
@ -106,7 +106,10 @@ void luaB__ALERT (lua_State *L) {
** The library `liolib' redefines _ERRORMESSAGE for better error information.
*/
void luaB__ERRORMESSAGE (lua_State *L) {
lua_Object al = lua_rawgetglobal(L, LUA_ALERT);
lua_Object al;
lua_pushglobaltable(L);
lua_pushstring(L, LUA_ALERT);
al = lua_rawget(L);
if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */
const char *s = luaL_check_string(L, 1);
char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n"));
@ -214,17 +217,17 @@ void luaB_globals (lua_State *L) {
lua_setglobaltable(L, luaL_tablearg(L, 1));
}
void luaB_rawgettable (lua_State *L) {
void luaB_rawget (lua_State *L) {
lua_pushobject(L, luaL_nonnullarg(L, 1));
lua_pushobject(L, luaL_nonnullarg(L, 2));
lua_pushobject(L, lua_rawgettable(L));
lua_pushobject(L, lua_rawget(L));
}
void luaB_rawsettable (lua_State *L) {
void luaB_rawset (lua_State *L) {
lua_pushobject(L, luaL_nonnullarg(L, 1));
lua_pushobject(L, luaL_nonnullarg(L, 2));
lua_pushobject(L, luaL_nonnullarg(L, 3));
lua_rawsettable(L);
lua_rawset(L);
}
void luaB_settagmethod (lua_State *L) {
@ -399,44 +402,6 @@ void luaB_assert (lua_State *L) {
}
void luaB_foreachi (lua_State *L) {
const Hash *t = gettable(L, 1);
int n = (int)getnarg(L, t);
int i;
lua_Object f = luaL_functionarg(L, 2);
luaD_checkstack(L, 3); /* for f, key, and val */
for (i=1; i<=n; i++) {
*(L->top++) = *f;
ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
*(L->top++) = *luaH_getnum(t, i);
luaD_call(L, L->top-3, 1);
if (ttype(L->top-1) != TAG_NIL)
return;
L->top--; /* remove nil result */
}
}
void luaB_foreach (lua_State *L) {
const Hash *a = gettable(L, 1);
lua_Object f = luaL_functionarg(L, 2);
int i;
luaD_checkstack(L, 3); /* for f, key, and val */
for (i=0; i<a->size; i++) {
const Node *nd = &(a->node[i]);
if (ttype(val(nd)) != TAG_NIL) {
*(L->top++) = *f;
*(L->top++) = *key(nd);
*(L->top++) = *val(nd);
luaD_call(L, L->top-3, 1);
if (ttype(L->top-1) != TAG_NIL)
return;
L->top--; /* remove result */
}
}
}
void luaB_getn (lua_State *L) {
lua_pushnumber(L, getnarg(L, gettable(L, 1)));
}
@ -562,21 +527,70 @@ void luaB_sort (lua_State *L) {
/* }====================================================== */
/*
** {======================================================
** Deprecated functions to manipulate global environment:
** all of them can be simulated through table operations
** some of them can be simulated through table operations
** over the global table.
** =======================================================
*/
#ifdef LUA_DEPRECATETFUNCS
static void luaB_foreachi (lua_State *L) {
const Hash *t = gettable(L, 1);
int n = (int)getnarg(L, t);
int i;
lua_Object f = luaL_functionarg(L, 2);
luaD_checkstack(L, 3); /* for f, key, and val */
for (i=1; i<=n; i++) {
*(L->top++) = *f;
ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
*(L->top++) = *luaH_getnum(t, i);
luaD_call(L, L->top-3, 1);
if (ttype(L->top-1) != TAG_NIL)
return;
L->top--; /* remove nil result */
}
}
static void luaB_foreach (lua_State *L) {
const Hash *a = gettable(L, 1);
lua_Object f = luaL_functionarg(L, 2);
int i;
luaD_checkstack(L, 3); /* for f, key, and val */
for (i=0; i<a->size; i++) {
const Node *nd = &(a->node[i]);
if (ttype(val(nd)) != TAG_NIL) {
*(L->top++) = *f;
*(L->top++) = *key(nd);
*(L->top++) = *val(nd);
luaD_call(L, L->top-3, 1);
if (ttype(L->top-1) != TAG_NIL)
return;
L->top--; /* remove result */
}
}
}
#define num_deprecated 4
static const struct luaL_reg deprecated_global_funcs[num_deprecated] = {
{"foreachvar", luaB_foreach},
{"nextvar", luaB_next},
{"rawgetglobal", luaB_rawgettable},
{"rawsetglobal", luaB_rawsettable}
{"rawgetglobal", luaB_rawget},
{"rawsetglobal", luaB_rawset}
};
static const struct luaL_reg other_deprecated_global_funcs[] = {
{"foreach", luaB_foreach},
{"foreachi", luaB_foreachi},
{"rawgettable", luaB_rawget},
{"rawsettable", luaB_rawset}
};
@ -591,8 +605,38 @@ static void deprecated_funcs (lua_State *L) {
lua_pushcclosure(L, deprecated_global_funcs[i].func, 1);
lua_setglobal(L, deprecated_global_funcs[i].name);
}
luaL_openl(L, other_deprecated_global_funcs);
}
#else
/*
** gives an explicit error in any attempt to call an obsolet function
*/
static void obsolete_func (lua_State *L) {
luaL_verror(L, "function `%.20s' is obsolete", luaL_check_string(L, 1));
}
#define num_deprecated 8
static const char *const obsolete_names [num_deprecated] = {
"foreach", "foreachi", "foreachvar", "nextvar", "rawgetglobal",
"rawgettable", "rawsetglobal", "rawsettable"
};
static void deprecated_funcs (lua_State *L) {
int i;
for (i=0; i<num_deprecated; i++) {
lua_pushstring(L, obsolete_names[i]);
lua_pushcclosure(L, obsolete_func, 1);
lua_setglobal(L, obsolete_names[i]);
}
}
#endif
/* }====================================================== */
static const struct luaL_reg builtin_funcs[] = {
@ -610,8 +654,8 @@ static const struct luaL_reg builtin_funcs[] = {
{"newtag", luaB_newtag},
{"next", luaB_next},
{"print", luaB_print},
{"rawgettable", luaB_rawgettable},
{"rawsettable", luaB_rawsettable},
{"rawget", luaB_rawget},
{"rawset", luaB_rawset},
{"setglobal", luaB_setglobal},
{"settag", luaB_settag},
{"settagmethod", luaB_settagmethod},
@ -621,8 +665,6 @@ static const struct luaL_reg builtin_funcs[] = {
{"type", luaB_type},
/* "Extra" functions */
{"assert", luaB_assert},
{"foreach", luaB_foreach},
{"foreachi", luaB_foreachi},
{"getn", luaB_getn},
{"sort", luaB_sort},
{"tinsert", luaB_tinsert},

View File

@ -1,5 +1,5 @@
/*
** $Id: lbuiltin.h,v 1.7 2000/04/17 19:23:12 roberto Exp roberto $
** $Id: lbuiltin.h,v 1.8 2000/05/08 19:32:53 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/
@ -18,8 +18,6 @@ void luaB_copytagmethods (lua_State *L);
void luaB_dofile (lua_State *L);
void luaB_dostring (lua_State *L);
void luaB_error (lua_State *L);
void luaB_foreach (lua_State *L);
void luaB_foreachi (lua_State *L);
void luaB_getglobal (lua_State *L);
void luaB_getn (lua_State *L);
void luaB_gettagmethod (lua_State *L);
@ -27,8 +25,8 @@ void luaB_globals (lua_State *L);
void luaB_newtag (lua_State *L);
void luaB_next (lua_State *L);
void luaB_print (lua_State *L);
void luaB_rawgettable (lua_State *L);
void luaB_rawsettable (lua_State *L);
void luaB_rawget (lua_State *L);
void luaB_rawset (lua_State *L);
void luaB_setglobal (lua_State *L);
void luaB_settag (lua_State *L);
void luaB_settagmethod (lua_State *L);

View File

@ -1,5 +1,5 @@
/*
** $Id: liolib.c,v 1.63 2000/05/09 14:50:16 roberto Exp roberto $
** $Id: liolib.c,v 1.64 2000/05/24 13:54:49 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@ -73,8 +73,10 @@ static void atribTM (lua_State *L) {
ctrl->file[inout] = (FILE *)lua_getuserdata(L, newvalue);
}
/* set the actual variable */
lua_pushglobaltable(L);
lua_pushstring(L, varname);
lua_pushobject(L, newvalue);
lua_rawsetglobal(L, varname);
lua_rawset(L);
}
@ -541,7 +543,7 @@ static void errorfb (lua_State *L) {
char buff[MAXMESSAGE];
int level = 1; /* skip level 0 (it's this function) */
lua_Debug ar;
lua_Object alertfunc = lua_rawgetglobal(L, LUA_ALERT);
lua_Object alertfunc;
sprintf(buff, "error: %.200s\n", lua_getstring(L, lua_getparam(L, 1)));
while (lua_getstack(L, level++, &ar)) {
char buffchunk[60];
@ -580,6 +582,9 @@ static void errorfb (lua_State *L) {
sprintf(buff+strlen(buff), " [%.70s]", buffchunk);
strcat(buff, "\n");
}
lua_pushglobaltable(L);
lua_pushstring(L, LUA_ALERT);
alertfunc = lua_rawget(L);
if (lua_isfunction(L, alertfunc)) { /* avoid loop if _ALERT is not defined */
lua_pushstring(L, buff);
lua_callfunction(L, alertfunc);

View File

@ -1,5 +1,5 @@
/*
** $Id: ltests.c,v 1.19 2000/05/15 19:48:04 roberto Exp roberto $
** $Id: ltests.c,v 1.20 2000/05/22 18:44:46 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
@ -246,10 +246,6 @@ static void testC (lua_State *L) {
int n = getreg(L, &pc);
reg[n] = lua_getglobal(L, getname(&pc));
}
else if EQ("rawgetglobal") {
int n = getreg(L, &pc);
reg[n] = lua_rawgetglobal(L, getname(&pc));
}
else if EQ("ref") {
lua_pushnumber(L, lua_ref(L, 0));
reg[getreg(L, &pc)] = lua_pop(L);
@ -276,8 +272,8 @@ static void testC (lua_State *L) {
else if EQ("setglobal") {
lua_setglobal(L, getname(&pc));
}
else if EQ("rawsetglobal") {
lua_rawsetglobal(L, getname(&pc));
else if EQ("pushglobals") {
lua_pushglobaltable(L);
}
else if EQ("pushstring") {
lua_pushstring(L, getname(&pc));
@ -291,14 +287,14 @@ static void testC (lua_State *L) {
else if EQ("gettable") {
reg[getreg(L, &pc)] = lua_gettable(L);
}
else if EQ("rawgettable") {
reg[getreg(L, &pc)] = lua_rawgettable(L);
else if EQ("rawget") {
reg[getreg(L, &pc)] = lua_rawget(L);
}
else if EQ("settable") {
lua_settable(L);
}
else if EQ("rawsettable") {
lua_rawsettable(L);
else if EQ("rawset") {
lua_rawset(L);
}
else if EQ("tag") {
lua_pushnumber(L, lua_tag(L, reg[getreg(L, &pc)]));
@ -360,6 +356,15 @@ static void testC (lua_State *L) {
while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
lua_pushstring(L, lua_getstring(L1, temp));
}
#if LUA_DEPRECATETFUNCS
else if EQ("rawsetglobal") {
lua_rawsetglobal(L, getname(&pc));
}
else if EQ("rawgetglobal") {
int n = getreg(L, &pc);
reg[n] = lua_rawgetglobal(L, getname(&pc));
}
#endif
else luaL_verror(L, "unknown command in `testC': %.20s", inst);
}
}

39
lua.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.52 2000/05/10 16:35:18 roberto Exp roberto $
** $Id: lua.h,v 1.53 2000/05/24 13:54:49 roberto Exp roberto $
** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br
@ -101,13 +101,12 @@ lua_Object lua_pop (lua_State *L);
lua_Object lua_getglobal (lua_State *L, const char *name);
void lua_setglobal (lua_State *L, const char *name); /* In: value */
lua_Object lua_rawgetglobal (lua_State *L, const char *name);
void lua_rawsetglobal (lua_State *L, const char *name);/* In: value */
void lua_settable (lua_State *L); /* In: table, index, value */
void lua_rawsettable (lua_State *L); /* In: table, index, value */
lua_Object lua_gettable (lua_State *L); /* In: table, index */
lua_Object lua_rawgettable (lua_State *L); /* In: table, index */
void lua_rawset (lua_State *L); /* In: table, index, value */
lua_Object lua_rawget (lua_State *L); /* In: table, index */
int lua_tag (lua_State *L, lua_Object obj);
@ -156,7 +155,7 @@ long lua_collectgarbage (lua_State *L, long limit);
#ifndef LUA_REENTRANT
/*
** ===============================================================
** {==============================================================
** Macros for single-state use
** ===============================================================
*/
@ -205,12 +204,10 @@ extern lua_State *lua_state;
#define lua_pop() (lua_pop)(lua_state)
#define lua_getglobal(name) (lua_getglobal)(lua_state, name)
#define lua_setglobal(name) (lua_setglobal)(lua_state, name)
#define lua_rawgetglobal(name) (lua_rawgetglobal)(lua_state, name)
#define lua_rawsetglobal(name) (lua_rawsetglobal)(lua_state, name)
#define lua_settable() (lua_settable)(lua_state)
#define lua_rawsettable() (lua_rawsettable)(lua_state)
#define lua_gettable() (lua_gettable)(lua_state)
#define lua_rawgettable() (lua_rawgettable)(lua_state)
#define lua_rawset() (lua_rawset)(lua_state)
#define lua_rawget() (lua_rawget)(lua_state)
#define lua_tag(obj) (lua_tag)(lua_state, obj)
#define lua_next(o,i) (lua_next)(lua_state, o,i)
#define lua_ref(lock) (lua_ref)(lua_state, lock)
@ -225,6 +222,27 @@ extern lua_State *lua_state;
#define lua_pushcclosure(fn,n) \
(lua_pushcclosure)(lua_state, (lua_CFunction)(fn), n)
/*
** }==============================================================
*/
#endif
/*
** compatibility with 3.2
** these functions are only available when Lua is compiled with
** the option LUA_DEPRECATETFUNCS
*/
#define lua_rawsettable lua_rawset
#define lua_rawgettable lua_rawget
lua_Object lua_rawgetglobal (lua_State *L, const char *name);
void lua_rawsetglobal (lua_State *L, const char *name);/* In: value */
#ifndef LUA_REENTRANT
#define lua_rawgetglobal(name) (lua_rawgetglobal(lua_state, name))
#define lua_rawsetglobal(name) (lua_rawsetglobal(lua_state, name))
#endif
@ -232,7 +250,6 @@ extern lua_State *lua_state;
/******************************************************************************
* Copyright (C) 1994-2000 TeCGraf, PUC-Rio. All rights reserved.
*