Browse Source

better names for type-related functions

v5-2
Roberto Ierusalimschy 24 years ago
parent
commit
ae1cf64348
  1. 30
      lapi.c
  2. 10
      lauxlib.c
  3. 25
      lbaselib.c
  4. 14
      liolib.c
  5. 8
      ltests.c
  6. 7
      ltm.c
  7. 4
      ltm.h
  8. 51
      lua.h

30
lapi.c

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.146 2001/06/15 20:36:57 roberto Exp roberto $
** $Id: lapi.c,v 1.147 2001/06/26 13:20:45 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -134,22 +134,13 @@ LUA_API void lua_pushvalue (lua_State *L, int index) {
*/
LUA_API int lua_type (lua_State *L, int index) {
LUA_API int lua_rawtag (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL) ? LUA_TNONE : ttype(o);
}
LUA_API const l_char *lua_tag2name (lua_State *L, int tag) {
const l_char *s;
lua_lock(L);
s = (tag == LUA_TNONE) ? l_s("no value") : basictypename(G(L), tag);
lua_unlock(L);
return s;
}
LUA_API const l_char *lua_xtypename (lua_State *L, int index) {
LUA_API const l_char *lua_type (lua_State *L, int index) {
StkId o;
const l_char *type;
lua_lock(L);
@ -174,7 +165,7 @@ LUA_API int lua_isnumber (lua_State *L, int index) {
LUA_API int lua_isstring (lua_State *L, int index) {
int t = lua_type(L, index);
int t = lua_rawtag(L, index);
return (t == LUA_TSTRING || t == LUA_TNUMBER);
}
@ -587,7 +578,7 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
** miscellaneous functions
*/
LUA_API int lua_newxtype (lua_State *L, const l_char *name, int basictype) {
LUA_API int lua_newtype (lua_State *L, const l_char *name, int basictype) {
int tag;
lua_lock(L);
if (basictype != LUA_TNONE &&
@ -618,6 +609,15 @@ LUA_API int lua_name2tag (lua_State *L, const l_char *name) {
}
LUA_API const l_char *lua_tag2name (lua_State *L, int tag) {
const l_char *s;
lua_lock(L);
s = (tag == LUA_TNONE) ? l_s("no value") : typenamebytag(G(L), tag);
lua_unlock(L);
return s;
}
LUA_API void lua_settag (lua_State *L, int tag) {
int basictype;
lua_lock(L);
@ -627,7 +627,7 @@ LUA_API void lua_settag (lua_State *L, int tag) {
basictype = G(L)->TMtable[tag].basictype;
if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
luaO_verror(L, l_s("tag %d can only be used for type '%.20s'"), tag,
basictypename(G(L), basictype));
typenamebytag(G(L), basictype));
switch (ttype(L->top-1)) {
case LUA_TTABLE:
hvalue(L->top-1)->htag = tag;

10
lauxlib.c

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.49 2001/03/26 14:31:49 roberto Exp roberto $
** $Id: lauxlib.c,v 1.50 2001/04/23 16:35:45 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -44,7 +44,7 @@ LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) {
static void type_error (lua_State *L, int narg, const l_char *tname) {
l_char buff[80];
sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_xtypename(L,narg));
sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
luaL_argerror(L, narg, buff);
}
@ -61,20 +61,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const l_char *mes) {
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
if (lua_type(L, narg) != t)
if (lua_rawtag(L, narg) != t)
tag_error(L, narg, t);
}
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
if (lua_type(L, narg) == LUA_TNONE)
if (lua_rawtag(L, narg) == LUA_TNONE)
luaL_argerror(L, narg, l_s("value expected"));
}
LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
const l_char *name) {
if (strcmp(lua_xtypename(L, narg), name) != 0)
if (strcmp(lua_type(L, narg), name) != 0)
type_error(L, narg, name);
return lua_touserdata(L, narg);
}

25
lbaselib.c

@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.37 2001/06/06 18:00:19 roberto Exp roberto $
** $Id: lbaselib.c,v 1.38 2001/06/20 17:25:30 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@ -139,7 +139,7 @@ static int luaB_getglobal (lua_State *L) {
/* auxiliary function to get `tags' */
static int gettag (lua_State *L, int narg) {
switch (lua_type(L, narg)) {
switch (lua_rawtag(L, narg)) {
case LUA_TNUMBER:
return (int)lua_tonumber(L, narg);
case LUA_TSTRING: {
@ -162,7 +162,7 @@ static int luaB_tag (lua_State *L) {
return 1;
}
static int luaB_settag (lua_State *L) {
static int luaB_settype (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, 1); /* push table */
lua_settag(L, gettag(L, 2));
@ -194,7 +194,7 @@ static int luaB_weakmode (lua_State *L) {
static int luaB_newtype (lua_State *L) {
const l_char *name = luaL_opt_string(L, 1, NULL);
lua_pushnumber(L, lua_newxtype(L, name, LUA_TTABLE));
lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
return 1;
}
@ -267,14 +267,14 @@ static int luaB_collectgarbage (lua_State *L) {
static int luaB_type (lua_State *L) {
luaL_checkany(L, 1);
lua_pushstring(L, lua_tag2name(L, lua_type(L, 1)));
lua_pushstring(L, lua_type(L, 1));
return 1;
}
static int luaB_xtype (lua_State *L) {
static int luaB_rawtype (lua_State *L) {
luaL_checkany(L, 1);
lua_pushstring(L, lua_xtypename(L, 1));
lua_pushstring(L, lua_tag2name(L, lua_rawtag(L, 1)));
return 1;
}
@ -458,7 +458,7 @@ static int luaB_call (lua_State *L) {
static int luaB_tostring (lua_State *L) {
l_char buff[64];
switch (lua_type(L, 1)) {
switch (lua_rawtag(L, 1)) {
case LUA_TNUMBER:
lua_pushstring(L, lua_tostring(L, 1));
return 1;
@ -466,13 +466,13 @@ static int luaB_tostring (lua_State *L) {
lua_pushvalue(L, 1);
return 1;
case LUA_TTABLE:
sprintf(buff, l_s("%.40s: %p"), lua_xtypename(L, 1), lua_topointer(L, 1));
sprintf(buff, l_s("%.40s: %p"), lua_type(L, 1), lua_topointer(L, 1));
break;
case LUA_TFUNCTION:
sprintf(buff, l_s("function: %p"), lua_topointer(L, 1));
break;
case LUA_TUSERDATA: {
const l_char *t = lua_xtypename(L, 1);
const l_char *t = lua_type(L, 1);
if (strcmp(t, l_s("userdata")) == 0)
sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1),
lua_touserdata(L, 1));
@ -715,9 +715,11 @@ static const luaL_reg base_funcs[] = {
{l_s("rawset"), luaB_rawset},
{l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */
{l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */
{l_s("rawtype"), luaB_rawtype},
{l_s("require"), luaB_require},
{l_s("setglobal"), luaB_setglobal},
{l_s("settag"), luaB_settag},
{l_s("settag"), luaB_settype}, /* for compatibility 4.0 */
{l_s("settype"), luaB_settype},
{l_s("settagmethod"), luaB_settagmethod},
{l_s("tag"), luaB_tag},
{l_s("tonumber"), luaB_tonumber},
@ -729,7 +731,6 @@ static const luaL_reg base_funcs[] = {
{l_s("tinsert"), luaB_tinsert},
{l_s("tremove"), luaB_tremove},
{l_s("unpack"), luaB_unpack},
{l_s("xtype"), luaB_xtype},
{l_s("weakmode"), luaB_weakmode}
};

14
liolib.c

@ -1,5 +1,5 @@
/*
** $Id: liolib.c,v 1.117 2001/06/28 14:45:44 roberto Exp roberto $
** $Id: liolib.c,v 1.118 2001/07/12 14:59:14 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@ -77,14 +77,14 @@ static int pushresult (lua_State *L, int i) {
*/
#define checkfile(L,f) (strcmp(lua_xtypename(L,(f)), FILEHANDLE) == 0)
#define checkfile(L,f) (strcmp(lua_type(L,(f)), FILEHANDLE) == 0)
static FILE *getopthandle (lua_State *L, int inout) {
FILE *p = (FILE *)lua_touserdata(L, 1);
if (p != NULL) { /* is it a userdata ? */
if (!checkfile(L, 1)) { /* not a valid file handle? */
if (strcmp(lua_xtypename(L, 1), CLOSEDFILEHANDLE) == 0)
if (strcmp(lua_type(L, 1), CLOSEDFILEHANDLE) == 0)
luaL_argerror(L, 1, l_s("file is closed"));
else
luaL_argerror(L, 1, l_s("(invalid value)"));
@ -310,7 +310,7 @@ static int io_read (lua_State *L) {
luaL_checkstack(L, nargs+LUA_MINSTACK, l_s("too many arguments"));
success = 1;
for (n = 1; n<=nargs && success; n++) {
if (lua_type(L, n) == LUA_TNUMBER) {
if (lua_rawtag(L, n) == LUA_TNUMBER) {
size_t l = (size_t)lua_tonumber(L, n);
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
}
@ -362,7 +362,7 @@ static int io_write (lua_State *L) {
int arg;
int status = 1;
for (arg=1; arg<=nargs; arg++) {
if (lua_type(L, arg) == LUA_TNUMBER) {
if (lua_rawtag(L, arg) == LUA_TNUMBER) {
/* optimization: could be done exactly as for strings */
status = status &&
fprintf(f, l_s(LUA_NUMBER_FMT), lua_tonumber(L, arg)) > 0;
@ -686,8 +686,8 @@ static const luaL_reg iolib[] = {
LUALIB_API int lua_iolibopen (lua_State *L) {
int iotag = lua_newxtype(L, FILEHANDLE, LUA_TUSERDATA);
lua_newxtype(L, CLOSEDFILEHANDLE, LUA_TUSERDATA);
int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA);
lua_newtype(L, CLOSEDFILEHANDLE, LUA_TUSERDATA);
luaL_openl(L, iolib);
/* predefined file handles */
newfilewithname(L, stdin, basicfiles[INFILE]);

8
ltests.c

@ -1,5 +1,5 @@
/*
** $Id: ltests.c,v 1.86 2001/06/28 19:58:57 roberto Exp roberto $
** $Id: ltests.c,v 1.87 2001/07/05 20:31:14 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
@ -367,8 +367,8 @@ static int udataval (lua_State *L) {
}
static int newtag (lua_State *L) {
lua_pushnumber(L, lua_newxtype(L, lua_tostring(L, 1),
(int)lua_tonumber(L, 2)));
lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1),
(int)lua_tonumber(L, 2)));
return 1;
}
@ -619,7 +619,7 @@ static int testC (lua_State *L) {
lua_gettagmethod(L, tag, event);
}
else if EQ(l_s("type")) {
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
lua_pushstring(L, lua_type(L, getnum));
}
else luaL_verror(L, l_s("unknown instruction %.30s"), buff);
}

7
ltm.c

@ -1,5 +1,5 @@
/*
** $Id: ltm.c,v 1.72 2001/06/06 18:00:19 roberto Exp roberto $
** $Id: ltm.c,v 1.73 2001/06/15 20:36:57 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@ -70,7 +70,8 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */
void luaT_init (lua_State *L) {
static const l_char *const typenames[NUM_TAGS] = {
l_s("userdata"), l_s("nil"), l_s("number"), l_s("string"), l_s("table"), l_s("function")
l_s("userdata"), l_s("nil"), l_s("number"), l_s("string"),
l_s("table"), l_s("function")
};
int i;
for (i=0; i<NUM_TAGS; i++)
@ -177,7 +178,7 @@ LUA_API void lua_settagmethod (lua_State *L, int t, const l_char *event) {
checktag(L, t);
if (!luaT_validevent(t, e))
luaO_verror(L, l_s("cannot change `%.20s' tag method for type `%.20s'%.20s"),
luaT_eventname[e], basictypename(G(L), t),
luaT_eventname[e], typenamebytag(G(L), t),
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
l_s(" with default tag") : l_s(""));
switch (ttype(L->top - 1)) {

4
ltm.h

@ -1,5 +1,5 @@
/*
** $Id: ltm.h,v 1.24 2001/02/23 17:17:25 roberto Exp roberto $
** $Id: ltm.h,v 1.25 2001/06/06 18:00:19 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@ -62,7 +62,7 @@ struct TM {
#define luaT_gettm(G,tag,event) (G->TMtable[tag].method[event])
#define luaT_gettmbyObj(G,o,e) (luaT_gettm((G),luaT_tag(o),(e)))
#define basictypename(G, t) getstr(G->TMtable[t].name)
#define typenamebytag(G, t) getstr(G->TMtable[t].name)
#define validtag(G,t) (NUM_TAGS <= (t) && (t) < G->ntag)

51
lua.h

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.99 2001/06/28 14:45:44 roberto Exp roberto $
** $Id: lua.h,v 1.100 2001/07/05 19:32:42 roberto Exp roberto $
** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org
@ -51,10 +51,16 @@ typedef struct lua_State lua_State;
typedef int (*lua_CFunction) (lua_State *L);
/*
** an invalid `tag'
*/
#define LUA_NOTAG (-1)
/*
** types returned by `lua_type'
** tags for basic types
*/
#define LUA_TNONE (-1)
#define LUA_TNONE LUA_NOTAG
#define LUA_TUSERDATA 0
#define LUA_TNIL 1
@ -63,11 +69,6 @@ typedef int (*lua_CFunction) (lua_State *L);
#define LUA_TTABLE 4
#define LUA_TFUNCTION 5
/*
** an invalid `tag'
*/
#define LUA_NOTAG (-2)
/* minimum Lua stack available to a C function */
#define LUA_MINSTACK 20
@ -122,13 +123,12 @@ LUA_API int lua_stackspace (lua_State *L);
** access functions (stack -> C)
*/
LUA_API int lua_type (lua_State *L, int index);
LUA_API const lua_char *lua_tag2name (lua_State *L, int tag);
LUA_API const lua_char *lua_xtypename (lua_State *L, int index);
LUA_API const lua_char *lua_type (lua_State *L, int index);
LUA_API int lua_isnumber (lua_State *L, int index);
LUA_API int lua_isstring (lua_State *L, int index);
LUA_API int lua_iscfunction (lua_State *L, int index);
LUA_API int lua_tag (lua_State *L, int index);
LUA_API int lua_rawtag (lua_State *L, int index);
LUA_API int lua_equal (lua_State *L, int index1, int index2);
LUA_API int lua_lessthan (lua_State *L, int index1, int index2);
@ -201,11 +201,13 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
/*
** miscellaneous functions
*/
LUA_API int lua_newxtype (lua_State *L, const lua_char *name, int basictype);
LUA_API int lua_name2tag (lua_State *L, const lua_char *name);
LUA_API int lua_newtype (lua_State *L, const lua_char *name, int basictype);
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
LUA_API void lua_settag (lua_State *L, int tag);
LUA_API int lua_name2tag (lua_State *L, const lua_char *name);
LUA_API const lua_char *lua_tag2name (lua_State *L, int tag);
LUA_API void lua_error (lua_State *L, const lua_char *s);
LUA_API void lua_unref (lua_State *L, int ref);
@ -219,14 +221,9 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
LUA_API void lua_newuserdatabox (lua_State *L, void *u);
LUA_API void lua_setweakmode (lua_State *L, int mode);
LUA_API int lua_getweakmode (lua_State *L, int index);
LUA_API int lua_getweakmode (lua_State *L, int index);
/*
** deprecated function
*/
LUA_API void lua_pushusertag (lua_State *L, void *u, int tag);
/*
** ===============================================================
@ -242,11 +239,11 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag);
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
#define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA)
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE)
#define lua_isfunction(L,n) (lua_rawtag(L,n) == LUA_TFUNCTION)
#define lua_istable(L,n) (lua_rawtag(L,n) == LUA_TTABLE)
#define lua_isuserdata(L,n) (lua_rawtag(L,n) == LUA_TUSERDATA)
#define lua_isnil(L,n) (lua_rawtag(L,n) == LUA_TNIL)
#define lua_isnull(L,n) (lua_rawtag(L,n) == LUA_TNONE)
#define lua_pushliteral(L, s) lua_pushlstring(L, s, \
(sizeof(s)/sizeof(lua_char))-1)
@ -256,7 +253,7 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag);
/*
** compatibility macros
*/
#define lua_newtag(L) lua_newxtype(L, NULL, LUA_TNONE)
#define lua_newtag(L) lua_newtype(L, NULL, LUA_TNONE)
#define lua_typename lua_tag2name
#endif
@ -294,8 +291,8 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag);
/*
** formats for Lua numbers
*/
#ifndef LUA_SCAN_NUMBER
#define LUA_SCAN_NUMBER "%lf"
#ifndef LUA_NUMBER_SCAN
#define LUA_NUMBER_SCAN "%lf"
#endif
#ifndef LUA_NUMBER_FMT
#define LUA_NUMBER_FMT "%.16g"

Loading…
Cancel
Save