aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-01-25 14:45:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-01-25 14:45:36 -0200
commita53d9b66ca6247818acaf41e28cdf123082a272b (patch)
tree8e909200d4d925fc7394e6adf83cc5941456c588
parentc8559e3c8d12e052783e2952db1372c68cc16059 (diff)
downloadlua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.gz
lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.bz2
lua-a53d9b66ca6247818acaf41e28cdf123082a272b.zip
first implementation for type names
-rw-r--r--lapi.c60
-rw-r--r--lauxlib.c28
-rw-r--r--lauxlib.h3
-rw-r--r--lbaselib.c53
-rw-r--r--ldebug.c8
-rw-r--r--lgc.c15
-rw-r--r--liolib.c241
-rw-r--r--lobject.c8
-rw-r--r--lobject.h6
-rw-r--r--lstate.c3
-rw-r--r--lstate.h3
-rw-r--r--ltable.c4
-rw-r--r--ltm.c76
-rw-r--r--ltm.h10
-rw-r--r--lua.h10
15 files changed, 295 insertions, 233 deletions
diff --git a/lapi.c b/lapi.c
index 0ca9bad6..d9229444 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.118 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: lapi.c,v 1.119 2001/01/24 15:45:33 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*/
@@ -132,16 +132,27 @@ LUA_API int lua_type (lua_State *L, int index) {
132 return i; 132 return i;
133} 133}
134 134
135
135LUA_API const char *lua_typename (lua_State *L, int t) { 136LUA_API const char *lua_typename (lua_State *L, int t) {
136 const char *s; 137 const char *s;
137 LUA_ENTRY; 138 LUA_ENTRY;
138 UNUSED(L); 139 s = (t == LUA_TNONE) ? "no value" : basictypename(G(L), t);
139 s = (t == LUA_TNONE) ? "no value" : luaO_typenames[t];
140 LUA_EXIT; 140 LUA_EXIT;
141 return s; 141 return s;
142} 142}
143 143
144 144
145LUA_API const char *lua_xtype (lua_State *L, int index) {
146 StkId o;
147 const char *type;
148 LUA_ENTRY;
149 o = luaA_indexAcceptable(L, index);
150 type = (o == NULL) ? "no value" : luaT_typename(G(L), o);
151 LUA_EXIT;
152 return type;
153}
154
155
145LUA_API int lua_iscfunction (lua_State *L, int index) { 156LUA_API int lua_iscfunction (lua_State *L, int index) {
146 StkId o; 157 StkId o;
147 int i; 158 int i;
@@ -553,9 +564,46 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
553** miscellaneous functions 564** miscellaneous functions
554*/ 565*/
555 566
556LUA_API void lua_settag (lua_State *L, int tag) { 567LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
568 int tag;
557 LUA_ENTRY; 569 LUA_ENTRY;
558 luaT_realtag(L, tag); 570 if (basictype != LUA_TNONE &&
571 basictype != LUA_TTABLE &&
572 basictype != LUA_TUSERDATA)
573 luaO_verror(L, "invalid basic type (%d) for new type", basictype);
574 tag = luaT_newtag(L, name, basictype);
575 if (tag == LUA_TNONE)
576 luaO_verror(L, "type name '%.30s' already exists", name);
577 LUA_EXIT;
578 return tag;
579}
580
581
582LUA_API int lua_type2tag (lua_State *L, const char *name) {
583 int tag;
584 const TObject *v;
585 LUA_ENTRY;
586 v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
587 if (ttype(v) == LUA_TNIL)
588 tag = LUA_TNONE;
589 else {
590 lua_assert(ttype(v) == LUA_TNUMBER);
591 tag = nvalue(v);
592 }
593 LUA_EXIT;
594 return tag;
595}
596
597
598LUA_API void lua_settag (lua_State *L, int tag) {
599 int basictype;
600 LUA_ENTRY;
601 if (tag < 0 || tag >= G(L)->ntag)
602 luaO_verror(L, "%d is not a valid tag", tag);
603 basictype = G(L)->TMtable[tag].basictype;
604 if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
605 luaO_verror(L, "tag %d can only be used for type '%.20s'", tag,
606 basictypename(G(L), basictype));
559 switch (ttype(L->top-1)) { 607 switch (ttype(L->top-1)) {
560 case LUA_TTABLE: 608 case LUA_TTABLE:
561 hvalue(L->top-1)->htag = tag; 609 hvalue(L->top-1)->htag = tag;
@@ -565,7 +613,7 @@ LUA_API void lua_settag (lua_State *L, int tag) {
565 break; 613 break;
566 default: 614 default:
567 luaO_verror(L, "cannot change the tag of a %.20s", 615 luaO_verror(L, "cannot change the tag of a %.20s",
568 luaO_typename(L->top-1)); 616 luaT_typename(G(L), L->top-1));
569 } 617 }
570 LUA_EXIT; 618 LUA_EXIT;
571} 619}
diff --git a/lauxlib.c b/lauxlib.c
index 5c9014f5..ab7609e6 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.43 2000/10/30 13:07:48 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.44 2000/12/04 18:33:40 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -40,14 +40,18 @@ LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
40} 40}
41 41
42 42
43static void type_error (lua_State *L, int narg, int t) { 43static void type_error (lua_State *L, int narg, const char *tname) {
44 char buff[50]; 44 char buff[80];
45 sprintf(buff, "%.8s expected, got %.8s", lua_typename(L, t), 45 sprintf(buff, "%.25s expected, got %.25s", tname, lua_xtype(L, narg));
46 lua_typename(L, lua_type(L, narg)));
47 luaL_argerror(L, narg, buff); 46 luaL_argerror(L, narg, buff);
48} 47}
49 48
50 49
50static void tag_error (lua_State *L, int narg, int tag) {
51 type_error(L, narg, lua_typename(L, tag));
52}
53
54
51LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { 55LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
52 if (space > lua_stackspace(L)) 56 if (space > lua_stackspace(L))
53 luaL_verror(L, "stack overflow (%.30s)", mes); 57 luaL_verror(L, "stack overflow (%.30s)", mes);
@@ -56,7 +60,7 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
56 60
57LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) { 61LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
58 if (lua_type(L, narg) != t) 62 if (lua_type(L, narg) != t)
59 type_error(L, narg, t); 63 tag_error(L, narg, t);
60} 64}
61 65
62 66
@@ -66,9 +70,17 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) {
66} 70}
67 71
68 72
73LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
74 const char *name) {
75 if (strcmp(lua_xtype(L, narg), name) != 0)
76 type_error(L, narg, name);
77 return lua_touserdata(L, narg);
78}
79
80
69LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { 81LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
70 const char *s = lua_tostring(L, narg); 82 const char *s = lua_tostring(L, narg);
71 if (!s) type_error(L, narg, LUA_TSTRING); 83 if (!s) tag_error(L, narg, LUA_TSTRING);
72 if (len) *len = lua_strlen(L, narg); 84 if (len) *len = lua_strlen(L, narg);
73 return s; 85 return s;
74} 86}
@@ -87,7 +99,7 @@ LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, s
87LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) { 99LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
88 lua_Number d = lua_tonumber(L, narg); 100 lua_Number d = lua_tonumber(L, narg);
89 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 101 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
90 type_error(L, narg, LUA_TNUMBER); 102 tag_error(L, narg, LUA_TNUMBER);
91 return d; 103 return d;
92} 104}
93 105
diff --git a/lauxlib.h b/lauxlib.h
index 286460ee..c7545ba3 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.30 2000/10/30 12:38:50 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.31 2000/12/04 18:33:40 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -36,6 +36,7 @@ LUALIB_API lua_Number luaL_opt_number (lua_State *L, int nArg, lua_Number def);
36LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg); 36LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg);
37LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); 37LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
38LUALIB_API void luaL_checkany (lua_State *L, int narg); 38LUALIB_API void luaL_checkany (lua_State *L, int narg);
39LUALIB_API void *luaL_check_userdata (lua_State *L, int narg, const char *name);
39 40
40LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...); 41LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...);
41LUALIB_API int luaL_findstring (const char *name, const char *const list[]); 42LUALIB_API int luaL_findstring (const char *name, const char *const list[]);
diff --git a/lbaselib.c b/lbaselib.c
index 3a2aea18..669e8e87 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.17 2000/11/06 13:45:18 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.18 2001/01/10 16:58:11 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -128,6 +128,26 @@ static int luaB_getglobal (lua_State *L) {
128 return 1; 128 return 1;
129} 129}
130 130
131
132/* auxiliary function to get `tags' */
133static int gettag (lua_State *L, int narg) {
134 switch (lua_type(L, narg)) {
135 case LUA_TNUMBER:
136 return (int)lua_tonumber(L, narg);
137 case LUA_TSTRING: {
138 const char *name = lua_tostring(L, narg);
139 int tag = lua_type2tag(L, name);
140 if (tag == LUA_TNONE)
141 luaL_verror(L, "'%.30s' is not a valid type name", name);
142 return tag;
143 }
144 default:
145 luaL_argerror(L, narg, "tag or type name expected");
146 return 0; /* to avoid warnings */
147 }
148}
149
150
131static int luaB_tag (lua_State *L) { 151static int luaB_tag (lua_State *L) {
132 luaL_checkany(L, 1); 152 luaL_checkany(L, 1);
133 lua_pushnumber(L, lua_tag(L, 1)); 153 lua_pushnumber(L, lua_tag(L, 1));
@@ -137,18 +157,18 @@ static int luaB_tag (lua_State *L) {
137static int luaB_settag (lua_State *L) { 157static int luaB_settag (lua_State *L) {
138 luaL_checktype(L, 1, LUA_TTABLE); 158 luaL_checktype(L, 1, LUA_TTABLE);
139 lua_pushvalue(L, 1); /* push table */ 159 lua_pushvalue(L, 1); /* push table */
140 lua_settag(L, luaL_check_int(L, 2)); 160 lua_settag(L, gettag(L, 2));
141 return 1; /* return table */ 161 return 1; /* return table */
142} 162}
143 163
144static int luaB_newtag (lua_State *L) { 164static int luaB_newtype (lua_State *L) {
145 lua_pushnumber(L, lua_newtag(L)); 165 const char *name = luaL_opt_string(L, 1, NULL);
166 lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
146 return 1; 167 return 1;
147} 168}
148 169
149static int luaB_copytagmethods (lua_State *L) { 170static int luaB_copytagmethods (lua_State *L) {
150 lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1), 171 lua_pushnumber(L, lua_copytagmethods(L, gettag(L, 1), gettag(L, 2)));
151 luaL_check_int(L, 2)));
152 return 1; 172 return 1;
153} 173}
154 174
@@ -178,7 +198,7 @@ static int luaB_rawset (lua_State *L) {
178} 198}
179 199
180static int luaB_settagmethod (lua_State *L) { 200static int luaB_settagmethod (lua_State *L) {
181 int tag = luaL_check_int(L, 1); 201 int tag = gettag(L, 1);
182 const char *event = luaL_check_string(L, 2); 202 const char *event = luaL_check_string(L, 2);
183 luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3, 203 luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
184 "function or nil expected"); 204 "function or nil expected");
@@ -192,7 +212,7 @@ static int luaB_settagmethod (lua_State *L) {
192 212
193 213
194static int luaB_gettagmethod (lua_State *L) { 214static int luaB_gettagmethod (lua_State *L) {
195 int tag = luaL_check_int(L, 1); 215 int tag = gettag(L, 1);
196 const char *event = luaL_check_string(L, 2); 216 const char *event = luaL_check_string(L, 2);
197 if (strcmp(event, "gc") == 0) 217 if (strcmp(event, "gc") == 0)
198 lua_error(L, "deprecated use: cannot get the `gc' tag method from Lua"); 218 lua_error(L, "deprecated use: cannot get the `gc' tag method from Lua");
@@ -221,6 +241,13 @@ static int luaB_type (lua_State *L) {
221} 241}
222 242
223 243
244static int luaB_xtype (lua_State *L) {
245 luaL_checkany(L, 1);
246 lua_pushstring(L, lua_xtype(L, 1));
247 return 1;
248}
249
250
224static int luaB_next (lua_State *L) { 251static int luaB_next (lua_State *L) {
225 luaL_checktype(L, 1, LUA_TTABLE); 252 luaL_checktype(L, 1, LUA_TTABLE);
226 lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 253 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
@@ -619,13 +646,14 @@ static const struct luaL_reg base_funcs[] = {
619 {"getglobal", luaB_getglobal}, 646 {"getglobal", luaB_getglobal},
620 {"gettagmethod", luaB_gettagmethod}, 647 {"gettagmethod", luaB_gettagmethod},
621 {"globals", luaB_globals}, 648 {"globals", luaB_globals},
622 {"newtag", luaB_newtag}, 649 {"newtype", luaB_newtype},
650 {"newtag", luaB_newtype}, /* for compatibility 4.0 */
623 {"next", luaB_next}, 651 {"next", luaB_next},
624 {"print", luaB_print}, 652 {"print", luaB_print},
625 {"rawget", luaB_rawget}, 653 {"rawget", luaB_rawget},
626 {"rawset", luaB_rawset}, 654 {"rawset", luaB_rawset},
627 {"rawgettable", luaB_rawget}, /* for compatibility */ 655 {"rawgettable", luaB_rawget}, /* for compatibility 3.2 */
628 {"rawsettable", luaB_rawset}, /* for compatibility */ 656 {"rawsettable", luaB_rawset}, /* for compatibility 3.2 */
629 {"setglobal", luaB_setglobal}, 657 {"setglobal", luaB_setglobal},
630 {"settag", luaB_settag}, 658 {"settag", luaB_settag},
631 {"settagmethod", luaB_settagmethod}, 659 {"settagmethod", luaB_settagmethod},
@@ -637,7 +665,8 @@ static const struct luaL_reg base_funcs[] = {
637 {"getn", luaB_getn}, 665 {"getn", luaB_getn},
638 {"sort", luaB_sort}, 666 {"sort", luaB_sort},
639 {"tinsert", luaB_tinsert}, 667 {"tinsert", luaB_tinsert},
640 {"tremove", luaB_tremove} 668 {"tremove", luaB_tremove},
669 {"xtype", luaB_xtype},
641}; 670};
642 671
643 672
diff --git a/ldebug.c b/ldebug.c
index 7d5a150d..ba43d6bc 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 1.54 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: ldebug.c,v 1.55 2001/01/24 15:45:33 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -466,7 +466,7 @@ static const char *getfuncname (lua_State *L, StkId f, const char **name) {
466void luaG_typeerror (lua_State *L, StkId o, const char *op) { 466void luaG_typeerror (lua_State *L, StkId o, const char *op) {
467 const char *name; 467 const char *name;
468 const char *kind = getobjname(L, o, &name); 468 const char *kind = getobjname(L, o, &name);
469 const char *t = luaO_typename(o); 469 const char *t = luaT_typename(G(L), o);
470 if (kind) 470 if (kind)
471 luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)", 471 luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
472 op, kind, name, t); 472 op, kind, name, t);
@@ -483,8 +483,8 @@ void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
483 483
484 484
485void luaG_ordererror (lua_State *L, StkId top) { 485void luaG_ordererror (lua_State *L, StkId top) {
486 const char *t1 = luaO_typename(top-2); 486 const char *t1 = luaT_typename(G(L), top-2);
487 const char *t2 = luaO_typename(top-1); 487 const char *t2 = luaT_typename(G(L), top-1);
488 if (t1[2] == t2[2]) 488 if (t1[2] == t2[2])
489 luaO_verror(L, "attempt to compare two %.10s values", t1); 489 luaO_verror(L, "attempt to compare two %.10s values", t1);
490 else 490 else
diff --git a/lgc.c b/lgc.c
index db713767..0c84da8f 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.77 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: lgc.c,v 1.78 2001/01/22 18:01:38 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -108,11 +108,13 @@ static void marklock (global_State *G, GCState *st) {
108 108
109 109
110static void marktagmethods (global_State *G, GCState *st) { 110static void marktagmethods (global_State *G, GCState *st) {
111 int e; 111 int t;
112 for (e=0; e<TM_N; e++) { 112 for (t=0; t<G->ntag; t++) {
113 int t; 113 struct TM *tm = &G->TMtable[t];
114 for (t=0; t<G->ntag; t++) { 114 int e;
115 Closure *cl = luaT_gettm(G, t, e); 115 if (tm->name) strmark(tm->name);
116 for (e=0; e<TM_N; e++) {
117 Closure *cl = tm->method[e];
116 if (cl) markclosure(st, cl); 118 if (cl) markclosure(st, cl);
117 } 119 }
118 } 120 }
@@ -126,6 +128,7 @@ static void markall (lua_State *L) {
126 marktagmethods(G(L), &st); /* mark tag methods */ 128 marktagmethods(G(L), &st); /* mark tag methods */
127 markstacks(L, &st); /* mark all stacks */ 129 markstacks(L, &st); /* mark all stacks */
128 marklock(G(L), &st); /* mark locked objects */ 130 marklock(G(L), &st); /* mark locked objects */
131 marktable(&st, G(L)->type2tag);
129 for (;;) { /* mark tables and closures */ 132 for (;;) { /* mark tables and closures */
130 if (st.cmark) { 133 if (st.cmark) {
131 int i; 134 int i;
diff --git a/liolib.c b/liolib.c
index 419cbf83..4e5f861a 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.98 2001/01/11 18:59:03 roberto Exp roberto $ 2** $Id: liolib.c,v 1.99 2001/01/18 15:59:09 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*/
@@ -32,7 +32,7 @@
32#define LC_MONETARY 0 32#define LC_MONETARY 0
33#define LC_NUMERIC 0 33#define LC_NUMERIC 0
34#define LC_TIME 0 34#define LC_TIME 0
35#define strerror(e) "generic I/O error" 35#define strerror(e) "I/O error"
36#define errno (-1) 36#define errno (-1)
37#endif 37#endif
38 38
@@ -53,13 +53,7 @@ int pclose(); */
53#define OUTFILE 1 53#define OUTFILE 1
54#define NOFILE 2 54#define NOFILE 2
55 55
56 56#define FILEHANDLE "FileHandle"
57typedef struct IOCtrl {
58 int ref[2]; /* ref for strings _INPUT/_OUTPUT */
59 int iotag; /* tag for file handles */
60 int closedtag; /* tag for closed handles */
61} IOCtrl;
62
63 57
64 58
65static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; 59static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
@@ -74,7 +68,7 @@ static int pushresult (lua_State *L, int i) {
74 lua_pushnil(L); 68 lua_pushnil(L);
75 lua_pushstring(L, strerror(errno)); 69 lua_pushstring(L, strerror(errno));
76 lua_pushnumber(L, errno); 70 lua_pushnumber(L, errno);
77 return 3;; 71 return 3;
78 } 72 }
79} 73}
80 74
@@ -86,83 +80,77 @@ static int pushresult (lua_State *L, int i) {
86*/ 80*/
87 81
88 82
89static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) { 83#define checkfile(L,f) (strcmp(lua_xtype(L,(f)), FILEHANDLE) == 0)
90 FILE *p = (FILE *)lua_touserdata(L, f);
91 if (p != NULL) { /* is `f' a userdata ? */
92 int ftag = lua_tag(L, f);
93 if (ftag == ctrl->iotag) /* does it have the correct tag? */
94 return p;
95 else if (ftag == ctrl->closedtag)
96 lua_error(L, "cannot access a closed file");
97 /* else go through */
98 }
99 return NULL;
100}
101 84
102 85
103static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) { 86static FILE *getopthandle (lua_State *L, int inout) {
104 FILE *f = gethandle(L, ctrl, arg); 87 FILE *p = (FILE *)lua_touserdata(L, 1);
105 luaL_arg_check(L, f, arg, "invalid file handle"); 88 if (p != NULL) { /* is it a userdata ? */
106 return f; 89 if (!checkfile(L,1)) {
90 if (strcmp(lua_xtype(L, 1), "ClosedFileHandle") == 0)
91 luaL_argerror(L, 1, "file is closed");
92 else
93 luaL_argerror(L, 1, "(invalid value)");
94 }
95 lua_remove(L, 1); /* remove it from stack */
96 }
97 else if (inout != NOFILE) { /* try global value */
98 lua_getglobal(L, filenames[inout]);
99 if (!checkfile(L,-1))
100 luaL_verror(L, "global variable `%.10s' is not a valid file handle",
101 filenames[inout]);
102 p = (FILE *)lua_touserdata(L, -1);
103 lua_pop(L, 1); /* remove global value from stack */
104 }
105 return p;
107} 106}
108 107
109 108
110static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) { 109static void pushfile (lua_State *L, FILE *f) {
111 FILE *f; 110 lua_pushusertag(L, f, lua_type2tag(L, FILEHANDLE));
112 lua_getglobals(L);
113 lua_getref(L, ctrl->ref[inout]);
114 lua_rawget(L, -2);
115 f = gethandle(L, ctrl, -1);
116 if (f == NULL)
117 luaL_verror(L, "global variable `%.10s' is not a file handle",
118 filenames[inout]);
119 return f;
120} 111}
121 112
122 113
123static void setfilebyname (lua_State *L, IOCtrl *ctrl, FILE *f, 114static void setfilebyname (lua_State *L, FILE *f, const char *name) {
124 const char *name) { 115 pushfile(L, f);
125 lua_pushusertag(L, f, ctrl->iotag);
126 lua_setglobal(L, name); 116 lua_setglobal(L, name);
127} 117}
128 118
129 119
130#define setfile(L,ctrl,f,inout) (setfilebyname(L,ctrl,f,filenames[inout])) 120#define setfile(L,f,inout) (setfilebyname(L,f,filenames[inout]))
131 121
132 122
133static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) { 123static int setreturn (lua_State *L, FILE *f, int inout) {
134 if (f == NULL) 124 if (f == NULL)
135 return pushresult(L, 0); 125 return pushresult(L, 0);
136 else { 126 else {
137 if (inout != NOFILE) 127 if (inout != NOFILE)
138 setfile(L, ctrl, f, inout); 128 setfile(L, f, inout);
139 lua_pushusertag(L, f, ctrl->iotag); 129 pushfile(L, f);
140 return 1; 130 return 1;
141 } 131 }
142} 132}
143 133
144 134
145static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) { 135static int closefile (lua_State *L, FILE *f) {
146 if (f == stdin || f == stdout || f == stderr) 136 if (f == stdin || f == stdout || f == stderr)
147 return 1; 137 return 1;
148 else { 138 else {
149 lua_pushusertag(L, f, ctrl->iotag); 139 pushfile(L, f);
150 lua_settag(L, ctrl->closedtag); 140 lua_settag(L, lua_type2tag(L, "ClosedFileHandle"));
151 return (CLOSEFILE(L, f) == 0); 141 return (CLOSEFILE(L, f) == 0);
152 } 142 }
153} 143}
154 144
155 145
156static int io_close (lua_State *L) { 146static int io_close (lua_State *L) {
157 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 147 FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
158 lua_pop(L, 1); /* remove upvalue */ 148 return pushresult(L, closefile(L, f));
159 return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1)));
160} 149}
161 150
162 151
163static int file_collect (lua_State *L) { 152static int file_collect (lua_State *L) {
164 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 153 FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
165 FILE *f = getnonullfile(L, ctrl, 1);
166 if (f != stdin && f != stdout && f != stderr) 154 if (f != stdin && f != stdout && f != stderr)
167 CLOSEFILE(L, f); 155 CLOSEFILE(L, f);
168 return 0; 156 return 0;
@@ -170,36 +158,30 @@ static int file_collect (lua_State *L) {
170 158
171 159
172static int io_open (lua_State *L) { 160static int io_open (lua_State *L) {
173 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 161 FILE *f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
174 FILE *f; 162 return setreturn(L, f, NOFILE);
175 lua_pop(L, 1); /* remove upvalue */
176 f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
177 return setreturn(L, ctrl, f, NOFILE);
178} 163}
179 164
180 165
181static int io_tmpfile (lua_State *L) { 166static int io_tmpfile (lua_State *L) {
182 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 167 return setreturn(L, tmpfile(), NOFILE);
183 return setreturn(L, ctrl, tmpfile(), NOFILE);
184} 168}
185 169
186 170
187 171
188static int io_fromto (lua_State *L, int inout, const char *mode) { 172static int io_fromto (lua_State *L, int inout, const char *mode) {
189 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
190 FILE *current; 173 FILE *current;
191 lua_pop(L, 1); /* remove upvalue */
192 if (lua_isnull(L, 1)) { 174 if (lua_isnull(L, 1)) {
193 closefile(L, ctrl, getfilebyref(L, ctrl, inout)); 175 closefile(L, getopthandle(L, inout));
194 current = (inout == 0) ? stdin : stdout; 176 current = (inout == 0) ? stdin : stdout;
195 } 177 }
196 else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */ 178 else if (checkfile(L, 1)) /* deprecated option */
197 current = (FILE *)lua_touserdata(L, 1); 179 current = (FILE *)lua_touserdata(L, 1);
198 else { 180 else {
199 const char *s = luaL_check_string(L, 1); 181 const char *s = luaL_check_string(L, 1);
200 current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); 182 current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
201 } 183 }
202 return setreturn(L, ctrl, current, inout); 184 return setreturn(L, current, inout);
203} 185}
204 186
205 187
@@ -214,11 +196,8 @@ static int io_writeto (lua_State *L) {
214 196
215 197
216static int io_appendto (lua_State *L) { 198static int io_appendto (lua_State *L) {
217 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 199 FILE *current = fopen(luaL_check_string(L, 1), "a");
218 FILE *current; 200 return setreturn(L, current, OUTFILE);
219 lua_pop(L, 1); /* remove upvalue */
220 current = fopen(luaL_check_string(L, 1), "a");
221 return setreturn(L, ctrl, current, OUTFILE);
222} 201}
223 202
224 203
@@ -315,31 +294,24 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
315 294
316 295
317static int io_read (lua_State *L) { 296static int io_read (lua_State *L) {
318 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 297 FILE *f = getopthandle(L, INFILE);
319 int lastarg = lua_gettop(L) - 1; 298 int nargs = lua_gettop(L);
320 int firstarg = 1;
321 int success; 299 int success;
322 FILE *f = gethandle(L, ctrl, firstarg);
323 int n; 300 int n;
324 if (f) firstarg++; 301 if (nargs == 0) { /* no arguments? */
325 else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */ 302 success = read_line(L, f);
326 lua_pop(L, 1); 303 n = 2; /* will return n-1 results */
327 if (firstarg > lastarg) { /* no arguments? */
328 lua_settop(L, 0); /* erase upvalue and other eventual garbage */
329 firstarg = lastarg = 1; /* correct indices */
330 lua_pushliteral(L, "*l"); /* push default argument */
331 } 304 }
332 else /* ensure stack space for all results and for auxlib's buffer */ 305 else { /* ensure stack space for all results and for auxlib's buffer */
333 luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments"); 306 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
334 success = 1; 307 success = 1;
335 for (n = firstarg; n<=lastarg && success; n++) { 308 for (n = 1; n<=nargs && success; n++) {
336 if (lua_isnumber(L, n))
337 success = read_chars(L, f, (size_t)lua_tonumber(L, n));
338 else {
339 const char *p = luaL_check_string(L, n); 309 const char *p = luaL_check_string(L, n);
340 if (p[0] != '*') { 310 if (p[0] != '*') {
341 lua_error(L, "read patterns are deprecated"); 311 if (lua_isnumber(L, n))
342 success = 0; /* to avoid warnings */ 312 success = read_chars(L, f, (size_t)lua_tonumber(L, n));
313 else
314 lua_error(L, "read patterns are deprecated");
343 } 315 }
344 else { 316 else {
345 switch (p[1]) { 317 switch (p[1]) {
@@ -367,21 +339,18 @@ static int io_read (lua_State *L) {
367 lua_pop(L, 1); /* remove last result */ 339 lua_pop(L, 1); /* remove last result */
368 lua_pushnil(L); /* push nil instead */ 340 lua_pushnil(L); /* push nil instead */
369 } 341 }
370 return n - firstarg; 342 return n - 1;
371} 343}
372 344
373/* }====================================================== */ 345/* }====================================================== */
374 346
375 347
376static int io_write (lua_State *L) { 348static int io_write (lua_State *L) {
377 int lastarg = lua_gettop(L) - 1; 349 FILE *f = getopthandle(L, OUTFILE);
378 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 350 int nargs = lua_gettop(L);
379 int arg = 1; 351 int arg;
380 int status = 1; 352 int status = 1;
381 FILE *f = gethandle(L, ctrl, arg); 353 for (arg=1; arg<=nargs; arg++) {
382 if (f) arg++;
383 else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
384 for (; arg <= lastarg; arg++) {
385 if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */ 354 if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */
386 /* optimization: could be done exactly as for strings */ 355 /* optimization: could be done exactly as for strings */
387 status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0; 356 status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
@@ -400,14 +369,9 @@ static int io_write (lua_State *L) {
400static int io_seek (lua_State *L) { 369static int io_seek (lua_State *L) {
401 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; 370 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
402 static const char *const modenames[] = {"set", "cur", "end", NULL}; 371 static const char *const modenames[] = {"set", "cur", "end", NULL};
403 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 372 FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
404 FILE *f; 373 int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
405 int op; 374 long offset = luaL_opt_long(L, 3, 0);
406 long offset;
407 lua_pop(L, 1); /* remove upvalue */
408 f = getnonullfile(L, ctrl, 1);
409 op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
410 offset = luaL_opt_long(L, 3, 0);
411 luaL_arg_check(L, op != -1, 2, "invalid mode"); 375 luaL_arg_check(L, op != -1, 2, "invalid mode");
412 op = fseek(f, offset, mode[op]); 376 op = fseek(f, offset, mode[op]);
413 if (op) 377 if (op)
@@ -420,10 +384,7 @@ static int io_seek (lua_State *L) {
420 384
421 385
422static int io_flush (lua_State *L) { 386static int io_flush (lua_State *L) {
423 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 387 FILE *f = getopthandle(L, NOFILE);
424 FILE *f;
425 lua_pop(L, 1); /* remove upvalue */
426 f = gethandle(L, ctrl, 1);
427 luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle"); 388 luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
428 return pushresult(L, fflush(f) == 0); 389 return pushresult(L, fflush(f) == 0);
429} 390}
@@ -686,66 +647,44 @@ static int errorfb (lua_State *L) {
686 647
687 648
688static const struct luaL_reg iolib[] = { 649static const struct luaL_reg iolib[] = {
689 {LUA_ERRORMESSAGE, errorfb}, 650 {"appendto", io_appendto},
690 {"clock", io_clock}, 651 {"clock", io_clock},
652 {"closefile", io_close},
691 {"date", io_date}, 653 {"date", io_date},
692 {"debug", io_debug}, 654 {"debug", io_debug},
693 {"difftime", io_difftime}, 655 {"difftime", io_difftime},
694 {"execute", io_execute}, 656 {"execute", io_execute},
695 {"exit", io_exit}, 657 {"exit", io_exit},
696 {"getenv", io_getenv},
697 {"remove", io_remove},
698 {"rename", io_rename},
699 {"setlocale", io_setloc},
700 {"time", io_time},
701 {"tmpname", io_tmpname}
702};
703
704
705static const struct luaL_reg iolibtag[] = {
706 {"appendto", io_appendto},
707 {"closefile", io_close},
708 {"flush", io_flush}, 658 {"flush", io_flush},
659 {"getenv", io_getenv},
709 {"openfile", io_open}, 660 {"openfile", io_open},
710 {"read", io_read}, 661 {"read", io_read},
711 {"readfrom", io_readfrom}, 662 {"readfrom", io_readfrom},
663 {"remove", io_remove},
664 {"rename", io_rename},
712 {"seek", io_seek}, 665 {"seek", io_seek},
666 {"setlocale", io_setloc},
667 {"time", io_time},
713 {"tmpfile", io_tmpfile}, 668 {"tmpfile", io_tmpfile},
669 {"tmpname", io_tmpname},
714 {"write", io_write}, 670 {"write", io_write},
715 {"writeto", io_writeto} 671 {"writeto", io_writeto},
672 {LUA_ERRORMESSAGE, errorfb}
716}; 673};
717 674
718 675
719static void openwithcontrol (lua_State *L) {
720 IOCtrl *ctrl = (IOCtrl *)lua_newuserdata(L, sizeof(IOCtrl));
721 unsigned int i;
722 ctrl->iotag = lua_newtag(L);
723 ctrl->closedtag = lua_newtag(L);
724 for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
725 /* put `ctrl' as upvalue for these functions */
726 lua_pushvalue(L, -1);
727 lua_pushcclosure(L, iolibtag[i].func, 1);
728 lua_setglobal(L, iolibtag[i].name);
729 }
730 /* create references to variable names */
731 lua_pushstring(L, filenames[INFILE]);
732 ctrl->ref[INFILE] = lua_ref(L, 1);
733 lua_pushstring(L, filenames[OUTFILE]);
734 ctrl->ref[OUTFILE] = lua_ref(L, 1);
735 /* predefined file handles */
736 setfile(L, ctrl, stdin, INFILE);
737 setfile(L, ctrl, stdout, OUTFILE);
738 setfilebyname(L, ctrl, stdin, "_STDIN");
739 setfilebyname(L, ctrl, stdout, "_STDOUT");
740 setfilebyname(L, ctrl, stderr, "_STDERR");
741 /* close files when collected */
742 lua_pushcclosure(L, file_collect, 1); /* pops `ctrl' from stack */
743 lua_settagmethod(L, ctrl->iotag, "gc");
744}
745
746
747LUALIB_API void lua_iolibopen (lua_State *L) { 676LUALIB_API void lua_iolibopen (lua_State *L) {
677 int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA);
678 lua_newtype(L, "ClosedFileHandle", LUA_TUSERDATA);
748 luaL_openl(L, iolib); 679 luaL_openl(L, iolib);
749 openwithcontrol(L); 680 /* predefined file handles */
681 setfile(L, stdin, INFILE);
682 setfile(L, stdout, OUTFILE);
683 setfilebyname(L, stdin, "_STDIN");
684 setfilebyname(L, stdout, "_STDOUT");
685 setfilebyname(L, stderr, "_STDERR");
686 /* close files when collected */
687 lua_pushcfunction(L, file_collect);
688 lua_settagmethod(L, iotag, "gc");
750} 689}
751 690
diff --git a/lobject.c b/lobject.c
index d709d442..a10dfeab 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.59 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: lobject.c,v 1.60 2001/01/24 15:45:33 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,12 +22,6 @@
22const TObject luaO_nilobject = {LUA_TNIL, {NULL}}; 22const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
23 23
24 24
25const char *const luaO_typenames[] = {
26 "userdata", "nil", "number", "string", "table", "function"
27};
28
29
30
31/* 25/*
32** returns smaller power of 2 larger than `n' (minimum is MINPOWER2) 26** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
33*/ 27*/
diff --git a/lobject.h b/lobject.h
index d025e43d..008b56ab 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.86 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: lobject.h,v 1.87 2001/01/19 13:20:30 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -212,10 +212,6 @@ typedef struct CallInfo {
212 212
213 213
214extern const TObject luaO_nilobject; 214extern const TObject luaO_nilobject;
215extern const char *const luaO_typenames[];
216
217
218#define luaO_typename(o) (luaO_typenames[ttype(o)])
219 215
220 216
221luint32 luaO_power2 (luint32 n); 217luint32 luaO_power2 (luint32 n);
diff --git a/lstate.c b/lstate.c
index 672aaba7..a5666839 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.52 2001/01/22 18:01:38 roberto Exp roberto $ 2** $Id: lstate.c,v 1.53 2001/01/24 15:45:33 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -86,6 +86,7 @@ static void f_luaopen (lua_State *L, void *ud) {
86 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); 86 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
87 luaD_init(L, so->stacksize); /* init stack */ 87 luaD_init(L, so->stacksize); /* init stack */
88 L->gt = luaH_new(L, 10); /* table of globals */ 88 L->gt = luaH_new(L, 10); /* table of globals */
89 G(L)->type2tag = luaH_new(L, 10);
89 luaS_init(L); 90 luaS_init(L);
90 luaX_init(L); 91 luaX_init(L);
91 luaT_init(L); 92 luaT_init(L);
diff --git a/lstate.h b/lstate.h
index a4c95e60..464af1c6 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.45 2001/01/22 18:01:38 roberto Exp roberto $ 2** $Id: lstate.h,v 1.46 2001/01/24 15:45:33 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -73,6 +73,7 @@ typedef struct global_State {
73 Hash *roottable; /* list of all tables */ 73 Hash *roottable; /* list of all tables */
74 stringtable strt; /* hash table for strings */ 74 stringtable strt; /* hash table for strings */
75 stringtable udt; /* hash table for udata */ 75 stringtable udt; /* hash table for udata */
76 Hash *type2tag; /* hash table from type names to tags */
76 struct TM *TMtable; /* table for tag methods */ 77 struct TM *TMtable; /* table for tag methods */
77 int sizeTM; /* size of TMtable */ 78 int sizeTM; /* size of TMtable */
78 int ntag; /* number of tags in TMtable */ 79 int ntag; /* number of tags in TMtable */
diff --git a/ltable.c b/ltable.c
index 81406e05..926fb49e 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.65 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: ltable.c,v 1.66 2001/01/24 15:45:33 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -236,6 +236,7 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
236 othern->next = n; /* redo the chain with `n' in place of `mp' */ 236 othern->next = n; /* redo the chain with `n' in place of `mp' */
237 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ 237 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
238 mp->next = NULL; /* now `mp' is free */ 238 mp->next = NULL; /* now `mp' is free */
239 setnilvalue(&mp->val);
239 } 240 }
240 else { /* colliding node is in its own main position */ 241 else { /* colliding node is in its own main position */
241 /* new node will go into free position */ 242 /* new node will go into free position */
@@ -245,6 +246,7 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
245 } 246 }
246 } 247 }
247 setobj(&mp->key, key); 248 setobj(&mp->key, key);
249 lua_assert(ttype(&mp->val) == LUA_TNIL);
248 for (;;) { /* correct `firstfree' */ 250 for (;;) { /* correct `firstfree' */
249 if (ttype(&t->firstfree->key) == LUA_TNIL) 251 if (ttype(&t->firstfree->key) == LUA_TNIL)
250 return &mp->val; /* OK; table still has a free place */ 252 return &mp->val; /* OK; table still has a free place */
diff --git a/ltm.c b/ltm.c
index 3f10a7b1..8e6996ae 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.61 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: ltm.c,v 1.62 2001/01/24 15:45:33 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,6 +14,8 @@
14#include "lmem.h" 14#include "lmem.h"
15#include "lobject.h" 15#include "lobject.h"
16#include "lstate.h" 16#include "lstate.h"
17#include "lstring.h"
18#include "ltable.h"
17#include "ltm.h" 19#include "ltm.h"
18 20
19 21
@@ -65,32 +67,38 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */
65} 67}
66 68
67 69
68static void init_entry (lua_State *L, int tag) {
69 int i;
70 for (i=0; i<TM_N; i++)
71 luaT_gettm(G(L), tag, i) = NULL;
72 G(L)->TMtable[tag].collected = NULL;
73}
74
75
76void luaT_init (lua_State *L) { 70void luaT_init (lua_State *L) {
77 int t; 71 static const char *const typenames[NUM_TAGS] = {
78 G(L)->TMtable = luaM_newvector(L, NUM_TAGS+2, struct TM); 72 "userdata", "nil", "number", "string", "table", "function"
79 G(L)->sizeTM = NUM_TAGS+2; 73 };
80 G(L)->ntag = NUM_TAGS; 74 int i;
81 for (t=0; t<G(L)->ntag; t++) 75 for (i=0; i<NUM_TAGS; i++)
82 init_entry(L, t); 76 luaT_newtag(L, typenames[i], i);
83} 77}
84 78
85 79
86LUA_API int lua_newtag (lua_State *L) { 80int luaT_newtag (lua_State *L, const char *name, int basictype) {
87 int tag; 81 int tag;
88 LUA_ENTRY; 82 int i;
83 TString *ts;
89 luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM, 84 luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
90 MAX_INT, "tag table overflow"); 85 MAX_INT, "tag table overflow");
91 init_entry(L, G(L)->ntag); 86 tag = G(L)->ntag;
92 tag = G(L)->ntag++; 87 if (name == NULL)
93 LUA_EXIT; 88 ts = NULL;
89 else {
90 TObject *v;
91 ts = luaS_new(L, name);
92 v = luaH_setstr(L, G(L)->type2tag, ts);
93 if (ttype(v) != LUA_TNIL) return LUA_TNONE; /* invalid name */
94 setnvalue(v, tag);
95 }
96 for (i=0; i<TM_N; i++)
97 luaT_gettm(G(L), tag, i) = NULL;
98 G(L)->TMtable[tag].collected = NULL;
99 G(L)->TMtable[tag].name = ts;
100 G(L)->TMtable[tag].basictype = basictype;
101 G(L)->ntag++;
94 return tag; 102 return tag;
95} 103}
96 104
@@ -100,11 +108,6 @@ static void checktag (lua_State *L, int tag) {
100 luaO_verror(L, "%d is not a valid tag", tag); 108 luaO_verror(L, "%d is not a valid tag", tag);
101} 109}
102 110
103void luaT_realtag (lua_State *L, int tag) {
104 if (!validtag(G(L), tag))
105 luaO_verror(L, "tag %d was not created by `newtag'", tag);
106}
107
108 111
109LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) { 112LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
110 int e; 113 int e;
@@ -130,6 +133,27 @@ int luaT_tag (const TObject *o) {
130} 133}
131 134
132 135
136const char *luaT_typename (global_State *G, const TObject *o) {
137 int t = ttype(o);
138 int tag;
139 TString *ts;
140 switch (t) {
141 case LUA_TUSERDATA:
142 tag = tsvalue(o)->u.d.tag;
143 break;
144 case LUA_TTABLE:
145 tag = hvalue(o)->htag;
146 break;
147 default:
148 tag = t;
149 }
150 ts = G->TMtable[tag].name;
151 if (ts == NULL)
152 ts = G->TMtable[t].name;
153 return ts->str;
154}
155
156
133LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) { 157LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
134 int e; 158 int e;
135 LUA_ENTRY; 159 LUA_ENTRY;
@@ -152,7 +176,7 @@ LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
152 checktag(L, t); 176 checktag(L, t);
153 if (!luaT_validevent(t, e)) 177 if (!luaT_validevent(t, e))
154 luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s", 178 luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
155 luaT_eventname[e], luaO_typenames[t], 179 luaT_eventname[e], basictypename(G(L), t),
156 (t == LUA_TTABLE || t == LUA_TUSERDATA) ? 180 (t == LUA_TTABLE || t == LUA_TUSERDATA) ?
157 " with default tag" : ""); 181 " with default tag" : "");
158 switch (ttype(L->top - 1)) { 182 switch (ttype(L->top - 1)) {
diff --git a/ltm.h b/ltm.h
index 9c0f99d3..11d51ccf 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 1.20 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: ltm.h,v 1.21 2001/01/24 16:20:54 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -38,6 +38,7 @@ typedef enum {
38 38
39/* 39/*
40** masks for allowable tag methods 40** masks for allowable tag methods
41** (see `luaT_validevents')
41*/ 42*/
42#define HAS_TM_GETGLOBAL(L,t) (1<<(t) & ((1<<LUA_TUSERDATA) | \ 43#define HAS_TM_GETGLOBAL(L,t) (1<<(t) & ((1<<LUA_TUSERDATA) | \
43 (1<<LUA_TTABLE) | \ 44 (1<<LUA_TTABLE) | \
@@ -53,12 +54,16 @@ typedef enum {
53struct TM { 54struct TM {
54 Closure *method[TM_N]; 55 Closure *method[TM_N];
55 TString *collected; /* list of garbage-collected udata with this tag */ 56 TString *collected; /* list of garbage-collected udata with this tag */
57 TString *name; /* type name */
58 int basictype;
56}; 59};
57 60
58 61
59#define luaT_gettm(G,tag,event) (G->TMtable[tag].method[event]) 62#define luaT_gettm(G,tag,event) (G->TMtable[tag].method[event])
60#define luaT_gettmbyObj(G,o,e) (luaT_gettm((G),luaT_tag(o),(e))) 63#define luaT_gettmbyObj(G,o,e) (luaT_gettm((G),luaT_tag(o),(e)))
61 64
65#define basictypename(G, t) (G->TMtable[t].name->str)
66
62 67
63#define validtag(G,t) (NUM_TAGS <= (t) && (t) < G->ntag) 68#define validtag(G,t) (NUM_TAGS <= (t) && (t) < G->ntag)
64 69
@@ -66,7 +71,8 @@ extern const char *const luaT_eventname[];
66 71
67 72
68void luaT_init (lua_State *L); 73void luaT_init (lua_State *L);
69void luaT_realtag (lua_State *L, int tag); 74int luaT_newtag (lua_State *L, const char *name, int basictype);
75const char *luaT_typename (global_State *G, const TObject *o);
70int luaT_tag (const TObject *o); 76int luaT_tag (const TObject *o);
71int luaT_validevent (int t, int e); /* used by compatibility module */ 77int luaT_validevent (int t, int e); /* used by compatibility module */
72 78
diff --git a/lua.h b/lua.h
index c8a105da..c6193483 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.82 2001/01/10 16:58:11 roberto Exp roberto $ 2** $Id: lua.h,v 1.83 2001/01/22 18:01:38 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
@@ -102,6 +102,7 @@ LUA_API int lua_stackspace (lua_State *L);
102 102
103LUA_API int lua_type (lua_State *L, int index); 103LUA_API int lua_type (lua_State *L, int index);
104LUA_API const char *lua_typename (lua_State *L, int t); 104LUA_API const char *lua_typename (lua_State *L, int t);
105LUA_API const char *lua_xtype (lua_State *L, int index);
105LUA_API int lua_isnumber (lua_State *L, int index); 106LUA_API int lua_isnumber (lua_State *L, int index);
106LUA_API int lua_isstring (lua_State *L, int index); 107LUA_API int lua_isstring (lua_State *L, int index);
107LUA_API int lua_iscfunction (lua_State *L, int index); 108LUA_API int lua_iscfunction (lua_State *L, int index);
@@ -173,7 +174,8 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
173/* 174/*
174** miscellaneous functions 175** miscellaneous functions
175*/ 176*/
176LUA_API int lua_newtag (lua_State *L); 177LUA_API int lua_newtype (lua_State *L, const char *name, int basictype);
178LUA_API int lua_type2tag (lua_State *L, const char *name);
177LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom); 179LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
178LUA_API void lua_settag (lua_State *L, int tag); 180LUA_API void lua_settag (lua_State *L, int tag);
179 181
@@ -212,6 +214,10 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
212 214
213#define lua_pushliteral(L, s) lua_pushlstring(L, "" s, (sizeof(s))-1) 215#define lua_pushliteral(L, s) lua_pushlstring(L, "" s, (sizeof(s))-1)
214 216
217
218
219#define lua_newtag(L) lua_newtype(L, NULL, LUA_TNONE)
220
215#endif 221#endif
216 222
217 223