lua_Object is a pointer to the stack (because now the stack doen't move)

This commit is contained in:
Roberto Ierusalimschy 1999-12-02 14:24:45 -02:00
parent fe237ad808
commit 8223ff473f
7 changed files with 80 additions and 84 deletions

102
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.60 1999/11/29 19:31:29 roberto Exp roberto $
** $Id: lapi.c,v 1.61 1999/12/01 19:50:08 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -31,11 +31,6 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
TObject *luaA_Address (lua_State *L, lua_Object o) {
return (o != LUA_NOOBJECT) ? Address(L, o) : NULL;
}
static lua_Type normalized_type (const TObject *o) {
int t = ttype(o);
switch (t) {
@ -71,14 +66,14 @@ static void checkCparams (lua_State *L, int nParams) {
static lua_Object put_luaObject (lua_State *L, const TObject *o) {
luaD_openstack(L, L->Cstack.base);
*L->Cstack.base++ = *o;
return Ref(L, L->Cstack.base-1);
return L->Cstack.base-1;
}
lua_Object luaA_putObjectOnTop (lua_State *L) {
luaD_openstack(L, L->Cstack.base);
*L->Cstack.base++ = *(--L->top);
return Ref(L, L->Cstack.base-1);
return L->Cstack.base-1;
}
@ -102,7 +97,7 @@ lua_Object lua_pop (lua_State *L) {
*/
lua_Object lua_lua2C (lua_State *L, int number) {
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
return Ref(L, L->Cstack.lua2C+number-1);
return L->Cstack.lua2C+number-1;
}
@ -111,7 +106,7 @@ int lua_callfunction (lua_State *L, lua_Object function) {
return 1;
else {
luaD_openstack(L, L->Cstack.base);
set_normalized(L->Cstack.base, Address(L, function));
set_normalized(L->Cstack.base, function);
return luaD_protectedrun(L);
}
}
@ -205,27 +200,33 @@ void lua_rawsetglobal (lua_State *L, const char *name) {
const char *lua_type (lua_State *L, lua_Object o) {
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, Address(L, o));
UNUSED(L);
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, o);
}
int lua_isnil (lua_State *L, lua_Object o) {
return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_NIL);
UNUSED(L);
return (o != LUA_NOOBJECT) && (ttype(o) == LUA_T_NIL);
}
int lua_istable (lua_State *L, lua_Object o) {
return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_ARRAY);
UNUSED(L);
return (o != LUA_NOOBJECT) && (ttype(o) == LUA_T_ARRAY);
}
int lua_isuserdata (lua_State *L, lua_Object o) {
return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_USERDATA);
UNUSED(L);
return (o != LUA_NOOBJECT) && (ttype(o) == LUA_T_USERDATA);
}
int lua_iscfunction (lua_State *L, lua_Object o) {
UNUSED(L);
return (lua_tag(L, o) == LUA_T_CPROTO);
}
int lua_isnumber (lua_State *L, lua_Object o) {
return (o != LUA_NOOBJECT) && (tonumber(Address(L, o)) == 0);
UNUSED(L);
return (o != LUA_NOOBJECT) && (tonumber(o) == 0);
}
int lua_isstring (lua_State *L, lua_Object o) {
@ -239,41 +240,44 @@ int lua_isfunction (lua_State *L, lua_Object o) {
}
int lua_equal(lua_State *L, lua_Object o1, lua_Object o2) {
UNUSED(L);
if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT) return (o1 == o2);
else return luaO_equalObj(Address(L, o1), Address(L, o2));
else return luaO_equalObj(o1, o2);
}
double lua_getnumber (lua_State *L, lua_Object obj) {
if (obj == LUA_NOOBJECT) return 0.0;
if (tonumber(Address(L, obj))) return 0.0;
else return (nvalue(Address(L, obj)));
UNUSED(L);
if (obj == LUA_NOOBJECT) return 0.0;
if (tonumber(obj)) return 0.0;
else return (nvalue(obj));
}
const char *lua_getstring (lua_State *L, lua_Object obj) {
luaC_checkGC(L); /* `tostring' may create a new string */
if (obj == LUA_NOOBJECT || tostring(L, Address(L, obj)))
if (obj == LUA_NOOBJECT || tostring(L, obj))
return NULL;
else return (svalue(Address(L, obj)));
else return (svalue(obj));
}
long lua_strlen (lua_State *L, lua_Object obj) {
luaC_checkGC(L); /* `tostring' may create a new string */
if (obj == LUA_NOOBJECT || tostring(L, Address(L, obj)))
UNUSED(L);
if (obj == LUA_NOOBJECT || tostring(L, obj))
return 0L;
else return (tsvalue(Address(L, obj))->u.s.len);
else return (tsvalue(obj)->u.s.len);
}
void *lua_getuserdata (lua_State *L, lua_Object obj) {
if (obj == LUA_NOOBJECT || ttype(Address(L, obj)) != LUA_T_USERDATA)
UNUSED(L);
if (obj == LUA_NOOBJECT || ttype(obj) != LUA_T_USERDATA)
return NULL;
else return tsvalue(Address(L, obj))->u.d.value;
else return tsvalue(obj)->u.d.value;
}
lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
if (!lua_iscfunction(L, obj))
return NULL;
else return fvalue(luaA_protovalue(Address(L, obj)));
else return fvalue(luaA_protovalue(obj));
}
@ -330,16 +334,16 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
void lua_pushobject (lua_State *L, lua_Object o) {
if (o == LUA_NOOBJECT)
lua_error(L, "API error - attempt to push a NOOBJECT");
set_normalized(L->top, Address(L, o));
set_normalized(L->top, o);
incr_top;
}
int lua_tag (lua_State *L, lua_Object lo) {
if (lo == LUA_NOOBJECT)
int lua_tag (lua_State *L, lua_Object o) {
UNUSED(L);
if (o == LUA_NOOBJECT)
return LUA_T_NIL;
else {
const TObject *o = Address(L, lo);
int t;
switch (t = ttype(o)) {
case LUA_T_USERDATA:
@ -429,8 +433,7 @@ int luaA_next (lua_State *L, const Hash *t, int i) {
}
int lua_next (lua_State *L, lua_Object o, int i) {
const TObject *t = Address(L, o);
int lua_next (lua_State *L, lua_Object t, int i) {
if (ttype(t) != LUA_T_ARRAY)
lua_error(L, "API error - object is not a table in `lua_next'");
i = luaA_next(L, avalue(t), i);
@ -482,33 +485,31 @@ lua_Function lua_stackedfunction (lua_State *L, int level) {
int t = L->stack[i].ttype;
if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
if (level-- == 0)
return Ref(L, L->stack+i);
return L->stack+i;
}
return LUA_NOOBJECT;
}
int lua_nups (lua_State *L, lua_Function func) {
const TObject *o = luaA_Address(L, func);
return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
int lua_nups (lua_State *L, lua_Function f) {
UNUSED(L);
return (!f || normalized_type(f) != LUA_T_CLOSURE) ? 0 : f->value.cl->nelems;
}
int lua_currentline (lua_State *L, lua_Function func) {
const TObject *f = Address(L, func);
int lua_currentline (lua_State *L, lua_Function f) {
return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
}
lua_Object lua_getlocal (lua_State *L, lua_Function func, int local_number,
lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
const char **name) {
/* check whether func is a Lua function */
if (lua_tag(L, func) != LUA_T_PROTO)
/* check whether `f' is a Lua function */
if (lua_tag(L, f) != LUA_T_PROTO)
return LUA_NOOBJECT;
else {
TObject *f = Address(L, func);
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
*name = luaF_getlocalname(fp, local_number, lua_currentline(L, func));
*name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
if (*name) {
/* if "*name", there must be a LUA_T_LINE */
/* therefore, f+2 points to function base */
@ -520,15 +521,14 @@ lua_Object lua_getlocal (lua_State *L, lua_Function func, int local_number,
}
int lua_setlocal (lua_State *L, lua_Function func, int local_number) {
/* check whether func is a Lua function */
if (lua_tag(L, func) != LUA_T_PROTO)
int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
/* check whether `f' is a Lua function */
if (lua_tag(L, f) != LUA_T_PROTO)
return 0;
else {
TObject *f = Address(L, func);
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
const char *name = luaF_getlocalname(fp, local_number,
lua_currentline(L, func));
lua_currentline(L, f));
checkCparams(L, 1);
--L->top;
if (name) {
@ -548,7 +548,7 @@ void lua_funcinfo (lua_State *L, lua_Object func,
if (!lua_isfunction(L, func))
lua_error(L, "API error - `funcinfo' called with a non-function value");
else {
const TObject *f = luaA_protovalue(Address(L, func));
const TObject *f = luaA_protovalue(func);
if (normalized_type(f) == LUA_T_PROTO) {
*source = tfvalue(f)->source->str;
*linedefined = tfvalue(f)->lineDefined;
@ -569,7 +569,7 @@ static int checkfunc (lua_State *L, TObject *o) {
const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
/* try to find a name for given function */
GlobalVar *g;
set_normalized(L->top, Address(L, o)); /* to be used by `checkfunc' */
set_normalized(L->top, o); /* to be used by `checkfunc' */
for (g=L->rootglobal; g; g=g->next) {
if (checkfunc(L, &g->value)) {
*name = g->name->str;

3
lapi.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.h,v 1.8 1999/11/04 17:22:26 roberto Exp roberto $
** $Id: lapi.h,v 1.9 1999/11/22 13:12:07 roberto Exp roberto $
** Auxiliary functions from Lua API
** See Copyright Notice in lua.h
*/
@ -11,7 +11,6 @@
#include "lobject.h"
TObject *luaA_Address (lua_State *L, lua_Object o);
void luaA_pushobject (lua_State *L, const TObject *o);
GlobalVar *luaA_nextvar (lua_State *L, TaggedString *g);
int luaA_next (lua_State *L, const Hash *t, int i);

View File

@ -1,5 +1,5 @@
/*
** $Id: lbuiltin.c,v 1.78 1999/11/30 13:06:50 roberto Exp roberto $
** $Id: lbuiltin.c,v 1.79 1999/12/01 19:50:08 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/
@ -70,7 +70,7 @@ static real getnarg (lua_State *L, const Hash *a) {
static Hash *gettable (lua_State *L, int arg) {
return avalue(luaA_Address(L, luaL_tablearg(L, arg)));
return avalue(luaL_tablearg(L, arg));
}
/* }====================================================== */
@ -329,7 +329,7 @@ static void luaB_call (lua_State *L) {
static void luaB_nextvar (lua_State *L) {
const TObject *o = luaA_Address(L, luaL_nonnullarg(L, 1));
lua_Object o = luaL_nonnullarg(L, 1);
TaggedString *g;
if (ttype(o) == LUA_T_NIL)
g = NULL;
@ -344,7 +344,7 @@ static void luaB_nextvar (lua_State *L) {
static void luaB_next (lua_State *L) {
const Hash *a = gettable(L, 1);
const TObject *k = luaA_Address(L, luaL_nonnullarg(L, 2));
lua_Object k = luaL_nonnullarg(L, 2);
int i; /* will get first element after `i' */
if (ttype(k) == LUA_T_NIL)
i = 0; /* get first */
@ -358,15 +358,14 @@ static void luaB_next (lua_State *L) {
static void luaB_tostring (lua_State *L) {
lua_Object obj = lua_getparam(L, 1);
const TObject *o = luaA_Address(L, obj);
lua_Object o = lua_getparam(L, 1);
char buff[64];
switch (ttype(o)) {
case LUA_T_NUMBER:
lua_pushstring(L, lua_getstring(L, obj));
lua_pushstring(L, lua_getstring(L, o));
return;
case LUA_T_STRING:
lua_pushobject(L, obj);
lua_pushobject(L, o);
return;
case LUA_T_ARRAY:
sprintf(buff, "table: %p", o->value.a);
@ -417,7 +416,7 @@ static void luaB_foreachi (lua_State *L) {
const Hash *t = gettable(L, 1);
int n = (int)getnarg(L, t);
int i;
StkId f = luaA_Address(L, luaL_functionarg(L, 2));
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;
@ -433,7 +432,7 @@ static void luaB_foreachi (lua_State *L) {
static void luaB_foreach (lua_State *L) {
const Hash *a = gettable(L, 1);
StkId f = luaA_Address(L, luaL_functionarg(L, 2));
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++) {
@ -452,7 +451,7 @@ static void luaB_foreach (lua_State *L) {
static void luaB_foreachvar (lua_State *L) {
StkId f = luaA_Address(L, luaL_functionarg(L, 1));
lua_Object f = luaL_functionarg(L, 1);
GlobalVar *gv;
luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
for (gv = L->rootglobal; gv; gv = gv->next) {
@ -492,7 +491,7 @@ static void luaB_tinsert (lua_State *L) {
luaV_setn(L, a, n+1); /* a.n = n+1 */
for ( ;n>=pos; n--)
luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
luaH_setint(L, a, pos, luaA_Address(L, v)); /* a[pos] = v */
luaH_setint(L, a, pos, v); /* a[pos] = v */
}
@ -525,7 +524,7 @@ static int sort_comp (lua_State *L, lua_Object f, const TObject *a,
const TObject *b) {
/* notice: the caller (auxsort) must check stack space */
if (f != LUA_NOOBJECT) {
*(L->top) = *luaA_Address(L, f);
*(L->top) = *f;
*(L->top+1) = *a;
*(L->top+2) = *b;
L->top += 3;
@ -618,20 +617,20 @@ static void mem_query (lua_State *L) {
static void hash_query (lua_State *L) {
const TObject *o = luaA_Address(L, luaL_nonnullarg(L, 1));
lua_Object o = luaL_nonnullarg(L, 1);
if (lua_getparam(L, 2) == LUA_NOOBJECT) {
luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected");
lua_pushnumber(L, tsvalue(o)->hash);
}
else {
const Hash *t = avalue(luaA_Address(L, luaL_tablearg(L, 2)));
const Hash *t = avalue(luaL_tablearg(L, 2));
lua_pushnumber(L, luaH_mainposition(L, t, o) - t->node);
}
}
static void table_query (lua_State *L) {
const Hash *t = avalue(luaA_Address(L, luaL_tablearg(L, 1)));
const Hash *t = avalue(luaL_tablearg(L, 1));
int i = luaL_opt_int(L, 2, -1);
if (i == -1) {
lua_pushnumber(L, t->size);

6
ldo.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 1.53 1999/11/25 18:58:51 roberto Exp roberto $
** $Id: ldo.c,v 1.54 1999/12/01 19:50:08 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@ -121,8 +121,8 @@ void luaD_callHook (lua_State *L, StkId base, const TProtoFunc *tf,
else {
TObject *f = base-1;
if (tf)
(*L->callhook)(L, Ref(L, f), tf->source->str, tf->lineDefined);
else (*L->callhook)(L, Ref(L, f), "(C)", -1);
v 1.3 1997/10/16, f, tf->source->str, tf->lineDefined);
else (*L->callhook)(L, f, "(C)", -1);
}
L->top = old_top;
L->Cstack = oldCLS;

8
ldo.h
View File

@ -1,5 +1,5 @@
/*
** $Id: ldo.h,v 1.11 1999/11/25 18:58:51 roberto Exp roberto $
** $Id: ldo.h,v 1.12 1999/12/01 19:50:08 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@ -23,12 +23,6 @@
#define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
/* macros to convert from lua_Object to (TObject *) and back */
#define Address(L, lo) ((lo)+L->stack-1)
#define Ref(L, st) ((st)-L->stack+1)
void luaD_init (lua_State *L);
void luaD_adjusttop (lua_State *L, StkId base, int extra);
void luaD_openstack (lua_State *L, StkId pos);

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 1.37 1999/11/22 13:12:07 roberto Exp roberto $
** $Id: lobject.h,v 1.38 1999/11/26 18:59:20 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -26,6 +26,8 @@
#endif
#define UNUSED(x) (void)x /* to avoid warnings */
/*
** "real" is the type "number" of Lua
** GREP LUA_NUMBER to change that

10
lua.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.40 1999/11/29 19:11:36 roberto Exp roberto $
** $Id: lua.h,v 1.41 1999/11/29 19:31:29 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
@ -16,8 +16,6 @@
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
#define LUA_NOOBJECT 0
#define LUA_NOREF (-2)
#define LUA_REFNIL (-1)
@ -26,7 +24,11 @@
typedef struct lua_State lua_State;
typedef void (*lua_CFunction) ( /* lua_State *L */ );
typedef unsigned int lua_Object;
typedef struct TObject *lua_Object;
#define LUA_NOOBJECT ((lua_Object)0)
lua_State *lua_newstate (void);
void lua_close (lua_State *L);