diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-25 14:45:36 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-25 14:45:36 -0200 |
commit | a53d9b66ca6247818acaf41e28cdf123082a272b (patch) | |
tree | 8e909200d4d925fc7394e6adf83cc5941456c588 /lapi.c | |
parent | c8559e3c8d12e052783e2952db1372c68cc16059 (diff) | |
download | lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.gz lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.bz2 lua-a53d9b66ca6247818acaf41e28cdf123082a272b.zip |
first implementation for type names
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 60 |
1 files changed, 54 insertions, 6 deletions
@@ -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 | |||
135 | LUA_API const char *lua_typename (lua_State *L, int t) { | 136 | LUA_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 | ||
145 | LUA_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 | |||
145 | LUA_API int lua_iscfunction (lua_State *L, int index) { | 156 | LUA_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 | ||
556 | LUA_API void lua_settag (lua_State *L, int tag) { | 567 | LUA_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 | |||
582 | LUA_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 | |||
598 | LUA_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 | } |