aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-10-05 09:14:08 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-10-05 09:14:08 -0300
commit001f2bdd0e2f8803889c1b5164b57a51e44aef5b (patch)
treed200cf4d708be3c61e64640c45b47050c9c6a375 /lapi.c
parentcd2ddaded97f7f2b2af02cecfd165cf70e6f83f4 (diff)
downloadlua-001f2bdd0e2f8803889c1b5164b57a51e44aef5b.tar.gz
lua-001f2bdd0e2f8803889c1b5164b57a51e44aef5b.tar.bz2
lua-001f2bdd0e2f8803889c1b5164b57a51e44aef5b.zip
new definition for types-tags
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c77
1 files changed, 35 insertions, 42 deletions
diff --git a/lapi.c b/lapi.c
index 7715c5a2..e9f9078e 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.103 2000/10/02 20:10:55 roberto Exp roberto $ 2** $Id: lapi.c,v 1.104 2000/10/03 14:27:44 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*/
@@ -116,20 +116,18 @@ void lua_pushvalue (lua_State *L, int index) {
116 return ((test) ? (value) : (default)); } 116 return ((test) ? (value) : (default)); }
117 117
118 118
119lua_Type lua_type (lua_State *L, int index) { 119int lua_type (lua_State *L, int index) {
120 btest(L, index, luaO_type(o), LUA_NOVALUE); 120 btest(L, index, ttype(o), LUA_TNONE);
121} 121}
122 122
123const char *lua_typename (lua_State *L, lua_Type t) { 123const char *lua_typename (lua_State *L, int t) {
124 static const char *const names[] = {
125 "NO VALUE", "userdata", "number", "string", "table", "function", "nil"
126 };
127 UNUSED(L); 124 UNUSED(L);
128 return names[(int)t]; 125 return luaO_typenames[t];
129} 126}
130 127
128
131int lua_iscfunction (lua_State *L, int index) { 129int lua_iscfunction (lua_State *L, int index) {
132 btest(L, index, (ttype(o) == TAG_CCLOSURE), 0); 130 btest(L, index, iscfunction(o), 0);
133} 131}
134 132
135int lua_isnumber (lua_State *L, int index) { 133int lua_isnumber (lua_State *L, int index) {
@@ -137,18 +135,13 @@ int lua_isnumber (lua_State *L, int index) {
137} 135}
138 136
139int lua_isstring (lua_State *L, int index) { 137int lua_isstring (lua_State *L, int index) {
140 lua_Type t = lua_type(L, index); 138 int t = lua_type(L, index);
141 return (t == LUA_TSTRING || t == LUA_TNUMBER); 139 return (t == LUA_TSTRING || t == LUA_TNUMBER);
142} 140}
143 141
144 142
145static int auxtag (const TObject *o) {
146return ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
147 (ttype(o) == TAG_TABLE) ? hvalue(o)->htag : (int)ttype(o));
148}
149
150int lua_tag (lua_State *L, int index) { 143int lua_tag (lua_State *L, int index) {
151 btest(L, index, auxtag(o), LUA_NOTAG); 144 btest(L, index, luaT_tag(o), LUA_NOTAG);
152} 145}
153 146
154int lua_equal (lua_State *L, int index1, int index2) { 147int lua_equal (lua_State *L, int index1, int index2) {
@@ -168,7 +161,7 @@ int lua_lessthan (lua_State *L, int index1, int index2) {
168 161
169 162
170double lua_tonumber (lua_State *L, int index) { 163double lua_tonumber (lua_State *L, int index) {
171 access(L, index, (tonumber(o) == 0), 0.0, nvalue(o)); 164 access(L, index, (tonumber(o) == 0), 0, nvalue(o));
172} 165}
173 166
174const char *lua_tostring (lua_State *L, int index) { 167const char *lua_tostring (lua_State *L, int index) {
@@ -180,19 +173,19 @@ size_t lua_strlen (lua_State *L, int index) {
180} 173}
181 174
182lua_CFunction lua_tocfunction (lua_State *L, int index) { 175lua_CFunction lua_tocfunction (lua_State *L, int index) {
183 access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c); 176 access(L, index, iscfunction(o), NULL, clvalue(o)->f.c);
184} 177}
185 178
186void *lua_touserdata (lua_State *L, int index) { 179void *lua_touserdata (lua_State *L, int index) {
187 access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value); 180 access(L, index, (ttype(o) == LUA_TUSERDATA), NULL, tsvalue(o)->u.d.value);
188} 181}
189 182
190const void *lua_topointer (lua_State *L, int index) { 183const void *lua_topointer (lua_State *L, int index) {
191 StkId o = Index(L, index); 184 StkId o = Index(L, index);
192 switch (ttype(o)) { 185 switch (ttype(o)) {
193 case TAG_TABLE: 186 case LUA_TTABLE:
194 return hvalue(o); 187 return hvalue(o);
195 case TAG_CCLOSURE: case TAG_LCLOSURE: 188 case LUA_TFUNCTION:
196 return clvalue(o); 189 return clvalue(o);
197 default: return NULL; 190 default: return NULL;
198 } 191 }
@@ -206,13 +199,13 @@ const void *lua_topointer (lua_State *L, int index) {
206 199
207 200
208void lua_pushnil (lua_State *L) { 201void lua_pushnil (lua_State *L) {
209 ttype(L->top) = TAG_NIL; 202 ttype(L->top) = LUA_TNIL;
210 api_incr_top(L); 203 api_incr_top(L);
211} 204}
212 205
213 206
214void lua_pushnumber (lua_State *L, double n) { 207void lua_pushnumber (lua_State *L, double n) {
215 ttype(L->top) = TAG_NUMBER; 208 ttype(L->top) = LUA_TNUMBER;
216 nvalue(L->top) = n; 209 nvalue(L->top) = n;
217 api_incr_top(L); 210 api_incr_top(L);
218} 211}
@@ -220,7 +213,7 @@ void lua_pushnumber (lua_State *L, double n) {
220 213
221void lua_pushlstring (lua_State *L, const char *s, size_t len) { 214void lua_pushlstring (lua_State *L, const char *s, size_t len) {
222 tsvalue(L->top) = luaS_newlstr(L, s, len); 215 tsvalue(L->top) = luaS_newlstr(L, s, len);
223 ttype(L->top) = TAG_STRING; 216 ttype(L->top) = LUA_TSTRING;
224 api_incr_top(L); 217 api_incr_top(L);
225} 218}
226 219
@@ -239,10 +232,10 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
239 232
240 233
241void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */ 234void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
242 if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS) 235 if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
243 luaO_verror(L, "invalid tag for a userdata (%d)", tag); 236 luaO_verror(L, "invalid tag for a userdata (%d)", tag);
244 tsvalue(L->top) = luaS_createudata(L, u, tag); 237 tsvalue(L->top) = luaS_createudata(L, u, tag);
245 ttype(L->top) = TAG_USERDATA; 238 ttype(L->top) = LUA_TUSERDATA;
246 api_incr_top(L); 239 api_incr_top(L);
247} 240}
248 241
@@ -271,14 +264,14 @@ void lua_gettable (lua_State *L, int index) {
271 264
272void lua_rawget (lua_State *L, int index) { 265void lua_rawget (lua_State *L, int index) {
273 StkId t = Index(L, index); 266 StkId t = Index(L, index);
274 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); 267 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
275 *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1); 268 *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
276} 269}
277 270
278 271
279void lua_rawgeti (lua_State *L, int index, int n) { 272void lua_rawgeti (lua_State *L, int index, int n) {
280 StkId o = Index(L, index); 273 StkId o = Index(L, index);
281 LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected"); 274 LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
282 *L->top = *luaH_getnum(hvalue(o), n); 275 *L->top = *luaH_getnum(hvalue(o), n);
283 api_incr_top(L); 276 api_incr_top(L);
284} 277}
@@ -286,14 +279,14 @@ void lua_rawgeti (lua_State *L, int index, int n) {
286 279
287void lua_getglobals (lua_State *L) { 280void lua_getglobals (lua_State *L) {
288 hvalue(L->top) = L->gt; 281 hvalue(L->top) = L->gt;
289 ttype(L->top) = TAG_TABLE; 282 ttype(L->top) = LUA_TTABLE;
290 api_incr_top(L); 283 api_incr_top(L);
291} 284}
292 285
293 286
294int lua_getref (lua_State *L, int ref) { 287int lua_getref (lua_State *L, int ref) {
295 if (ref == LUA_REFNIL) 288 if (ref == LUA_REFNIL)
296 ttype(L->top) = TAG_NIL; 289 ttype(L->top) = LUA_TNIL;
297 else if (0 <= ref && ref < L->refSize && 290 else if (0 <= ref && ref < L->refSize &&
298 (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) 291 (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
299 *L->top = L->refArray[ref].o; 292 *L->top = L->refArray[ref].o;
@@ -306,7 +299,7 @@ int lua_getref (lua_State *L, int ref) {
306 299
307void lua_newtable (lua_State *L) { 300void lua_newtable (lua_State *L) {
308 hvalue(L->top) = luaH_new(L, 0); 301 hvalue(L->top) = luaH_new(L, 0);
309 ttype(L->top) = TAG_TABLE; 302 ttype(L->top) = LUA_TTABLE;
310 api_incr_top(L); 303 api_incr_top(L);
311} 304}
312 305
@@ -334,7 +327,7 @@ void lua_settable (lua_State *L, int index) {
334 327
335void lua_rawset (lua_State *L, int index) { 328void lua_rawset (lua_State *L, int index) {
336 StkId t = Index(L, index); 329 StkId t = Index(L, index);
337 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); 330 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
338 *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1); 331 *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
339 L->top -= 2; 332 L->top -= 2;
340} 333}
@@ -342,7 +335,7 @@ void lua_rawset (lua_State *L, int index) {
342 335
343void lua_rawseti (lua_State *L, int index, int n) { 336void lua_rawseti (lua_State *L, int index, int n) {
344 StkId o = Index(L, index); 337 StkId o = Index(L, index);
345 LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected"); 338 LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
346 *luaH_setint(L, hvalue(o), n) = *(L->top-1); 339 *luaH_setint(L, hvalue(o), n) = *(L->top-1);
347 L->top--; 340 L->top--;
348} 341}
@@ -350,14 +343,14 @@ void lua_rawseti (lua_State *L, int index, int n) {
350 343
351void lua_setglobals (lua_State *L) { 344void lua_setglobals (lua_State *L) {
352 StkId newtable = --L->top; 345 StkId newtable = --L->top;
353 LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); 346 LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
354 L->gt = hvalue(newtable); 347 L->gt = hvalue(newtable);
355} 348}
356 349
357 350
358int lua_ref (lua_State *L, int lock) { 351int lua_ref (lua_State *L, int lock) {
359 int ref; 352 int ref;
360 if (ttype(L->top-1) == TAG_NIL) 353 if (ttype(L->top-1) == LUA_TNIL)
361 ref = LUA_REFNIL; 354 ref = LUA_REFNIL;
362 else { 355 else {
363 if (L->refFree != NONEXT) { /* is there a free place? */ 356 if (L->refFree != NONEXT) { /* is there a free place? */
@@ -420,15 +413,15 @@ void lua_setgcthreshold (lua_State *L, int newthreshold) {
420void lua_settag (lua_State *L, int tag) { 413void lua_settag (lua_State *L, int tag) {
421 luaT_realtag(L, tag); 414 luaT_realtag(L, tag);
422 switch (ttype(L->top-1)) { 415 switch (ttype(L->top-1)) {
423 case TAG_TABLE: 416 case LUA_TTABLE:
424 hvalue(L->top-1)->htag = tag; 417 hvalue(L->top-1)->htag = tag;
425 break; 418 break;
426 case TAG_USERDATA: 419 case LUA_TUSERDATA:
427 tsvalue(L->top-1)->u.d.tag = tag; 420 tsvalue(L->top-1)->u.d.tag = tag;
428 break; 421 break;
429 default: 422 default:
430 luaO_verror(L, "cannot change the tag of a %.20s", 423 luaO_verror(L, "cannot change the tag of a %.20s",
431 luaO_typename(L, L->top-1)); 424 luaO_typename(L->top-1));
432 } 425 }
433 L->top--; 426 L->top--;
434} 427}
@@ -446,7 +439,7 @@ void lua_unref (lua_State *L, int ref) {
446int lua_next (lua_State *L, int index) { 439int lua_next (lua_State *L, int index) {
447 StkId t = Index(L, index); 440 StkId t = Index(L, index);
448 Node *n; 441 Node *n;
449 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); 442 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
450 n = luaH_next(L, hvalue(t), Index(L, -1)); 443 n = luaH_next(L, hvalue(t), Index(L, -1));
451 if (n) { 444 if (n) {
452 *(L->top-1) = *key(n); 445 *(L->top-1) = *key(n);
@@ -464,15 +457,15 @@ int lua_next (lua_State *L, int index) {
464int lua_getn (lua_State *L, int index) { 457int lua_getn (lua_State *L, int index) {
465 Hash *h = hvalue(Index(L, index)); 458 Hash *h = hvalue(Index(L, index));
466 const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ 459 const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
467 if (ttype(value) == TAG_NUMBER) 460 if (ttype(value) == LUA_TNUMBER)
468 return (int)nvalue(value); 461 return (int)nvalue(value);
469 else { 462 else {
470 Number max = 0; 463 Number max = 0;
471 int i = h->size; 464 int i = h->size;
472 Node *n = h->node; 465 Node *n = h->node;
473 while (i--) { 466 while (i--) {
474 if (ttype(key(n)) == TAG_NUMBER && 467 if (ttype(key(n)) == LUA_TNUMBER &&
475 ttype(val(n)) != TAG_NIL && 468 ttype(val(n)) != LUA_TNIL &&
476 nvalue(key(n)) > max) 469 nvalue(key(n)) > max)
477 max = nvalue(key(n)); 470 max = nvalue(key(n));
478 n++; 471 n++;