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 /lapi.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 'lapi.c')
-rw-r--r-- | lapi.c | 150 |
1 files changed, 59 insertions, 91 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.160 2001/11/16 16:29:51 roberto Exp $ | 2 | ** $Id: lapi.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -45,8 +45,8 @@ static TObject *negindex (lua_State *L, int index) { | |||
45 | return L->top+index; | 45 | return L->top+index; |
46 | } | 46 | } |
47 | else switch (index) { /* pseudo-indices */ | 47 | else switch (index) { /* pseudo-indices */ |
48 | case LUA_REGISTRYINDEX: return &G(L)->registry; | 48 | case LUA_REGISTRYINDEX: return registry(L); |
49 | case LUA_GLOBALSINDEX: return &L->gt; | 49 | case LUA_GLOBALSINDEX: return gt(L); |
50 | default: { | 50 | default: { |
51 | TObject *func = (L->ci->base - 1); | 51 | TObject *func = (L->ci->base - 1); |
52 | index = LUA_GLOBALSINDEX - index; | 52 | index = LUA_GLOBALSINDEX - index; |
@@ -149,20 +149,15 @@ LUA_API void lua_pushvalue (lua_State *L, int index) { | |||
149 | */ | 149 | */ |
150 | 150 | ||
151 | 151 | ||
152 | LUA_API int lua_rawtag (lua_State *L, int index) { | 152 | LUA_API int lua_type (lua_State *L, int index) { |
153 | StkId o = luaA_indexAcceptable(L, index); | 153 | StkId o = luaA_indexAcceptable(L, index); |
154 | return (o == NULL) ? LUA_TNONE : ttype(o); | 154 | return (o == NULL) ? LUA_TNONE : ttype(o); |
155 | } | 155 | } |
156 | 156 | ||
157 | 157 | ||
158 | LUA_API const char *lua_type (lua_State *L, int index) { | 158 | LUA_API const char *lua_typename (lua_State *L, int t) { |
159 | StkId o; | 159 | UNUSED(L); |
160 | const char *type; | 160 | return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; |
161 | lua_lock(L); | ||
162 | o = luaA_indexAcceptable(L, index); | ||
163 | type = (o == NULL) ? "no value" : luaT_typename(G(L), o); | ||
164 | lua_unlock(L); | ||
165 | return type; | ||
166 | } | 161 | } |
167 | 162 | ||
168 | 163 | ||
@@ -180,22 +175,11 @@ LUA_API int lua_isnumber (lua_State *L, int index) { | |||
180 | 175 | ||
181 | 176 | ||
182 | LUA_API int lua_isstring (lua_State *L, int index) { | 177 | LUA_API int lua_isstring (lua_State *L, int index) { |
183 | int t = lua_rawtag(L, index); | 178 | int t = lua_type(L, index); |
184 | return (t == LUA_TSTRING || t == LUA_TNUMBER); | 179 | return (t == LUA_TSTRING || t == LUA_TNUMBER); |
185 | } | 180 | } |
186 | 181 | ||
187 | 182 | ||
188 | LUA_API int lua_tag (lua_State *L, int index) { | ||
189 | StkId o; | ||
190 | int i; | ||
191 | lua_lock(L); /* other thread could be changing the tag */ | ||
192 | o = luaA_indexAcceptable(L, index); | ||
193 | i = (o == NULL) ? LUA_NOTAG : luaT_tag(o); | ||
194 | lua_unlock(L); | ||
195 | return i; | ||
196 | } | ||
197 | |||
198 | |||
199 | LUA_API int lua_equal (lua_State *L, int index1, int index2) { | 183 | LUA_API int lua_equal (lua_State *L, int index1, int index2) { |
200 | StkId o1 = luaA_indexAcceptable(L, index1); | 184 | StkId o1 = luaA_indexAcceptable(L, index1); |
201 | StkId o2 = luaA_indexAcceptable(L, index2); | 185 | StkId o2 = luaA_indexAcceptable(L, index2); |
@@ -346,8 +330,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | |||
346 | 330 | ||
347 | 331 | ||
348 | LUA_API void lua_getglobal (lua_State *L, const char *name) { | 332 | LUA_API void lua_getglobal (lua_State *L, const char *name) { |
333 | TObject o; | ||
349 | lua_lock(L); | 334 | lua_lock(L); |
350 | luaV_getglobal(L, luaS_new(L, name), L->top); | 335 | setsvalue(&o, luaS_new(L, name)); |
336 | luaV_gettable(L, gt(L), &o, L->top); | ||
351 | api_incr_top(L); | 337 | api_incr_top(L); |
352 | lua_unlock(L); | 338 | lua_unlock(L); |
353 | } | 339 | } |
@@ -391,6 +377,29 @@ LUA_API void lua_newtable (lua_State *L) { | |||
391 | } | 377 | } |
392 | 378 | ||
393 | 379 | ||
380 | LUA_API void lua_geteventtable (lua_State *L, int objindex) { | ||
381 | StkId obj; | ||
382 | Table *et; | ||
383 | lua_lock(L); | ||
384 | obj = luaA_indexAcceptable(L, objindex); | ||
385 | switch (ttype(obj)) { | ||
386 | case LUA_TTABLE: | ||
387 | et = hvalue(obj)->eventtable; | ||
388 | break; | ||
389 | case LUA_TUSERDATA: | ||
390 | et = uvalue(obj)->uv.eventtable; | ||
391 | break; | ||
392 | default: | ||
393 | et = hvalue(defaultet(L)); | ||
394 | } | ||
395 | if (et == hvalue(defaultet(L))) | ||
396 | setnilvalue(L->top); | ||
397 | else | ||
398 | sethvalue(L->top, et); | ||
399 | api_incr_top(L); | ||
400 | lua_unlock(L); | ||
401 | } | ||
402 | |||
394 | 403 | ||
395 | /* | 404 | /* |
396 | ** set functions (stack -> Lua) | 405 | ** set functions (stack -> Lua) |
@@ -398,9 +407,11 @@ LUA_API void lua_newtable (lua_State *L) { | |||
398 | 407 | ||
399 | 408 | ||
400 | LUA_API void lua_setglobal (lua_State *L, const char *name) { | 409 | LUA_API void lua_setglobal (lua_State *L, const char *name) { |
410 | TObject o; | ||
401 | lua_lock(L); | 411 | lua_lock(L); |
402 | api_checknelems(L, 1); | 412 | api_checknelems(L, 1); |
403 | luaV_setglobal(L, luaS_new(L, name), L->top - 1); | 413 | setsvalue(&o, luaS_new(L, name)); |
414 | luaV_settable(L, gt(L), &o, L->top - 1); | ||
404 | L->top--; /* remove element from the top */ | 415 | L->top--; /* remove element from the top */ |
405 | lua_unlock(L); | 416 | lua_unlock(L); |
406 | } | 417 | } |
@@ -447,11 +458,32 @@ LUA_API void lua_setglobals (lua_State *L) { | |||
447 | api_checknelems(L, 1); | 458 | api_checknelems(L, 1); |
448 | newtable = --L->top; | 459 | newtable = --L->top; |
449 | api_check(L, ttype(newtable) == LUA_TTABLE); | 460 | api_check(L, ttype(newtable) == LUA_TTABLE); |
450 | setobj(&L->gt, newtable); | 461 | setobj(gt(L), newtable); |
451 | lua_unlock(L); | 462 | lua_unlock(L); |
452 | } | 463 | } |
453 | 464 | ||
454 | 465 | ||
466 | LUA_API void lua_seteventtable (lua_State *L, int objindex) { | ||
467 | StkId obj, et; | ||
468 | lua_lock(L); | ||
469 | api_checknelems(L, 1); | ||
470 | obj = luaA_indexAcceptable(L, objindex); | ||
471 | et = --L->top; | ||
472 | api_check(L, ttype(et) == LUA_TTABLE); | ||
473 | switch (ttype(obj)) { | ||
474 | case LUA_TTABLE: | ||
475 | hvalue(obj)->eventtable = hvalue(et); | ||
476 | break; | ||
477 | case LUA_TUSERDATA: | ||
478 | uvalue(obj)->uv.eventtable = hvalue(et); | ||
479 | break; | ||
480 | default: | ||
481 | luaO_verror(L, "cannot change the event table of a %.20s", | ||
482 | luaT_typenames[ttype(obj)]); | ||
483 | } | ||
484 | lua_unlock(L); | ||
485 | } | ||
486 | |||
455 | 487 | ||
456 | /* | 488 | /* |
457 | ** `do' functions (run Lua code) | 489 | ** `do' functions (run Lua code) |
@@ -533,70 +565,6 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) { | |||
533 | ** miscellaneous functions | 565 | ** miscellaneous functions |
534 | */ | 566 | */ |
535 | 567 | ||
536 | LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) { | ||
537 | int tag; | ||
538 | lua_lock(L); | ||
539 | if (basictype != LUA_TNONE && | ||
540 | basictype != LUA_TTABLE && | ||
541 | basictype != LUA_TUSERDATA) | ||
542 | luaO_verror(L, "invalid basic type (%d) for new type", basictype); | ||
543 | tag = luaT_newtag(L, name, basictype); | ||
544 | if (tag == LUA_TNONE) | ||
545 | luaO_verror(L, "type name '%.30s' already exists", name); | ||
546 | lua_unlock(L); | ||
547 | return tag; | ||
548 | } | ||
549 | |||
550 | |||
551 | LUA_API int lua_name2tag (lua_State *L, const char *name) { | ||
552 | int tag; | ||
553 | const TObject *v; | ||
554 | lua_lock(L); | ||
555 | v = luaH_getstr(G(L)->type2tag, luaS_new(L, name)); | ||
556 | if (ttype(v) == LUA_TNIL) | ||
557 | tag = LUA_TNONE; | ||
558 | else { | ||
559 | lua_assert(ttype(v) == LUA_TNUMBER); | ||
560 | tag = cast(int, nvalue(v)); | ||
561 | } | ||
562 | lua_unlock(L); | ||
563 | return tag; | ||
564 | } | ||
565 | |||
566 | |||
567 | LUA_API const char *lua_tag2name (lua_State *L, int tag) { | ||
568 | const char *s; | ||
569 | lua_lock(L); | ||
570 | s = (tag == LUA_TNONE) ? "no value" : typenamebytag(G(L), tag); | ||
571 | lua_unlock(L); | ||
572 | return s; | ||
573 | } | ||
574 | |||
575 | |||
576 | LUA_API void lua_settag (lua_State *L, int tag) { | ||
577 | int basictype; | ||
578 | lua_lock(L); | ||
579 | api_checknelems(L, 1); | ||
580 | if (tag < 0 || tag >= G(L)->ntag) | ||
581 | luaO_verror(L, "%d is not a valid tag", tag); | ||
582 | basictype = G(L)->TMtable[tag].basictype; | ||
583 | if (basictype != LUA_TNONE && basictype != ttype(L->top-1)) | ||
584 | luaO_verror(L, "tag %d can only be used for type '%.20s'", tag, | ||
585 | typenamebytag(G(L), basictype)); | ||
586 | switch (ttype(L->top-1)) { | ||
587 | case LUA_TTABLE: | ||
588 | hvalue(L->top-1)->htag = tag; | ||
589 | break; | ||
590 | case LUA_TUSERDATA: | ||
591 | uvalue(L->top-1)->uv.tag = tag; | ||
592 | break; | ||
593 | default: | ||
594 | luaO_verror(L, "cannot change the tag of a %.20s", | ||
595 | luaT_typename(G(L), L->top-1)); | ||
596 | } | ||
597 | lua_unlock(L); | ||
598 | } | ||
599 | |||
600 | 568 | ||
601 | LUA_API void lua_error (lua_State *L, const char *s) { | 569 | LUA_API void lua_error (lua_State *L, const char *s) { |
602 | lua_lock(L); | 570 | lua_lock(L); |