aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c48
1 files changed, 43 insertions, 5 deletions
diff --git a/lapi.c b/lapi.c
index 5364c040..fb22b3df 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.91 2000/08/31 14:08:27 roberto Exp roberto $ 2** $Id: lapi.c,v 1.92 2000/08/31 20:23:40 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*/
@@ -167,6 +167,23 @@ void *lua_touserdata (lua_State *L, int index) {
167 access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value); 167 access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
168} 168}
169 169
170const void *lua_topointer (lua_State *L, int index) {
171 const TObject *o = Index(L, index);
172 switch (ttype(o)) {
173 case TAG_NUMBER: case TAG_NIL:
174 return NULL;
175 case TAG_STRING:
176 return tsvalue(o)->str;
177 case TAG_USERDATA:
178 return tsvalue(o)->u.d.value;
179 case TAG_TABLE:
180 return hvalue(o);
181 case TAG_CCLOSURE: case TAG_LCLOSURE:
182 return clvalue(o);
183 default: return NULL;
184 }
185}
186
170 187
171 188
172/* 189/*
@@ -236,7 +253,7 @@ void lua_gettable (lua_State *L) {
236 253
237 254
238void lua_rawget (lua_State *L) { 255void lua_rawget (lua_State *L) {
239 LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "not a table"); 256 LUA_ASSERT(ttype(L->top-2) == TAG_TABLE, "table expected");
240 *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1); 257 *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1);
241 L->top--; 258 L->top--;
242} 259}
@@ -295,7 +312,7 @@ void lua_settable (lua_State *L) {
295 312
296 313
297void lua_rawset (lua_State *L) { 314void lua_rawset (lua_State *L) {
298 LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "not a table"); 315 LUA_ASSERT(ttype(L->top-3) == TAG_TABLE, "table expected");
299 *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1); 316 *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1);
300 L->top -= 3; 317 L->top -= 3;
301} 318}
@@ -303,7 +320,7 @@ void lua_rawset (lua_State *L) {
303 320
304void lua_setglobals (lua_State *L) { 321void lua_setglobals (lua_State *L) {
305 TObject *newtable = --L->top; 322 TObject *newtable = --L->top;
306 LUA_ASSERT(ttype(newtable) == TAG_TABLE, "not a table"); 323 LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
307 L->gt = hvalue(newtable); 324 L->gt = hvalue(newtable);
308} 325}
309 326
@@ -375,7 +392,7 @@ void lua_unref (lua_State *L, int ref) {
375int lua_next (lua_State *L) { 392int lua_next (lua_State *L) {
376 const TObject *t = Index(L, -2); 393 const TObject *t = Index(L, -2);
377 Node *n; 394 Node *n;
378 LUA_ASSERT(ttype(t) == TAG_TABLE, "object is not a table in `lua_next'"); 395 LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
379 n = luaH_next(L, hvalue(t), Index(L, -1)); 396 n = luaH_next(L, hvalue(t), Index(L, -1));
380 if (n) { 397 if (n) {
381 *(L->top-1) = *key(n); 398 *(L->top-1) = *key(n);
@@ -389,3 +406,24 @@ int lua_next (lua_State *L) {
389 } 406 }
390} 407}
391 408
409
410int lua_getn (lua_State *L, int index) {
411 Hash *h = hvalue(Index(L, index));
412 const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
413 if (ttype(value) == TAG_NUMBER)
414 return (int)nvalue(value);
415 else {
416 Number max = 0;
417 int i = h->size;
418 Node *n = h->node;
419 while (i--) {
420 if (ttype(key(n)) == TAG_NUMBER &&
421 ttype(val(n)) != TAG_NIL &&
422 nvalue(key(n)) > max)
423 max = nvalue(key(n));
424 n++;
425 }
426 return (int)max;
427 }
428}
429