int Table::size() const { assert( m_ref >= 0 ); RestoreStack rs( m_lua ); lua_getref( m_lua, m_ref ); return lua_getn( m_lua, -1 ); }
// $$$ Added by ben static int luaB_xsort (lua_State *L) { int n; luaL_checktype(L, 1, LUA_TTABLE); n = lua_getn(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); lua_settop(L, 3); /* make sure there is 3 arguments */ xauxsort(L, 1, n); return 0; }
static int luaB_sort (lua_State *L) { int n; luaL_checktype(L, 1, LUA_TTABLE); n = lua_getn(L, 1); if (!lua_isnull(L, 2)) /* is there a 2nd argument? */ luaL_checktype(L, 2, LUA_TFUNCTION); lua_settop(L, 2); /* make sure there is two arguments */ auxsort(L, 1, n); return 0; }
static int luaB_foreachi (lua_State *L) { int n, i; luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); n = lua_getn(L, 1); for (i=1; i<=n; i++) { lua_pushvalue(L, 2); /* function */ lua_pushnumber(L, i); /* 1st argument */ lua_rawgeti(L, 1, i); /* 2nd argument */ lua_rawcall(L, 2, 1); if (!lua_isnil(L, -1)) return 1; lua_pop(L, 1); /* remove nil result */ } return 0; }
/* i: table index in stack */ static int vertex_from_table(draw_vertex_t *v, lua_State * L, int i) { int j; int top = lua_gettop(L); int n = lua_getn(L,i); float *f = &v->x; if (n > 4 + 4 + 2) { n = 4 + 4 + 2; } for (j=1; j<n; ++j) { lua_rawgeti(L,i,j); *f++ = lua_tonumber(L,-1); } lua_settop(L,top); return n; }
static int luaB_tremove (lua_State *L) { int pos, n; luaL_checktype(L, 1, LUA_TTABLE); n = lua_getn(L, 1); pos = luaL_opt_int(L, 2, n); if (n <= 0) return 0; /* table is "empty" */ lua_rawgeti(L, 1, pos); /* result = t[pos] */ for ( ;pos<n; pos++) { lua_rawgeti(L, 1, pos+1); lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */ } lua_pushstring(L, "n"); lua_pushnumber(L, n-1); lua_rawset(L, 1); /* t.n = n-1 */ lua_pushnil(L); lua_rawseti(L, 1, n); /* t[n] = nil */ return 1; }
static int luaB_tinsert (lua_State *L) { int v = lua_gettop(L); /* last argument: to be inserted */ int n, pos; luaL_checktype(L, 1, LUA_TTABLE); n = lua_getn(L, 1); if (v == 2) /* called with only 2 arguments */ pos = n+1; else pos = luaL_check_int(L, 2); /* 2nd argument is the position */ lua_pushstring(L, "n"); lua_pushnumber(L, n+1); lua_rawset(L, 1); /* t.n = n+1 */ for (; n>=pos; n--) { lua_rawgeti(L, 1, n); lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */ } lua_pushvalue(L, v); lua_rawseti(L, 1, pos); /* t[pos] = v */ return 0; }
static int luaB_call (lua_State *L) { int oldtop; const char *options = luaL_opt_string(L, 3, ""); int err = 0; /* index of old error method */ int i, status; int n; luaL_checktype(L, 2, LUA_TTABLE); n = lua_getn(L, 2); if (!lua_isnull(L, 4)) /* set new error method */ { lua_getglobal(L, LUA_ERRORMESSAGE); err = lua_gettop(L); /* get index */ lua_pushvalue(L, 4); lua_setglobal(L, LUA_ERRORMESSAGE); } oldtop = lua_gettop(L); /* top before function-call preparation */ /* push function */ lua_pushvalue(L, 1); luaL_checkstack(L, n, "too many arguments"); for (i=0; i<n; i++) /* push arg[1...n] */ lua_rawgeti(L, 2, i+1); status = lua_call(L, n, LUA_MULTRET); if (err != 0) /* restore old error method */ { lua_pushvalue(L, err); lua_setglobal(L, LUA_ERRORMESSAGE); } if (status != 0) /* error in call? */ { if (strchr(options, 'x')) lua_pushnil(L); /* return nil to signal the error */ else lua_error(L, NULL); /* propagate error without additional messages */ return 1; } if (strchr(options, 'p')) /* pack results? */ lua_error(L, "deprecated option `p' in `call'"); return lua_gettop(L) - oldtop; /* results are already on the stack */ }
static int luaB_getn (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); lua_pushnumber(L, lua_getn(L, 1)); return 1; }