diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-12-05 18:15:18 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-12-05 18:15:18 -0200 |
commit | 592a309177edc52847b1196969ad6d49ba21f4fb (patch) | |
tree | 06add977885c012ee22cc4f105785c435b6af353 /lbaselib.c | |
parent | 413fc7334bf8ceaea71417d73edef15c99d3a793 (diff) | |
download | lua-592a309177edc52847b1196969ad6d49ba21f4fb.tar.gz lua-592a309177edc52847b1196969ad6d49ba21f4fb.tar.bz2 lua-592a309177edc52847b1196969ad6d49ba21f4fb.zip |
tag system replaced by event tables
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 138 |
1 files changed, 40 insertions, 98 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.45 2001/10/26 17:33:30 roberto Exp $ | 2 | ** $Id: lbaselib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -41,7 +41,7 @@ static int luaB__ALERT (lua_State *L) { | |||
41 | ** The library `liolib' redefines _ERRORMESSAGE for better error information. | 41 | ** The library `liolib' redefines _ERRORMESSAGE for better error information. |
42 | */ | 42 | */ |
43 | static int luaB__ERRORMESSAGE (lua_State *L) { | 43 | static int luaB__ERRORMESSAGE (lua_State *L) { |
44 | luaL_check_rawtype(L, 1, LUA_TSTRING); | 44 | luaL_check_type(L, 1, LUA_TSTRING); |
45 | lua_getglobal(L, LUA_ALERT); | 45 | lua_getglobal(L, LUA_ALERT); |
46 | if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */ | 46 | if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */ |
47 | lua_Debug ar; | 47 | lua_Debug ar; |
@@ -136,41 +136,22 @@ static int luaB_getglobal (lua_State *L) { | |||
136 | } | 136 | } |
137 | 137 | ||
138 | 138 | ||
139 | /* auxiliary function to get `tags' */ | 139 | static int luaB_eventtable (lua_State *L) { |
140 | static int gettag (lua_State *L, int narg) { | 140 | luaL_check_type(L, 1, LUA_TTABLE); |
141 | switch (lua_rawtag(L, narg)) { | 141 | if (lua_isnull(L, 2)) |
142 | case LUA_TNUMBER: | 142 | lua_geteventtable(L, 1); |
143 | return (int)(lua_tonumber(L, narg)); | 143 | else { |
144 | case LUA_TSTRING: { | 144 | lua_settop(L, 2); |
145 | const char *name = lua_tostring(L, narg); | 145 | luaL_check_type(L, 2, LUA_TTABLE); |
146 | int tag = lua_name2tag(L, name); | 146 | lua_seteventtable(L, 1); |
147 | if (tag == LUA_TNONE) | ||
148 | luaL_verror(L, "'%.30s' is not a valid type name", name); | ||
149 | return tag; | ||
150 | } | ||
151 | default: | ||
152 | luaL_argerror(L, narg, "tag or type name expected"); | ||
153 | return 0; /* to avoid warnings */ | ||
154 | } | 147 | } |
155 | } | ||
156 | |||
157 | |||
158 | static int luaB_tag (lua_State *L) { | ||
159 | luaL_check_any(L, 1); | ||
160 | lua_pushnumber(L, lua_tag(L, 1)); | ||
161 | return 1; | 148 | return 1; |
162 | } | 149 | } |
163 | 150 | ||
164 | static int luaB_settype (lua_State *L) { | ||
165 | luaL_check_rawtype(L, 1, LUA_TTABLE); | ||
166 | lua_pushvalue(L, 1); /* push table */ | ||
167 | lua_settag(L, gettag(L, 2)); | ||
168 | return 1; /* return table */ | ||
169 | } | ||
170 | 151 | ||
171 | static int luaB_weakmode (lua_State *L) { | 152 | static int luaB_weakmode (lua_State *L) { |
172 | const char *mode = luaL_check_string(L, 2); | 153 | const char *mode = luaL_check_string(L, 2); |
173 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 154 | luaL_check_type(L, 1, LUA_TTABLE); |
174 | if (*mode == '?') { | 155 | if (*mode == '?') { |
175 | char buff[3]; | 156 | char buff[3]; |
176 | char *s = buff; | 157 | char *s = buff; |
@@ -191,17 +172,11 @@ static int luaB_weakmode (lua_State *L) { | |||
191 | } | 172 | } |
192 | } | 173 | } |
193 | 174 | ||
194 | static int luaB_newtype (lua_State *L) { | ||
195 | const char *name = luaL_opt_string(L, 1, NULL); | ||
196 | lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE)); | ||
197 | return 1; | ||
198 | } | ||
199 | |||
200 | 175 | ||
201 | static int luaB_globals (lua_State *L) { | 176 | static int luaB_globals (lua_State *L) { |
202 | lua_getglobals(L); /* value to be returned */ | 177 | lua_getglobals(L); /* value to be returned */ |
203 | if (!lua_isnull(L, 1)) { | 178 | if (!lua_isnull(L, 1)) { |
204 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 179 | luaL_check_type(L, 1, LUA_TTABLE); |
205 | lua_pushvalue(L, 1); /* new table of globals */ | 180 | lua_pushvalue(L, 1); /* new table of globals */ |
206 | lua_setglobals(L); | 181 | lua_setglobals(L); |
207 | } | 182 | } |
@@ -209,43 +184,20 @@ static int luaB_globals (lua_State *L) { | |||
209 | } | 184 | } |
210 | 185 | ||
211 | static int luaB_rawget (lua_State *L) { | 186 | static int luaB_rawget (lua_State *L) { |
212 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 187 | luaL_check_type(L, 1, LUA_TTABLE); |
213 | luaL_check_any(L, 2); | 188 | luaL_check_any(L, 2); |
214 | lua_rawget(L, -2); | 189 | lua_rawget(L, -2); |
215 | return 1; | 190 | return 1; |
216 | } | 191 | } |
217 | 192 | ||
218 | static int luaB_rawset (lua_State *L) { | 193 | static int luaB_rawset (lua_State *L) { |
219 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 194 | luaL_check_type(L, 1, LUA_TTABLE); |
220 | luaL_check_any(L, 2); | 195 | luaL_check_any(L, 2); |
221 | luaL_check_any(L, 3); | 196 | luaL_check_any(L, 3); |
222 | lua_rawset(L, -3); | 197 | lua_rawset(L, -3); |
223 | return 1; | 198 | return 1; |
224 | } | 199 | } |
225 | 200 | ||
226 | static int luaB_settagmethod (lua_State *L) { | ||
227 | int tag = gettag(L, 1); | ||
228 | const char *event = luaL_check_string(L, 2); | ||
229 | luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3, | ||
230 | "function or nil expected"); | ||
231 | if (strcmp(event, "gc") == 0) | ||
232 | lua_error(L, "cannot set `gc' tag method from Lua"); | ||
233 | lua_gettagmethod(L, tag, event); | ||
234 | lua_pushvalue(L, 3); | ||
235 | lua_settagmethod(L, tag, event); | ||
236 | return 1; | ||
237 | } | ||
238 | |||
239 | |||
240 | static int luaB_gettagmethod (lua_State *L) { | ||
241 | int tag = gettag(L, 1); | ||
242 | const char *event = luaL_check_string(L, 2); | ||
243 | if (strcmp(event, "gc") == 0) | ||
244 | lua_error(L, "cannot get `gc' tag method from Lua"); | ||
245 | lua_gettagmethod(L, tag, event); | ||
246 | return 1; | ||
247 | } | ||
248 | |||
249 | 201 | ||
250 | static int luaB_gcinfo (lua_State *L) { | 202 | static int luaB_gcinfo (lua_State *L) { |
251 | lua_pushnumber(L, lua_getgccount(L)); | 203 | lua_pushnumber(L, lua_getgccount(L)); |
@@ -262,20 +214,20 @@ static int luaB_collectgarbage (lua_State *L) { | |||
262 | 214 | ||
263 | static int luaB_type (lua_State *L) { | 215 | static int luaB_type (lua_State *L) { |
264 | luaL_check_any(L, 1); | 216 | luaL_check_any(L, 1); |
265 | lua_pushstring(L, lua_type(L, 1)); | 217 | if (lua_isnull(L, 2)) |
266 | return 1; | 218 | lua_pushstring(L, lua_typename(L, lua_type(L, 1))); |
267 | } | 219 | else { |
268 | 220 | if (strcmp(lua_typename(L, lua_type(L, 1)), luaL_check_string(L, 2)) == 0) | |
269 | 221 | lua_pushnumber(L, 1); | |
270 | static int luaB_rawtype (lua_State *L) { | 222 | else |
271 | luaL_check_any(L, 1); | 223 | lua_pushnil(L); |
272 | lua_pushstring(L, lua_tag2name(L, lua_rawtag(L, 1))); | 224 | } |
273 | return 1; | 225 | return 1; |
274 | } | 226 | } |
275 | 227 | ||
276 | 228 | ||
277 | static int luaB_next (lua_State *L) { | 229 | static int luaB_next (lua_State *L) { |
278 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 230 | luaL_check_type(L, 1, LUA_TTABLE); |
279 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ | 231 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ |
280 | if (lua_next(L, 1)) | 232 | if (lua_next(L, 1)) |
281 | return 2; | 233 | return 2; |
@@ -393,7 +345,7 @@ static int luaB_require (lua_State *L) { | |||
393 | 345 | ||
394 | static int aux_unpack (lua_State *L, int arg) { | 346 | static int aux_unpack (lua_State *L, int arg) { |
395 | int n, i; | 347 | int n, i; |
396 | luaL_check_rawtype(L, arg, LUA_TTABLE); | 348 | luaL_check_type(L, arg, LUA_TTABLE); |
397 | n = lua_getn(L, arg); | 349 | n = lua_getn(L, arg); |
398 | luaL_check_stack(L, n, "table too big to unpack"); | 350 | luaL_check_stack(L, n, "table too big to unpack"); |
399 | for (i=1; i<=n; i++) /* push arg[1...n] */ | 351 | for (i=1; i<=n; i++) /* push arg[1...n] */ |
@@ -443,7 +395,7 @@ static int luaB_call (lua_State *L) { | |||
443 | 395 | ||
444 | static int luaB_tostring (lua_State *L) { | 396 | static int luaB_tostring (lua_State *L) { |
445 | char buff[64]; | 397 | char buff[64]; |
446 | switch (lua_rawtag(L, 1)) { | 398 | switch (lua_type(L, 1)) { |
447 | case LUA_TNUMBER: | 399 | case LUA_TNUMBER: |
448 | lua_pushstring(L, lua_tostring(L, 1)); | 400 | lua_pushstring(L, lua_tostring(L, 1)); |
449 | return 1; | 401 | return 1; |
@@ -451,16 +403,15 @@ static int luaB_tostring (lua_State *L) { | |||
451 | lua_pushvalue(L, 1); | 403 | lua_pushvalue(L, 1); |
452 | return 1; | 404 | return 1; |
453 | case LUA_TTABLE: | 405 | case LUA_TTABLE: |
454 | sprintf(buff, "%.40s: %p", lua_type(L, 1), lua_topointer(L, 1)); | 406 | sprintf(buff, "%.40s: %p", lua_typename(L, lua_type(L, 1)), lua_topointer(L, 1)); |
455 | break; | 407 | break; |
456 | case LUA_TFUNCTION: | 408 | case LUA_TFUNCTION: |
457 | sprintf(buff, "function: %p", lua_topointer(L, 1)); | 409 | sprintf(buff, "function: %p", lua_topointer(L, 1)); |
458 | break; | 410 | break; |
459 | case LUA_TUSERDATA: { | 411 | case LUA_TUSERDATA: { |
460 | const char *t = lua_type(L, 1); | 412 | const char *t = lua_typename(L, lua_type(L, 1)); |
461 | if (strcmp(t, "userdata") == 0) | 413 | if (strcmp(t, "userdata") == 0) |
462 | sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), | 414 | sprintf(buff, "userdata: %p", lua_touserdata(L, 1)); |
463 | lua_touserdata(L, 1)); | ||
464 | else | 415 | else |
465 | sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1)); | 416 | sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1)); |
466 | break; | 417 | break; |
@@ -478,8 +429,8 @@ static int luaB_tostring (lua_State *L) { | |||
478 | 429 | ||
479 | static int luaB_foreachi (lua_State *L) { | 430 | static int luaB_foreachi (lua_State *L) { |
480 | int n, i; | 431 | int n, i; |
481 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 432 | luaL_check_type(L, 1, LUA_TTABLE); |
482 | luaL_check_rawtype(L, 2, LUA_TFUNCTION); | 433 | luaL_check_type(L, 2, LUA_TFUNCTION); |
483 | n = lua_getn(L, 1); | 434 | n = lua_getn(L, 1); |
484 | for (i=1; i<=n; i++) { | 435 | for (i=1; i<=n; i++) { |
485 | lua_pushvalue(L, 2); /* function */ | 436 | lua_pushvalue(L, 2); /* function */ |
@@ -495,8 +446,8 @@ static int luaB_foreachi (lua_State *L) { | |||
495 | 446 | ||
496 | 447 | ||
497 | static int luaB_foreach (lua_State *L) { | 448 | static int luaB_foreach (lua_State *L) { |
498 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 449 | luaL_check_type(L, 1, LUA_TTABLE); |
499 | luaL_check_rawtype(L, 2, LUA_TFUNCTION); | 450 | luaL_check_type(L, 2, LUA_TFUNCTION); |
500 | lua_pushnil(L); /* first index */ | 451 | lua_pushnil(L); /* first index */ |
501 | for (;;) { | 452 | for (;;) { |
502 | if (lua_next(L, 1) == 0) | 453 | if (lua_next(L, 1) == 0) |
@@ -523,7 +474,7 @@ static int luaB_assert (lua_State *L) { | |||
523 | 474 | ||
524 | 475 | ||
525 | static int luaB_getn (lua_State *L) { | 476 | static int luaB_getn (lua_State *L) { |
526 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 477 | luaL_check_type(L, 1, LUA_TTABLE); |
527 | lua_pushnumber(L, lua_getn(L, 1)); | 478 | lua_pushnumber(L, lua_getn(L, 1)); |
528 | return 1; | 479 | return 1; |
529 | } | 480 | } |
@@ -532,7 +483,7 @@ static int luaB_getn (lua_State *L) { | |||
532 | static int luaB_tinsert (lua_State *L) { | 483 | static int luaB_tinsert (lua_State *L) { |
533 | int v = lua_gettop(L); /* number of arguments */ | 484 | int v = lua_gettop(L); /* number of arguments */ |
534 | int n, pos; | 485 | int n, pos; |
535 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 486 | luaL_check_type(L, 1, LUA_TTABLE); |
536 | n = lua_getn(L, 1); | 487 | n = lua_getn(L, 1); |
537 | if (v == 2) /* called with only 2 arguments */ | 488 | if (v == 2) /* called with only 2 arguments */ |
538 | pos = n+1; | 489 | pos = n+1; |
@@ -553,7 +504,7 @@ static int luaB_tinsert (lua_State *L) { | |||
553 | 504 | ||
554 | static int luaB_tremove (lua_State *L) { | 505 | static int luaB_tremove (lua_State *L) { |
555 | int pos, n; | 506 | int pos, n; |
556 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 507 | luaL_check_type(L, 1, LUA_TTABLE); |
557 | n = lua_getn(L, 1); | 508 | n = lua_getn(L, 1); |
558 | pos = luaL_opt_int(L, 2, n); | 509 | pos = luaL_opt_int(L, 2, n); |
559 | if (n <= 0) return 0; /* table is `empty' */ | 510 | if (n <= 0) return 0; /* table is `empty' */ |
@@ -665,10 +616,10 @@ static void auxsort (lua_State *L, int l, int u) { | |||
665 | 616 | ||
666 | static int luaB_sort (lua_State *L) { | 617 | static int luaB_sort (lua_State *L) { |
667 | int n; | 618 | int n; |
668 | luaL_check_rawtype(L, 1, LUA_TTABLE); | 619 | luaL_check_type(L, 1, LUA_TTABLE); |
669 | n = lua_getn(L, 1); | 620 | n = lua_getn(L, 1); |
670 | if (!lua_isnull(L, 2)) /* is there a 2nd argument? */ | 621 | if (!lua_isnull(L, 2)) /* is there a 2nd argument? */ |
671 | luaL_check_rawtype(L, 2, LUA_TFUNCTION); | 622 | luaL_check_type(L, 2, LUA_TFUNCTION); |
672 | lua_settop(L, 2); /* make sure there is two arguments */ | 623 | lua_settop(L, 2); /* make sure there is two arguments */ |
673 | auxsort(L, 1, n); | 624 | auxsort(L, 1, n); |
674 | return 0; | 625 | return 0; |
@@ -686,28 +637,19 @@ static const luaL_reg base_funcs[] = { | |||
686 | {"dofile", luaB_dofile}, | 637 | {"dofile", luaB_dofile}, |
687 | {"dostring", luaB_dostring}, | 638 | {"dostring", luaB_dostring}, |
688 | {"error", luaB_error}, | 639 | {"error", luaB_error}, |
640 | {"eventtable", luaB_eventtable}, | ||
689 | {"foreach", luaB_foreach}, | 641 | {"foreach", luaB_foreach}, |
690 | {"foreachi", luaB_foreachi}, | 642 | {"foreachi", luaB_foreachi}, |
691 | {"gcinfo", luaB_gcinfo}, | 643 | {"gcinfo", luaB_gcinfo}, |
692 | {"getglobal", luaB_getglobal}, | 644 | {"getglobal", luaB_getglobal}, /* compatibility with 4.0 */ |
693 | {"gettagmethod", luaB_gettagmethod}, | ||
694 | {"globals", luaB_globals}, | 645 | {"globals", luaB_globals}, |
695 | {"loadfile", luaB_loadfile}, | 646 | {"loadfile", luaB_loadfile}, |
696 | {"loadstring", luaB_loadstring}, | 647 | {"loadstring", luaB_loadstring}, |
697 | {"newtype", luaB_newtype}, | ||
698 | {"newtag", luaB_newtype}, /* for compatibility 4.0 */ | ||
699 | {"next", luaB_next}, | 648 | {"next", luaB_next}, |
700 | {"print", luaB_print}, | 649 | {"print", luaB_print}, |
701 | {"rawget", luaB_rawget}, | 650 | {"rawget", luaB_rawget}, |
702 | {"rawset", luaB_rawset}, | 651 | {"rawset", luaB_rawset}, |
703 | {"rawgettable", luaB_rawget}, /* for compatibility 3.2 */ | 652 | {"setglobal", luaB_setglobal}, /* compatibility with 4.0 */ |
704 | {"rawsettable", luaB_rawset}, /* for compatibility 3.2 */ | ||
705 | {"rawtype", luaB_rawtype}, | ||
706 | {"setglobal", luaB_setglobal}, | ||
707 | {"settag", luaB_settype}, /* for compatibility 4.0 */ | ||
708 | {"settype", luaB_settype}, | ||
709 | {"settagmethod", luaB_settagmethod}, | ||
710 | {"tag", luaB_tag}, | ||
711 | {"tonumber", luaB_tonumber}, | 653 | {"tonumber", luaB_tonumber}, |
712 | {"tostring", luaB_tostring}, | 654 | {"tostring", luaB_tostring}, |
713 | {"type", luaB_type}, | 655 | {"type", luaB_type}, |