From 9a21e81907e49b79ec44677660acf9e35ad308bb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 31 Aug 2000 18:01:43 -0300 Subject: more builtin functions using official API --- lapi.c | 48 +++++++++++++++++-- lbuiltin.c | 159 +++++++++++++++++++++++++++---------------------------------- lua.h | 4 +- 3 files changed, 115 insertions(+), 96 deletions(-) diff --git a/lapi.c b/lapi.c index 5364c040..fb22b3df 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.91 2000/08/31 14:08:27 roberto Exp roberto $ +** $Id: lapi.c,v 1.92 2000/08/31 20:23:40 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -167,6 +167,23 @@ void *lua_touserdata (lua_State *L, int index) { access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value); } +const void *lua_topointer (lua_State *L, int index) { + const TObject *o = Index(L, index); + switch (ttype(o)) { + case TAG_NUMBER: case TAG_NIL: + return NULL; + case TAG_STRING: + return tsvalue(o)->str; + case TAG_USERDATA: + return tsvalue(o)->u.d.value; + case TAG_TABLE: + return hvalue(o); + case TAG_CCLOSURE: case TAG_LCLOSURE: + return clvalue(o); + default: return NULL; + } +} + /* @@ -236,7 +253,7 @@ void lua_gettable (lua_State *L) { void lua_rawget (lua_State *L) { - LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "not a table"); + LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "table expected"); *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1); L->top--; } @@ -295,7 +312,7 @@ void lua_settable (lua_State *L) { void lua_rawset (lua_State *L) { - LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "not a table"); + LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "table expected"); *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1); L->top -= 3; } @@ -303,7 +320,7 @@ void lua_rawset (lua_State *L) { void lua_setglobals (lua_State *L) { TObject *newtable = --L->top; - LUA_ASSERT(ttype(newtable) == TAG_TABLE, "not a table"); + LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); L->gt = hvalue(newtable); } @@ -375,7 +392,7 @@ void lua_unref (lua_State *L, int ref) { int lua_next (lua_State *L) { const TObject *t = Index(L, -2); Node *n; - LUA_ASSERT(ttype(t) == TAG_TABLE, "object is not a table in `lua_next'"); + LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); n = luaH_next(L, hvalue(t), Index(L, -1)); if (n) { *(L->top-1) = *key(n); @@ -389,3 +406,24 @@ int lua_next (lua_State *L) { } } + +int lua_getn (lua_State *L, int index) { + Hash *h = hvalue(Index(L, index)); + const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ + if (ttype(value) == TAG_NUMBER) + return (int)nvalue(value); + else { + Number max = 0; + int i = h->size; + Node *n = h->node; + while (i--) { + if (ttype(key(n)) == TAG_NUMBER && + ttype(val(n)) != TAG_NIL && + nvalue(key(n)) > max) + max = nvalue(key(n)); + n++; + } + return (int)max; + } +} + diff --git a/lbuiltin.c b/lbuiltin.c index 348214e4..4f75a5bc 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.126 2000/08/31 16:52:06 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.127 2000/08/31 20:23:40 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -270,71 +270,15 @@ int luaB_dofile (lua_State *L) { return passresults(L, lua_dofile(L, fname), oldtop); } -/* }====================================================== */ - - -/* -** {====================================================== -** Functions that could use only the official API but -** do not, for efficiency. -** ======================================================= -*/ - -#include "lapi.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - -/* -** {====================================================== -** Auxiliary functions -** ======================================================= -*/ - -static Number getsize (const Hash *h) { - Number max = 0; - int i = h->size; - Node *n = h->node; - while (i--) { - if (ttype(key(n)) == TAG_NUMBER && - ttype(val(n)) != TAG_NIL && - nvalue(key(n)) > max) - max = nvalue(key(n)); - n++; - } - return max; -} - - -static Number getnarg (lua_State *L, const Hash *a) { - const TObject *value = luaH_getstr(a, luaS_new(L, "n")); /* value = a.n */ - return (ttype(value) == TAG_NUMBER) ? nvalue(value) : getsize(a); -} - - -static Hash *gettable (lua_State *L, int arg) { - luaL_checktype(L, arg, "table"); - return hvalue(luaA_index(L, arg)); -} - -/* }====================================================== */ - - - int luaB_call (lua_State *L) { int oldtop; - const Hash *arg = gettable(L, 2); const char *options = luaL_opt_string(L, 3, ""); int err = 0; /* index of old error method */ - int n = (int)getnarg(L, arg); int i, status; + int n; + luaL_checktype(L, 2, "table"); + n = lua_getn(L, 2); if (!lua_isnull(L, 4)) { /* set new error method */ lua_getglobal(L, LUA_ERRORMESSAGE); err = lua_gettop(L); /* get index */ @@ -345,9 +289,12 @@ int luaB_call (lua_State *L) { /* push function */ lua_pushobject(L, 1); /* push arg[1...n] */ - luaD_checkstack(L, n); - for (i=0; itop++) = *luaH_getnum(arg, i+1); + luaL_checkstack(L, n, "too many arguments"); + for (i=0; i