diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-05 16:33:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-05 16:33:32 -0300 |
| commit | 6e80c1cde193b767d63d2cc30ebd71d65512e061 (patch) | |
| tree | cb599bdc956c0dc9b3d469bb01de47185db3e4e2 /lapi.c | |
| parent | f67f324377aff66d78479eaaffbb94a6b092ae45 (diff) | |
| download | lua-6e80c1cde193b767d63d2cc30ebd71d65512e061.tar.gz lua-6e80c1cde193b767d63d2cc30ebd71d65512e061.tar.bz2 lua-6e80c1cde193b767d63d2cc30ebd71d65512e061.zip | |
new version for API
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 118 |
1 files changed, 69 insertions, 49 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.92 2000/08/31 20:23:40 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.93 2000/08/31 21:01:43 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 | */ |
| @@ -69,25 +69,24 @@ void lua_settop (lua_State *L, int index) { | |||
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | void lua_move (lua_State *L, int index) { | 72 | void lua_remove (lua_State *L, int index) { |
| 73 | TObject *p = Index(L, index); | 73 | StkId p = Index(L, index); |
| 74 | TObject temp = *p; | ||
| 75 | while (++p < L->top) *(p-1) = *p; | 74 | while (++p < L->top) *(p-1) = *p; |
| 76 | *(L->top-1) = temp; | 75 | L->top--; |
| 77 | } | 76 | } |
| 78 | 77 | ||
| 79 | 78 | ||
| 80 | void lua_insert (lua_State *L, int index) { | 79 | void lua_insert (lua_State *L, int index) { |
| 81 | TObject temp = *(L->top-1); | 80 | TObject temp = *(L->top-1); |
| 82 | TObject *p = Index(L, index); | 81 | StkId p = Index(L, index); |
| 83 | TObject *q; | 82 | StkId q; |
| 84 | for (q = L->top-1; q>p; q--) | 83 | for (q = L->top-1; q>p; q--) |
| 85 | *q = *(q-1); | 84 | *q = *(q-1); |
| 86 | *p = temp; | 85 | *p = temp; |
| 87 | } | 86 | } |
| 88 | 87 | ||
| 89 | 88 | ||
| 90 | void lua_pushobject (lua_State *L, int index) { | 89 | void lua_pushvalue (lua_State *L, int index) { |
| 91 | *L->top = *Index(L, index); | 90 | *L->top = *Index(L, index); |
| 92 | api_incr_top(L); | 91 | api_incr_top(L); |
| 93 | } | 92 | } |
| @@ -133,17 +132,24 @@ int lua_isnumber (lua_State *L, int index) { | |||
| 133 | 132 | ||
| 134 | int lua_tag (lua_State *L, int index) { | 133 | int lua_tag (lua_State *L, int index) { |
| 135 | btest(L, index, | 134 | btest(L, index, |
| 136 | ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag : luaT_effectivetag(L, o)), | 135 | ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag : |
| 137 | -1); | 136 | luaT_effectivetag(L, o)), -1); |
| 138 | } | 137 | } |
| 139 | 138 | ||
| 140 | int lua_equal(lua_State *L, int index1, int index2) { | 139 | int lua_equal (lua_State *L, int index1, int index2) { |
| 141 | StkId o1 = Index(L, index1); | 140 | StkId o1 = Index(L, index1); |
| 142 | StkId o2 = Index(L, index2); | 141 | StkId o2 = Index(L, index2); |
| 143 | if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */ | 142 | if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */ |
| 144 | else return luaO_equalObj(o1, o2); | 143 | else return luaO_equalObj(o1, o2); |
| 145 | } | 144 | } |
| 146 | 145 | ||
| 146 | int lua_lessthan (lua_State *L, int index1, int index2) { | ||
| 147 | StkId o1 = Index(L, index1); | ||
| 148 | StkId o2 = Index(L, index2); | ||
| 149 | if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */ | ||
| 150 | else return luaV_lessthan(L, o1, o2, L->top); | ||
| 151 | } | ||
| 152 | |||
| 147 | 153 | ||
| 148 | 154 | ||
| 149 | double lua_tonumber (lua_State *L, int index) { | 155 | double lua_tonumber (lua_State *L, int index) { |
| @@ -168,14 +174,13 @@ void *lua_touserdata (lua_State *L, int index) { | |||
| 168 | } | 174 | } |
| 169 | 175 | ||
| 170 | const void *lua_topointer (lua_State *L, int index) { | 176 | const void *lua_topointer (lua_State *L, int index) { |
| 171 | const TObject *o = Index(L, index); | 177 | StkId o = Index(L, index); |
| 172 | switch (ttype(o)) { | 178 | switch (ttype(o)) { |
| 173 | case TAG_NUMBER: case TAG_NIL: | 179 | case TAG_NUMBER: case TAG_NIL: |
| 174 | return NULL; | 180 | return NULL; |
| 175 | case TAG_STRING: | 181 | case TAG_STRING: |
| 176 | return tsvalue(o)->str; | ||
| 177 | case TAG_USERDATA: | 182 | case TAG_USERDATA: |
| 178 | return tsvalue(o)->u.d.value; | 183 | return tsvalue(o); |
| 179 | case TAG_TABLE: | 184 | case TAG_TABLE: |
| 180 | return hvalue(o); | 185 | return hvalue(o); |
| 181 | case TAG_CCLOSURE: case TAG_LCLOSURE: | 186 | case TAG_CCLOSURE: case TAG_LCLOSURE: |
| @@ -243,31 +248,38 @@ void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */ | |||
| 243 | 248 | ||
| 244 | 249 | ||
| 245 | void lua_getglobal (lua_State *L, const char *name) { | 250 | void lua_getglobal (lua_State *L, const char *name) { |
| 246 | luaV_getglobal(L, luaS_new(L, name), L->top++); | 251 | StkId top = L->top; |
| 252 | *top = *luaV_getglobal(L, luaS_new(L, name)); | ||
| 253 | L->top = top+1; | ||
| 247 | } | 254 | } |
| 248 | 255 | ||
| 249 | 256 | ||
| 250 | void lua_gettable (lua_State *L) { | 257 | void lua_gettable (lua_State *L, int tableindex) { |
| 251 | luaV_gettable(L, L->top--); | 258 | StkId t = Index(L, tableindex); |
| 259 | StkId top = L->top; | ||
| 260 | *(top-1) = *luaV_gettable(L, t); | ||
| 261 | L->top = top; /* tag method may change top */ | ||
| 252 | } | 262 | } |
| 253 | 263 | ||
| 254 | 264 | ||
| 255 | void lua_rawget (lua_State *L) { | 265 | void lua_rawget (lua_State *L, int tableindex) { |
| 256 | LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "table expected"); | 266 | StkId t = Index(L, tableindex); |
| 257 | *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1); | 267 | LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); |
| 258 | L->top--; | 268 | *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1); |
| 259 | } | 269 | } |
| 260 | 270 | ||
| 261 | 271 | ||
| 262 | void lua_getglobals (lua_State *L) { | 272 | void lua_rawgeti (lua_State *L, int index, int n) { |
| 263 | hvalue(L->top) = L->gt; | 273 | StkId o = Index(L, index); |
| 264 | ttype(L->top) = TAG_TABLE; | 274 | LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected"); |
| 275 | *L->top = *luaH_getnum(hvalue(o), n); | ||
| 265 | api_incr_top(L); | 276 | api_incr_top(L); |
| 266 | } | 277 | } |
| 267 | 278 | ||
| 268 | 279 | ||
| 269 | void lua_gettagmethod (lua_State *L, int tag, const char *event) { | 280 | void lua_getglobals (lua_State *L) { |
| 270 | *L->top = *luaT_gettagmethod(L, tag, event); | 281 | hvalue(L->top) = L->gt; |
| 282 | ttype(L->top) = TAG_TABLE; | ||
| 271 | api_incr_top(L); | 283 | api_incr_top(L); |
| 272 | } | 284 | } |
| 273 | 285 | ||
| @@ -300,38 +312,40 @@ void lua_newtable (lua_State *L) { | |||
| 300 | 312 | ||
| 301 | 313 | ||
| 302 | void lua_setglobal (lua_State *L, const char *name) { | 314 | void lua_setglobal (lua_State *L, const char *name) { |
| 303 | luaV_setglobal(L, luaS_new(L, name), L->top--); | 315 | StkId top = L->top; |
| 316 | luaV_setglobal(L, luaS_new(L, name)); | ||
| 317 | L->top = top-1; /* remove element from the top */ | ||
| 304 | } | 318 | } |
| 305 | 319 | ||
| 306 | 320 | ||
| 307 | void lua_settable (lua_State *L) { | 321 | void lua_settable (lua_State *L, int tableindex) { |
| 322 | StkId t = Index(L, tableindex); | ||
| 308 | StkId top = L->top; | 323 | StkId top = L->top; |
| 309 | luaV_settable(L, top-3, top); | 324 | luaV_settable(L, t, top-2); |
| 310 | L->top = top-3; /* pop table, index, and value */ | 325 | L->top = top-2; /* pop index and value */ |
| 311 | } | 326 | } |
| 312 | 327 | ||
| 313 | 328 | ||
| 314 | void lua_rawset (lua_State *L) { | 329 | void lua_rawset (lua_State *L, int tableindex) { |
| 315 | LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "table expected"); | 330 | StkId t = Index(L, tableindex); |
| 316 | *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1); | 331 | LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); |
| 317 | L->top -= 3; | 332 | *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1); |
| 333 | L->top -= 2; | ||
| 318 | } | 334 | } |
| 319 | 335 | ||
| 320 | 336 | ||
| 321 | void lua_setglobals (lua_State *L) { | 337 | void lua_rawseti (lua_State *L, int index, int n) { |
| 322 | TObject *newtable = --L->top; | 338 | StkId o = Index(L, index); |
| 323 | LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); | 339 | LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected"); |
| 324 | L->gt = hvalue(newtable); | 340 | *luaH_setint(L, hvalue(o), n) = *(L->top-1); |
| 341 | L->top--; | ||
| 325 | } | 342 | } |
| 326 | 343 | ||
| 327 | 344 | ||
| 328 | void lua_settagmethod (lua_State *L, int tag, const char *event) { | 345 | void lua_setglobals (lua_State *L) { |
| 329 | TObject *method = L->top - 1; | 346 | StkId newtable = --L->top; |
| 330 | if (ttype(method) != TAG_NIL && | 347 | LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); |
| 331 | ttype(method) != TAG_CCLOSURE && | 348 | L->gt = hvalue(newtable); |
| 332 | ttype(method) != TAG_LCLOSURE) | ||
| 333 | lua_error(L, "Lua API error - tag method must be a function or nil"); | ||
| 334 | luaT_settagmethod(L, tag, event, method); | ||
| 335 | } | 349 | } |
| 336 | 350 | ||
| 337 | 351 | ||
| @@ -362,7 +376,6 @@ int lua_ref (lua_State *L, int lock) { | |||
| 362 | ** miscellaneous functions | 376 | ** miscellaneous functions |
| 363 | */ | 377 | */ |
| 364 | 378 | ||
| 365 | |||
| 366 | void lua_settag (lua_State *L, int tag) { | 379 | void lua_settag (lua_State *L, int tag) { |
| 367 | luaT_realtag(L, tag); | 380 | luaT_realtag(L, tag); |
| 368 | switch (ttype(L->top-1)) { | 381 | switch (ttype(L->top-1)) { |
| @@ -389,8 +402,8 @@ void lua_unref (lua_State *L, int ref) { | |||
| 389 | } | 402 | } |
| 390 | 403 | ||
| 391 | 404 | ||
| 392 | int lua_next (lua_State *L) { | 405 | int lua_next (lua_State *L, int tableindex) { |
| 393 | const TObject *t = Index(L, -2); | 406 | StkId t = Index(L, tableindex); |
| 394 | Node *n; | 407 | Node *n; |
| 395 | LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); | 408 | LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); |
| 396 | n = luaH_next(L, hvalue(t), Index(L, -1)); | 409 | n = luaH_next(L, hvalue(t), Index(L, -1)); |
| @@ -401,7 +414,7 @@ int lua_next (lua_State *L) { | |||
| 401 | return 1; | 414 | return 1; |
| 402 | } | 415 | } |
| 403 | else { /* no more elements */ | 416 | else { /* no more elements */ |
| 404 | L->top -= 2; /* remove key and table */ | 417 | L->top -= 1; /* remove key */ |
| 405 | return 0; | 418 | return 0; |
| 406 | } | 419 | } |
| 407 | } | 420 | } |
| @@ -427,3 +440,10 @@ int lua_getn (lua_State *L, int index) { | |||
| 427 | } | 440 | } |
| 428 | } | 441 | } |
| 429 | 442 | ||
| 443 | |||
| 444 | void lua_concat (lua_State *L, int n) { | ||
| 445 | StkId top = L->top; | ||
| 446 | luaV_strconc(L, n, top); | ||
| 447 | L->top = top-(n-1); | ||
| 448 | } | ||
| 449 | |||
