diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-13 14:17:53 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-13 14:17:53 -0200 |
commit | a4ae1475e354ff61eda8fe64745de42526a4c53e (patch) | |
tree | fff03f87c1a792003cce408fe077dc8d5475dd0b /lapi.c | |
parent | b3da4ee90753bcb6d7225e0e737ee692df669fb1 (diff) | |
download | lua-a4ae1475e354ff61eda8fe64745de42526a4c53e.tar.gz lua-a4ae1475e354ff61eda8fe64745de42526a4c53e.tar.bz2 lua-a4ae1475e354ff61eda8fe64745de42526a4c53e.zip |
details (smaller code)
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 64 |
1 files changed, 28 insertions, 36 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.127 2001/02/09 19:53:16 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.128 2001/02/12 15:42: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 | */ |
@@ -31,33 +31,35 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" | |||
31 | #define api_check(L, o) /* nothing */ | 31 | #define api_check(L, o) /* nothing */ |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | /* valid indices */ | ||
35 | #define api_checkindex(L, i) \ | ||
36 | api_check(L, (0 < abs(i)) && abs(i) <= (L->top - L->Cbase)) | ||
37 | |||
38 | #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->Cbase)) | 34 | #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->Cbase)) |
39 | 35 | ||
40 | |||
41 | |||
42 | #define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i))) | ||
43 | |||
44 | #define api_incr_top(L) incr_top | 36 | #define api_incr_top(L) incr_top |
45 | 37 | ||
46 | 38 | ||
47 | 39 | ||
48 | TObject *luaA_index (lua_State *L, int index) { | 40 | TObject *luaA_index (lua_State *L, int index) { |
49 | return Index(L, index); | 41 | if (index > 0) { |
42 | api_check(L, index <= L->top - L->Cbase); | ||
43 | return L->Cbase+index-1; | ||
44 | } | ||
45 | else { | ||
46 | api_check(L, index != 0 && -index <= L->top - L->Cbase); | ||
47 | return L->top+index; | ||
48 | } | ||
50 | } | 49 | } |
51 | 50 | ||
52 | 51 | ||
53 | static TObject *luaA_indexAcceptable (lua_State *L, int index) { | 52 | static TObject *luaA_indexAcceptable (lua_State *L, int index) { |
54 | api_check(L, (0 < abs(index)) && abs(index) <= (L->stack_last - L->Cbase)); | 53 | if (index > 0) { |
55 | if (index >= 0) { | ||
56 | TObject *o = L->Cbase+(index-1); | 54 | TObject *o = L->Cbase+(index-1); |
55 | api_check(L, index <= L->stack_last - L->Cbase); | ||
57 | if (o >= L->top) return NULL; | 56 | if (o >= L->top) return NULL; |
58 | else return o; | 57 | else return o; |
59 | } | 58 | } |
60 | else return L->top+index; | 59 | else { |
60 | api_check(L, index != 0 && -index <= L->top - L->Cbase); | ||
61 | return L->top+index; | ||
62 | } | ||
61 | } | 63 | } |
62 | 64 | ||
63 | 65 | ||
@@ -105,8 +107,7 @@ LUA_API void lua_settop (lua_State *L, int index) { | |||
105 | LUA_API void lua_remove (lua_State *L, int index) { | 107 | LUA_API void lua_remove (lua_State *L, int index) { |
106 | StkId p; | 108 | StkId p; |
107 | LUA_LOCK(L); | 109 | LUA_LOCK(L); |
108 | api_checkindex(L, index); | 110 | p = luaA_index(L, index); |
109 | p = Index(L, index); | ||
110 | while (++p < L->top) setobj(p-1, p); | 111 | while (++p < L->top) setobj(p-1, p); |
111 | L->top--; | 112 | L->top--; |
112 | LUA_UNLOCK(L); | 113 | LUA_UNLOCK(L); |
@@ -117,8 +118,7 @@ LUA_API void lua_insert (lua_State *L, int index) { | |||
117 | StkId p; | 118 | StkId p; |
118 | StkId q; | 119 | StkId q; |
119 | LUA_LOCK(L); | 120 | LUA_LOCK(L); |
120 | api_checkindex(L, index); | 121 | p = luaA_index(L, index); |
121 | p = Index(L, index); | ||
122 | for (q = L->top; q>p; q--) setobj(q, q-1); | 122 | for (q = L->top; q>p; q--) setobj(q, q-1); |
123 | setobj(p, L->top); | 123 | setobj(p, L->top); |
124 | LUA_UNLOCK(L); | 124 | LUA_UNLOCK(L); |
@@ -127,8 +127,7 @@ LUA_API void lua_insert (lua_State *L, int index) { | |||
127 | 127 | ||
128 | LUA_API void lua_pushvalue (lua_State *L, int index) { | 128 | LUA_API void lua_pushvalue (lua_State *L, int index) { |
129 | LUA_LOCK(L); | 129 | LUA_LOCK(L); |
130 | api_checkindex(L, index); | 130 | setobj(L->top, luaA_index(L, index)); |
131 | setobj(L->top, Index(L, index)); | ||
132 | api_incr_top(L); | 131 | api_incr_top(L); |
133 | LUA_UNLOCK(L); | 132 | LUA_UNLOCK(L); |
134 | } | 133 | } |
@@ -381,8 +380,7 @@ LUA_API void lua_getglobal (lua_State *L, const char *name) { | |||
381 | LUA_API void lua_gettable (lua_State *L, int index) { | 380 | LUA_API void lua_gettable (lua_State *L, int index) { |
382 | StkId t; | 381 | StkId t; |
383 | LUA_LOCK(L); | 382 | LUA_LOCK(L); |
384 | api_checkindex(L, index); | 383 | t = luaA_index(L, index); |
385 | t = Index(L, index); | ||
386 | luaV_gettable(L, t, L->top-1, L->top-1); | 384 | luaV_gettable(L, t, L->top-1, L->top-1); |
387 | LUA_UNLOCK(L); | 385 | LUA_UNLOCK(L); |
388 | } | 386 | } |
@@ -391,8 +389,7 @@ LUA_API void lua_gettable (lua_State *L, int index) { | |||
391 | LUA_API void lua_rawget (lua_State *L, int index) { | 389 | LUA_API void lua_rawget (lua_State *L, int index) { |
392 | StkId t; | 390 | StkId t; |
393 | LUA_LOCK(L); | 391 | LUA_LOCK(L); |
394 | api_checkindex(L, index); | 392 | t = luaA_index(L, index); |
395 | t = Index(L, index); | ||
396 | api_check(L, ttype(t) == LUA_TTABLE); | 393 | api_check(L, ttype(t) == LUA_TTABLE); |
397 | setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1)); | 394 | setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1)); |
398 | LUA_UNLOCK(L); | 395 | LUA_UNLOCK(L); |
@@ -402,8 +399,7 @@ LUA_API void lua_rawget (lua_State *L, int index) { | |||
402 | LUA_API void lua_rawgeti (lua_State *L, int index, int n) { | 399 | LUA_API void lua_rawgeti (lua_State *L, int index, int n) { |
403 | StkId o; | 400 | StkId o; |
404 | LUA_LOCK(L); | 401 | LUA_LOCK(L); |
405 | api_checkindex(L, index); | 402 | o = luaA_index(L, index); |
406 | o = Index(L, index); | ||
407 | api_check(L, ttype(o) == LUA_TTABLE); | 403 | api_check(L, ttype(o) == LUA_TTABLE); |
408 | setobj(L->top, luaH_getnum(hvalue(o), n)); | 404 | setobj(L->top, luaH_getnum(hvalue(o), n)); |
409 | api_incr_top(L); | 405 | api_incr_top(L); |
@@ -467,8 +463,7 @@ LUA_API void lua_settable (lua_State *L, int index) { | |||
467 | StkId t; | 463 | StkId t; |
468 | LUA_LOCK(L); | 464 | LUA_LOCK(L); |
469 | api_checknelems(L, 2); | 465 | api_checknelems(L, 2); |
470 | api_checkindex(L, index); | 466 | t = luaA_index(L, index); |
471 | t = Index(L, index); | ||
472 | luaV_settable(L, t, L->top - 2, L->top - 1); | 467 | luaV_settable(L, t, L->top - 2, L->top - 1); |
473 | L->top -= 2; /* pop index and value */ | 468 | L->top -= 2; /* pop index and value */ |
474 | LUA_UNLOCK(L); | 469 | LUA_UNLOCK(L); |
@@ -479,8 +474,7 @@ LUA_API void lua_rawset (lua_State *L, int index) { | |||
479 | StkId t; | 474 | StkId t; |
480 | LUA_LOCK(L); | 475 | LUA_LOCK(L); |
481 | api_checknelems(L, 2); | 476 | api_checknelems(L, 2); |
482 | api_checkindex(L, index); | 477 | t = luaA_index(L, index); |
483 | t = Index(L, index); | ||
484 | api_check(L, ttype(t) == LUA_TTABLE); | 478 | api_check(L, ttype(t) == LUA_TTABLE); |
485 | setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1)); | 479 | setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1)); |
486 | L->top -= 2; | 480 | L->top -= 2; |
@@ -492,8 +486,7 @@ LUA_API void lua_rawseti (lua_State *L, int index, int n) { | |||
492 | StkId o; | 486 | StkId o; |
493 | LUA_LOCK(L); | 487 | LUA_LOCK(L); |
494 | api_checknelems(L, 1); | 488 | api_checknelems(L, 1); |
495 | api_checkindex(L, index); | 489 | o = luaA_index(L, index); |
496 | o = Index(L, index); | ||
497 | api_check(L, ttype(o) == LUA_TTABLE); | 490 | api_check(L, ttype(o) == LUA_TTABLE); |
498 | setobj(luaH_setnum(L, hvalue(o), n), (L->top-1)); | 491 | setobj(luaH_setnum(L, hvalue(o), n), (L->top-1)); |
499 | L->top--; | 492 | L->top--; |
@@ -668,10 +661,9 @@ LUA_API int lua_next (lua_State *L, int index) { | |||
668 | Node *n; | 661 | Node *n; |
669 | int more; | 662 | int more; |
670 | LUA_LOCK(L); | 663 | LUA_LOCK(L); |
671 | api_checkindex(L, index); | 664 | t = luaA_index(L, index); |
672 | t = Index(L, index); | ||
673 | api_check(L, ttype(t) == LUA_TTABLE); | 665 | api_check(L, ttype(t) == LUA_TTABLE); |
674 | n = luaH_next(L, hvalue(t), Index(L, -1)); | 666 | n = luaH_next(L, hvalue(t), luaA_index(L, -1)); |
675 | if (n) { | 667 | if (n) { |
676 | setkey2obj(L->top-1, n); | 668 | setkey2obj(L->top-1, n); |
677 | setobj(L->top, val(n)); | 669 | setobj(L->top, val(n)); |
@@ -692,8 +684,7 @@ LUA_API int lua_getn (lua_State *L, int index) { | |||
692 | const TObject *value; | 684 | const TObject *value; |
693 | int n; | 685 | int n; |
694 | LUA_LOCK(L); | 686 | LUA_LOCK(L); |
695 | api_checkindex(L, index); | 687 | h = hvalue(luaA_index(L, index)); |
696 | h = hvalue(Index(L, index)); | ||
697 | value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */ | 688 | value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */ |
698 | if (ttype(value) == LUA_TNUMBER) | 689 | if (ttype(value) == LUA_TNUMBER) |
699 | n = (int)nvalue(value); | 690 | n = (int)nvalue(value); |
@@ -718,6 +709,7 @@ LUA_API int lua_getn (lua_State *L, int index) { | |||
718 | LUA_API void lua_concat (lua_State *L, int n) { | 709 | LUA_API void lua_concat (lua_State *L, int n) { |
719 | StkId top; | 710 | StkId top; |
720 | LUA_LOCK(L); | 711 | LUA_LOCK(L); |
712 | api_check(L, n >= 2); | ||
721 | api_checknelems(L, n); | 713 | api_checknelems(L, n); |
722 | top = L->top; | 714 | top = L->top; |
723 | luaV_strconc(L, n, top); | 715 | luaV_strconc(L, n, top); |