diff options
| -rw-r--r-- | lapi.c | 97 | ||||
| -rw-r--r-- | ltests.c | 41 |
2 files changed, 93 insertions, 45 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.104 2000/10/03 14:27:44 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.105 2000/10/05 12:14:08 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 | */ |
| @@ -39,6 +39,16 @@ TObject *luaA_index (lua_State *L, int index) { | |||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | static TObject *luaA_indexAcceptable (lua_State *L, int index) { | ||
| 43 | if (index >= 0) { | ||
| 44 | TObject *o = L->Cbase+(index-1); | ||
| 45 | if (o >= L->top) return NULL; | ||
| 46 | else return o; | ||
| 47 | } | ||
| 48 | else return L->top+index; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 42 | void luaA_pushobject (lua_State *L, const TObject *o) { | 52 | void luaA_pushobject (lua_State *L, const TObject *o) { |
| 43 | *L->top = *o; | 53 | *L->top = *o; |
| 44 | incr_top; | 54 | incr_top; |
| @@ -69,14 +79,14 @@ void lua_settop (lua_State *L, int index) { | |||
| 69 | 79 | ||
| 70 | 80 | ||
| 71 | void lua_remove (lua_State *L, int index) { | 81 | void lua_remove (lua_State *L, int index) { |
| 72 | StkId p = Index(L, index); | 82 | StkId p = luaA_index(L, index); |
| 73 | while (++p < L->top) *(p-1) = *p; | 83 | while (++p < L->top) *(p-1) = *p; |
| 74 | L->top--; | 84 | L->top--; |
| 75 | } | 85 | } |
| 76 | 86 | ||
| 77 | 87 | ||
| 78 | void lua_insert (lua_State *L, int index) { | 88 | void lua_insert (lua_State *L, int index) { |
| 79 | StkId p = Index(L, index); | 89 | StkId p = luaA_index(L, index); |
| 80 | StkId q; | 90 | StkId q; |
| 81 | for (q = L->top; q>p; q--) | 91 | for (q = L->top; q>p; q--) |
| 82 | *q = *(q-1); | 92 | *q = *(q-1); |
| @@ -85,7 +95,7 @@ void lua_insert (lua_State *L, int index) { | |||
| 85 | 95 | ||
| 86 | 96 | ||
| 87 | void lua_pushvalue (lua_State *L, int index) { | 97 | void lua_pushvalue (lua_State *L, int index) { |
| 88 | *L->top = *Index(L, index); | 98 | *L->top = *luaA_index(L, index); |
| 89 | api_incr_top(L); | 99 | api_incr_top(L); |
| 90 | } | 100 | } |
| 91 | 101 | ||
| @@ -96,28 +106,10 @@ void lua_pushvalue (lua_State *L, int index) { | |||
| 96 | */ | 106 | */ |
| 97 | 107 | ||
| 98 | 108 | ||
| 99 | #define btest(L,i,value,default) { \ | ||
| 100 | StkId o; \ | ||
| 101 | if ((i) >= 0) { \ | ||
| 102 | o = L->Cbase+((i)-1); \ | ||
| 103 | if (o >= L->top) return (default); \ | ||
| 104 | } \ | ||
| 105 | else o = L->top+(i); \ | ||
| 106 | return (value); } | ||
| 107 | |||
| 108 | |||
| 109 | #define access(L,i,test,default,value) { \ | ||
| 110 | StkId o; \ | ||
| 111 | if ((i) >= 0) { \ | ||
| 112 | o = L->Cbase+((i)-1); \ | ||
| 113 | if (o >= L->top) return (default); \ | ||
| 114 | } \ | ||
| 115 | else o = L->top+(i); \ | ||
| 116 | return ((test) ? (value) : (default)); } | ||
| 117 | |||
| 118 | |||
| 119 | int lua_type (lua_State *L, int index) { | 109 | int lua_type (lua_State *L, int index) { |
| 120 | btest(L, index, ttype(o), LUA_TNONE); | 110 | StkId o = luaA_indexAcceptable(L, index); |
| 111 | if (o == NULL) return LUA_TNONE; | ||
| 112 | else return ttype(o); | ||
| 121 | } | 113 | } |
| 122 | 114 | ||
| 123 | const char *lua_typename (lua_State *L, int t) { | 115 | const char *lua_typename (lua_State *L, int t) { |
| @@ -127,11 +119,15 @@ const char *lua_typename (lua_State *L, int t) { | |||
| 127 | 119 | ||
| 128 | 120 | ||
| 129 | int lua_iscfunction (lua_State *L, int index) { | 121 | int lua_iscfunction (lua_State *L, int index) { |
| 130 | btest(L, index, iscfunction(o), 0); | 122 | StkId o = luaA_indexAcceptable(L, index); |
| 123 | if (o == NULL) return 0; | ||
| 124 | else return iscfunction(o); | ||
| 131 | } | 125 | } |
| 132 | 126 | ||
| 133 | int lua_isnumber (lua_State *L, int index) { | 127 | int lua_isnumber (lua_State *L, int index) { |
| 134 | btest(L, index, (tonumber(Index(L, index)) == 0), 0); | 128 | TObject *o = luaA_indexAcceptable(L, index); |
| 129 | if (o == NULL) return 0; | ||
| 130 | else return (tonumber(o) == 0); | ||
| 135 | } | 131 | } |
| 136 | 132 | ||
| 137 | int lua_isstring (lua_State *L, int index) { | 133 | int lua_isstring (lua_State *L, int index) { |
| @@ -141,47 +137,60 @@ int lua_isstring (lua_State *L, int index) { | |||
| 141 | 137 | ||
| 142 | 138 | ||
| 143 | int lua_tag (lua_State *L, int index) { | 139 | int lua_tag (lua_State *L, int index) { |
| 144 | btest(L, index, luaT_tag(o), LUA_NOTAG); | 140 | StkId o = luaA_indexAcceptable(L, index); |
| 141 | if (o == NULL) return LUA_NOTAG; | ||
| 142 | else return luaT_tag(o); | ||
| 145 | } | 143 | } |
| 146 | 144 | ||
| 147 | int lua_equal (lua_State *L, int index1, int index2) { | 145 | int lua_equal (lua_State *L, int index1, int index2) { |
| 148 | StkId o1 = Index(L, index1); | 146 | StkId o1 = luaA_indexAcceptable(L, index1); |
| 149 | StkId o2 = Index(L, index2); | 147 | StkId o2 = luaA_indexAcceptable(L, index2); |
| 150 | if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */ | 148 | if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */ |
| 151 | else return luaO_equalObj(o1, o2); | 149 | else return luaO_equalObj(o1, o2); |
| 152 | } | 150 | } |
| 153 | 151 | ||
| 154 | int lua_lessthan (lua_State *L, int index1, int index2) { | 152 | int lua_lessthan (lua_State *L, int index1, int index2) { |
| 155 | StkId o1 = Index(L, index1); | 153 | StkId o1 = luaA_indexAcceptable(L, index1); |
| 156 | StkId o2 = Index(L, index2); | 154 | StkId o2 = luaA_indexAcceptable(L, index2); |
| 157 | if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */ | 155 | if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */ |
| 158 | else return luaV_lessthan(L, o1, o2, L->top); | 156 | else return luaV_lessthan(L, o1, o2, L->top); |
| 159 | } | 157 | } |
| 160 | 158 | ||
| 161 | 159 | ||
| 162 | 160 | ||
| 163 | double lua_tonumber (lua_State *L, int index) { | 161 | double lua_tonumber (lua_State *L, int index) { |
| 164 | access(L, index, (tonumber(o) == 0), 0, nvalue(o)); | 162 | StkId o = luaA_indexAcceptable(L, index); |
| 163 | if (o == NULL || tonumber(o)) return 0; | ||
| 164 | else return nvalue(o); | ||
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | const char *lua_tostring (lua_State *L, int index) { | 167 | const char *lua_tostring (lua_State *L, int index) { |
| 168 | access(L, index, (tostring(L, o) == 0), NULL, svalue(o)); | 168 | StkId o = luaA_indexAcceptable(L, index); |
| 169 | if (o == NULL || tostring(L, o)) return NULL; | ||
| 170 | else return svalue(o); | ||
| 169 | } | 171 | } |
| 170 | 172 | ||
| 171 | size_t lua_strlen (lua_State *L, int index) { | 173 | size_t lua_strlen (lua_State *L, int index) { |
| 172 | access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len); | 174 | StkId o = luaA_indexAcceptable(L, index); |
| 175 | if (o == NULL || tostring(L, o)) return 0; | ||
| 176 | else return tsvalue(o)->u.s.len; | ||
| 173 | } | 177 | } |
| 174 | 178 | ||
| 175 | lua_CFunction lua_tocfunction (lua_State *L, int index) { | 179 | lua_CFunction lua_tocfunction (lua_State *L, int index) { |
| 176 | access(L, index, iscfunction(o), NULL, clvalue(o)->f.c); | 180 | StkId o = luaA_indexAcceptable(L, index); |
| 181 | if (o == NULL || !iscfunction(o)) return NULL; | ||
| 182 | else return clvalue(o)->f.c; | ||
| 177 | } | 183 | } |
| 178 | 184 | ||
| 179 | void *lua_touserdata (lua_State *L, int index) { | 185 | void *lua_touserdata (lua_State *L, int index) { |
| 180 | access(L, index, (ttype(o) == LUA_TUSERDATA), NULL, tsvalue(o)->u.d.value); | 186 | StkId o = luaA_indexAcceptable(L, index); |
| 187 | if (o == NULL || ttype(o) != LUA_TUSERDATA) return NULL; | ||
| 188 | else return tsvalue(o)->u.d.value; | ||
| 181 | } | 189 | } |
| 182 | 190 | ||
| 183 | const void *lua_topointer (lua_State *L, int index) { | 191 | const void *lua_topointer (lua_State *L, int index) { |
| 184 | StkId o = Index(L, index); | 192 | StkId o = luaA_indexAcceptable(L, index); |
| 193 | if (o == NULL) return NULL; | ||
| 185 | switch (ttype(o)) { | 194 | switch (ttype(o)) { |
| 186 | case LUA_TTABLE: | 195 | case LUA_TTABLE: |
| 187 | return hvalue(o); | 196 | return hvalue(o); |
| @@ -205,8 +214,8 @@ void lua_pushnil (lua_State *L) { | |||
| 205 | 214 | ||
| 206 | 215 | ||
| 207 | void lua_pushnumber (lua_State *L, double n) { | 216 | void lua_pushnumber (lua_State *L, double n) { |
| 208 | ttype(L->top) = LUA_TNUMBER; | ||
| 209 | nvalue(L->top) = n; | 217 | nvalue(L->top) = n; |
| 218 | ttype(L->top) = LUA_TNUMBER; | ||
| 210 | api_incr_top(L); | 219 | api_incr_top(L); |
| 211 | } | 220 | } |
| 212 | 221 | ||
| @@ -437,10 +446,10 @@ void lua_unref (lua_State *L, int ref) { | |||
| 437 | 446 | ||
| 438 | 447 | ||
| 439 | int lua_next (lua_State *L, int index) { | 448 | int lua_next (lua_State *L, int index) { |
| 440 | StkId t = Index(L, index); | 449 | StkId t = luaA_index(L, index); |
| 441 | Node *n; | 450 | Node *n; |
| 442 | LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); | 451 | LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); |
| 443 | n = luaH_next(L, hvalue(t), Index(L, -1)); | 452 | n = luaH_next(L, hvalue(t), luaA_index(L, -1)); |
| 444 | if (n) { | 453 | if (n) { |
| 445 | *(L->top-1) = *key(n); | 454 | *(L->top-1) = *key(n); |
| 446 | *L->top = *val(n); | 455 | *L->top = *val(n); |
| @@ -455,7 +464,7 @@ int lua_next (lua_State *L, int index) { | |||
| 455 | 464 | ||
| 456 | 465 | ||
| 457 | int lua_getn (lua_State *L, int index) { | 466 | int lua_getn (lua_State *L, int index) { |
| 458 | Hash *h = hvalue(Index(L, index)); | 467 | Hash *h = hvalue(luaA_index(L, index)); |
| 459 | const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ | 468 | const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ |
| 460 | if (ttype(value) == LUA_TNUMBER) | 469 | if (ttype(value) == LUA_TNUMBER) |
| 461 | return (int)nvalue(value); | 470 | return (int)nvalue(value); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 1.48 2000/10/05 12:14:08 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 1.49 2000/10/05 13:00:17 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -389,6 +389,45 @@ static int testC (lua_State *L) { | |||
| 389 | for (;;) { | 389 | for (;;) { |
| 390 | const char *inst = getname; | 390 | const char *inst = getname; |
| 391 | if EQ("") return 0; | 391 | if EQ("") return 0; |
| 392 | else if EQ("isnumber") { | ||
| 393 | lua_pushnumber(L, lua_isnumber(L, getnum)); | ||
| 394 | } | ||
| 395 | else if EQ("isstring") { | ||
| 396 | lua_pushnumber(L, lua_isstring(L, getnum)); | ||
| 397 | } | ||
| 398 | else if EQ("istable") { | ||
| 399 | lua_pushnumber(L, lua_istable(L, getnum)); | ||
| 400 | } | ||
| 401 | else if EQ("iscfunction") { | ||
| 402 | lua_pushnumber(L, lua_iscfunction(L, getnum)); | ||
| 403 | } | ||
| 404 | else if EQ("isfunction") { | ||
| 405 | lua_pushnumber(L, lua_isfunction(L, getnum)); | ||
| 406 | } | ||
| 407 | else if EQ("isuserdata") { | ||
| 408 | lua_pushnumber(L, lua_isuserdata(L, getnum)); | ||
| 409 | } | ||
| 410 | else if EQ("isnil") { | ||
| 411 | lua_pushnumber(L, lua_isnil(L, getnum)); | ||
| 412 | } | ||
| 413 | else if EQ("isnull") { | ||
| 414 | lua_pushnumber(L, lua_isnull(L, getnum)); | ||
| 415 | } | ||
| 416 | else if EQ("tonumber") { | ||
| 417 | lua_pushnumber(L, lua_tonumber(L, getnum)); | ||
| 418 | } | ||
| 419 | else if EQ("tostring") { | ||
| 420 | lua_pushstring(L, lua_tostring(L, getnum)); | ||
| 421 | } | ||
| 422 | else if EQ("tonumber") { | ||
| 423 | lua_pushnumber(L, lua_tonumber(L, getnum)); | ||
| 424 | } | ||
| 425 | else if EQ("strlen") { | ||
| 426 | lua_pushnumber(L, lua_strlen(L, getnum)); | ||
| 427 | } | ||
| 428 | else if EQ("tocfunction") { | ||
| 429 | lua_pushcfunction(L, lua_tocfunction(L, getnum)); | ||
| 430 | } | ||
| 392 | else if EQ("return") { | 431 | else if EQ("return") { |
| 393 | return getnum; | 432 | return getnum; |
| 394 | } | 433 | } |
