diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 53 |
1 files changed, 41 insertions, 12 deletions
@@ -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' */ | ||
133 | static 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 | |||
131 | static int luaB_tag (lua_State *L) { | 151 | static 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) { | |||
137 | static int luaB_settag (lua_State *L) { | 157 | static 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 | ||
144 | static int luaB_newtag (lua_State *L) { | 164 | static 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 | ||
149 | static int luaB_copytagmethods (lua_State *L) { | 170 | static 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 | ||
180 | static int luaB_settagmethod (lua_State *L) { | 200 | static 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 | ||
194 | static int luaB_gettagmethod (lua_State *L) { | 214 | static 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 | ||
244 | static 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 | |||
224 | static int luaB_next (lua_State *L) { | 251 | static 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 | ||