1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2001-02-21 02:15:33 +08:00
|
|
|
** $Id: lapi.c,v 1.130 2001/02/14 17:04:11 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Lua API
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lapi.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#include "lstate.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
1999-12-15 02:33:29 +08:00
|
|
|
"$Authors: " LUA_AUTHORS " $";
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-02-12 23:42:44 +08:00
|
|
|
#ifndef api_check
|
|
|
|
#define api_check(L, o) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->Cbase))
|
|
|
|
|
2000-09-12 03:45:27 +08:00
|
|
|
#define api_incr_top(L) incr_top
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
1999-02-23 22:57:28 +08:00
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
TObject *luaA_index (lua_State *L, int index) {
|
2001-02-14 00:17:53 +08:00
|
|
|
if (index > 0) {
|
|
|
|
api_check(L, index <= L->top - L->Cbase);
|
|
|
|
return L->Cbase+index-1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
api_check(L, index != 0 && -index <= L->top - L->Cbase);
|
|
|
|
return L->top+index;
|
|
|
|
}
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-07 03:29:26 +08:00
|
|
|
static TObject *luaA_indexAcceptable (lua_State *L, int index) {
|
2001-02-14 00:17:53 +08:00
|
|
|
if (index > 0) {
|
2000-10-07 03:29:26 +08:00
|
|
|
TObject *o = L->Cbase+(index-1);
|
2001-02-14 00:17:53 +08:00
|
|
|
api_check(L, index <= L->stack_last - L->Cbase);
|
2000-10-07 03:29:26 +08:00
|
|
|
if (o >= L->top) return NULL;
|
|
|
|
else return o;
|
|
|
|
}
|
2001-02-14 00:17:53 +08:00
|
|
|
else {
|
|
|
|
api_check(L, index != 0 && -index <= L->top - L->Cbase);
|
|
|
|
return L->top+index;
|
|
|
|
}
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
void luaA_pushobject (lua_State *L, const TObject *o) {
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(L->top, o);
|
2000-05-09 03:32:53 +08:00
|
|
|
incr_top;
|
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_stackspace (lua_State *L) {
|
2001-01-24 23:45:33 +08:00
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
i = (L->stack_last - L->top);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
2000-08-30 04:43:28 +08:00
|
|
|
}
|
|
|
|
|
2000-05-09 03:32:53 +08:00
|
|
|
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2000-08-29 01:57:04 +08:00
|
|
|
** basic stack manipulation
|
1997-09-17 03:25:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_gettop (lua_State *L) {
|
2001-01-24 23:45:33 +08:00
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
i = (L->top - L->Cbase);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_settop (lua_State *L, int index) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
if (index >= 0)
|
|
|
|
luaD_adjusttop(L, L->Cbase, index);
|
2001-02-12 23:42:44 +08:00
|
|
|
else {
|
|
|
|
api_check(L, -(index+1) <= (L->top - L->Cbase));
|
2000-09-01 04:23:40 +08:00
|
|
|
L->top = L->top+index+1; /* index is negative */
|
2001-02-12 23:42:44 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_remove (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId p;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
p = luaA_index(L, index);
|
2001-01-18 23:59:09 +08:00
|
|
|
while (++p < L->top) setobj(p-1, p);
|
2000-09-06 03:33:32 +08:00
|
|
|
L->top--;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_insert (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId p;
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId q;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
p = luaA_index(L, index);
|
2001-01-18 23:59:09 +08:00
|
|
|
for (q = L->top; q>p; q--) setobj(q, q-1);
|
|
|
|
setobj(p, L->top);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushvalue (lua_State *L, int index) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
setobj(L->top, luaA_index(L, index));
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
|
|
|
** access functions (stack -> C)
|
|
|
|
*/
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_type (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
i = (o == NULL) ? LUA_TNONE : ttype(o);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
2000-10-03 04:10:55 +08:00
|
|
|
}
|
|
|
|
|
2001-01-26 00:45:36 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API const char *lua_typename (lua_State *L, int t) {
|
2001-01-24 23:45:33 +08:00
|
|
|
const char *s;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
s = (t == LUA_TNONE) ? "no value" : basictypename(G(L), t);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return s;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
|
2001-01-26 00:45:36 +08:00
|
|
|
LUA_API const char *lua_xtype (lua_State *L, int index) {
|
|
|
|
StkId o;
|
|
|
|
const char *type;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
type = (o == NULL) ? "no value" : luaT_typename(G(L), o);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_iscfunction (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
i = (o == NULL) ? 0 : iscfunction(o);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_isnumber (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
TObject *o;
|
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
i = (o == NULL) ? 0 : (tonumber(o) == 0);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_isstring (lua_State *L, int index) {
|
2000-10-05 20:14:08 +08:00
|
|
|
int t = lua_type(L, index);
|
2000-10-03 04:10:55 +08:00
|
|
|
return (t == LUA_TSTRING || t == LUA_TNUMBER);
|
|
|
|
}
|
|
|
|
|
2000-10-03 22:27:44 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_tag (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
1999-10-08 03:04:30 +08:00
|
|
|
}
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o1, o2;
|
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o1 = luaA_indexAcceptable(L, index1);
|
|
|
|
o2 = luaA_indexAcceptable(L, index2);
|
|
|
|
i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
|
|
|
|
: luaO_equalObj(o1, o2);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o1, o2;
|
|
|
|
int i;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o1 = luaA_indexAcceptable(L, index1);
|
|
|
|
o2 = luaA_indexAcceptable(L, index2);
|
|
|
|
i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
|
2001-02-08 02:13:49 +08:00
|
|
|
: luaV_lessthan(L, o1, o2);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
2000-09-06 03:33:32 +08:00
|
|
|
}
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-12-05 02:33:40 +08:00
|
|
|
LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
lua_Number n;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
n = (o == NULL || tonumber(o)) ? 0 : nvalue(o);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return n;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API const char *lua_tostring (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
const char *s;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
s = (o == NULL || tostring(L, o)) ? NULL : svalue(o);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return s;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API size_t lua_strlen (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
size_t l;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return l;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
lua_CFunction f;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return f;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void *lua_touserdata (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
void *p;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
|
2000-10-30 20:50:09 +08:00
|
|
|
tsvalue(o)->u.d.value;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return p;
|
1999-11-12 01:02:40 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API const void *lua_topointer (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
|
|
|
const void *p;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
o = luaA_indexAcceptable(L, index);
|
|
|
|
if (o == NULL) p = NULL;
|
|
|
|
else {
|
|
|
|
switch (ttype(o)) {
|
|
|
|
case LUA_TTABLE:
|
|
|
|
p = hvalue(o);
|
|
|
|
break;
|
|
|
|
case LUA_TFUNCTION:
|
|
|
|
p = clvalue(o);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2000-09-01 05:01:43 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return p;
|
2000-09-01 05:01:43 +08:00
|
|
|
}
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
|
|
|
** push functions (C -> stack)
|
|
|
|
*/
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushnil (lua_State *L) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
setnilvalue(L->top);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-12-05 02:33:40 +08:00
|
|
|
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
setnvalue(L->top, n);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
setsvalue(L->top, luaS_newlstr(L, s, len));
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushstring (lua_State *L, const char *s) {
|
1998-03-07 00:54:42 +08:00
|
|
|
if (s == NULL)
|
1999-11-22 21:12:07 +08:00
|
|
|
lua_pushnil(L);
|
1998-03-07 00:54:42 +08:00
|
|
|
else
|
1999-11-22 21:12:07 +08:00
|
|
|
lua_pushlstring(L, s, strlen(s));
|
1998-03-07 00:54:42 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, n);
|
2000-08-29 01:57:04 +08:00
|
|
|
luaV_Cclosure(L, fn, n);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2001-02-10 03:53:16 +08:00
|
|
|
LUA_API int lua_pushuserdata (lua_State *L, void *u) {
|
|
|
|
int isnew;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-10 03:53:16 +08:00
|
|
|
isnew = luaS_createudata(L, u, L->top);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-02-10 03:53:16 +08:00
|
|
|
return isnew;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** get functions (Lua -> stack)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_getglobal (lua_State *L, const char *name) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-02 00:03:38 +08:00
|
|
|
luaV_getglobal(L, luaS_new(L, name), L->top);
|
2000-09-12 03:45:27 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_gettable (lua_State *L, int index) {
|
2001-02-02 00:03:38 +08:00
|
|
|
StkId t;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
t = luaA_index(L, index);
|
2001-02-08 02:13:49 +08:00
|
|
|
luaV_gettable(L, t, L->top-1, L->top-1);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_rawget (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId t;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
t = luaA_index(L, index);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ttype(t) == LUA_TTABLE);
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
o = luaA_index(L, index);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ttype(o) == LUA_TTABLE);
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(L->top, luaH_getnum(hvalue(o), n));
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_getglobals (lua_State *L) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
sethvalue(L->top, L->gt);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getref (lua_State *L, int ref) {
|
2001-01-24 23:45:33 +08:00
|
|
|
int status = 1;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
if (ref == LUA_REFNIL) {
|
|
|
|
setnilvalue(L->top);
|
2001-01-24 23:45:33 +08:00
|
|
|
api_incr_top(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
}
|
2001-02-12 23:42:44 +08:00
|
|
|
else {
|
|
|
|
api_check(L, 0 <= ref && ref < G(L)->nref);
|
|
|
|
if (G(L)->refArray[ref].st != LOCK && G(L)->refArray[ref].st != HOLD)
|
|
|
|
status = 0;
|
|
|
|
else {
|
|
|
|
setobj(L->top, &G(L)->refArray[ref].o);
|
|
|
|
api_incr_top(L);
|
|
|
|
}
|
2001-01-18 23:59:09 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return status;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_newtable (lua_State *L) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-18 23:59:09 +08:00
|
|
|
sethvalue(L->top, luaH_new(L, 0));
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** set functions (stack -> Lua)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 1);
|
2001-02-08 02:13:49 +08:00
|
|
|
luaV_setglobal(L, luaS_new(L, name), L->top - 1);
|
2001-02-02 00:03:38 +08:00
|
|
|
L->top--; /* remove element from the top */
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_settable (lua_State *L, int index) {
|
2001-02-02 00:03:38 +08:00
|
|
|
StkId t;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 2);
|
2001-02-14 00:17:53 +08:00
|
|
|
t = luaA_index(L, index);
|
2001-02-08 02:13:49 +08:00
|
|
|
luaV_settable(L, t, L->top - 2, L->top - 1);
|
2001-02-02 00:03:38 +08:00
|
|
|
L->top -= 2; /* pop index and value */
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_rawset (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId t;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 2);
|
2001-02-14 00:17:53 +08:00
|
|
|
t = luaA_index(L, index);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ttype(t) == LUA_TTABLE);
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
|
2000-09-06 03:33:32 +08:00
|
|
|
L->top -= 2;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_rawseti (lua_State *L, int index, int n) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId o;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 1);
|
2001-02-14 00:17:53 +08:00
|
|
|
o = luaA_index(L, index);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ttype(o) == LUA_TTABLE);
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
|
2000-09-06 03:33:32 +08:00
|
|
|
L->top--;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_setglobals (lua_State *L) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId newtable;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 1);
|
2001-01-24 23:45:33 +08:00
|
|
|
newtable = --L->top;
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ttype(newtable) == LUA_TTABLE);
|
2000-09-06 03:33:32 +08:00
|
|
|
L->gt = hvalue(newtable);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_ref (lua_State *L, int lock) {
|
2000-08-29 01:57:04 +08:00
|
|
|
int ref;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 1);
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(L->top-1) == LUA_TNIL)
|
2000-08-29 01:57:04 +08:00
|
|
|
ref = LUA_REFNIL;
|
|
|
|
else {
|
2001-01-19 21:20:30 +08:00
|
|
|
if (G(L)->refFree != NONEXT) { /* is there a free place? */
|
|
|
|
ref = G(L)->refFree;
|
|
|
|
G(L)->refFree = G(L)->refArray[ref].st;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
else { /* no more free places */
|
2001-01-19 21:20:30 +08:00
|
|
|
luaM_growvector(L, G(L)->refArray, G(L)->nref, G(L)->sizeref, struct Ref,
|
2000-12-27 02:46:09 +08:00
|
|
|
MAX_INT, "reference table overflow");
|
2001-01-19 21:20:30 +08:00
|
|
|
ref = G(L)->nref++;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-01-19 21:20:30 +08:00
|
|
|
setobj(&G(L)->refArray[ref].o, L->top-1);
|
|
|
|
G(L)->refArray[ref].st = lock ? LOCK : HOLD;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
L->top--;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 22:09:31 +08:00
|
|
|
/*
|
|
|
|
** "do" functions (run Lua code)
|
|
|
|
** (most of them are in ldo.c)
|
|
|
|
*/
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, nargs+1);
|
2000-09-14 22:09:31 +08:00
|
|
|
luaD_call(L, L->top-(nargs+1), nresults);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-09-14 22:09:31 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-10-02 22:47:43 +08:00
|
|
|
/*
|
|
|
|
** Garbage-collection functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* GC values are expressed in Kbytes: #bytes/2^10 */
|
|
|
|
#define GCscale(x) ((int)((x)>>10))
|
2001-02-21 02:15:33 +08:00
|
|
|
#define GCunscale(x) ((lu_mem)(x)<<10)
|
2000-10-02 22:47:43 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getgcthreshold (lua_State *L) {
|
2001-01-24 23:45:33 +08:00
|
|
|
int threshold;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
threshold = GCscale(G(L)->GCthreshold);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return threshold;
|
2000-10-02 22:47:43 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getgccount (lua_State *L) {
|
2001-01-24 23:45:33 +08:00
|
|
|
int count;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
count = GCscale(G(L)->nblocks);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return count;
|
2000-10-02 22:47:43 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2000-10-02 22:47:43 +08:00
|
|
|
if (newthreshold > GCscale(ULONG_MAX))
|
2001-01-19 21:20:30 +08:00
|
|
|
G(L)->GCthreshold = ULONG_MAX;
|
2000-10-02 22:47:43 +08:00
|
|
|
else
|
2001-01-19 21:20:30 +08:00
|
|
|
G(L)->GCthreshold = GCunscale(newthreshold);
|
2000-10-02 22:47:43 +08:00
|
|
|
luaC_checkGC(L);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-10-02 22:47:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
2000-08-31 22:08:27 +08:00
|
|
|
** miscellaneous functions
|
2000-08-29 01:57:04 +08:00
|
|
|
*/
|
|
|
|
|
2001-01-26 00:45:36 +08:00
|
|
|
LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
|
|
|
|
int tag;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
if (basictype != LUA_TNONE &&
|
|
|
|
basictype != LUA_TTABLE &&
|
|
|
|
basictype != LUA_TUSERDATA)
|
|
|
|
luaO_verror(L, "invalid basic type (%d) for new type", basictype);
|
|
|
|
tag = luaT_newtag(L, name, basictype);
|
|
|
|
if (tag == LUA_TNONE)
|
|
|
|
luaO_verror(L, "type name '%.30s' already exists", name);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API int lua_type2tag (lua_State *L, const char *name) {
|
|
|
|
int tag;
|
|
|
|
const TObject *v;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
|
|
|
|
if (ttype(v) == LUA_TNIL)
|
|
|
|
tag = LUA_TNONE;
|
|
|
|
else {
|
|
|
|
lua_assert(ttype(v) == LUA_TNUMBER);
|
2001-01-26 19:45:51 +08:00
|
|
|
tag = (int)nvalue(v);
|
2001-01-26 00:45:36 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-26 00:45:36 +08:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API void lua_settag (lua_State *L, int tag) {
|
|
|
|
int basictype;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 1);
|
2001-01-26 00:45:36 +08:00
|
|
|
if (tag < 0 || tag >= G(L)->ntag)
|
|
|
|
luaO_verror(L, "%d is not a valid tag", tag);
|
|
|
|
basictype = G(L)->TMtable[tag].basictype;
|
|
|
|
if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
|
|
|
|
luaO_verror(L, "tag %d can only be used for type '%.20s'", tag,
|
|
|
|
basictypename(G(L), basictype));
|
1999-12-02 03:50:08 +08:00
|
|
|
switch (ttype(L->top-1)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TTABLE:
|
2000-06-09 02:27:13 +08:00
|
|
|
hvalue(L->top-1)->htag = tag;
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TUSERDATA:
|
2000-06-09 02:27:13 +08:00
|
|
|
tsvalue(L->top-1)->u.d.tag = tag;
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
|
|
|
default:
|
2000-09-12 04:29:27 +08:00
|
|
|
luaO_verror(L, "cannot change the tag of a %.20s",
|
2001-01-26 00:45:36 +08:00
|
|
|
luaT_typename(G(L), L->top-1));
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API void lua_error (lua_State *L, const char *s) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
luaD_error(L, s);
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_unref (lua_State *L, int ref) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
if (ref >= 0) {
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ref < G(L)->nref && G(L)->refArray[ref].st < 0);
|
2001-01-19 21:20:30 +08:00
|
|
|
G(L)->refArray[ref].st = G(L)->refFree;
|
|
|
|
G(L)->refFree = ref;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_next (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId t;
|
2000-08-31 22:08:27 +08:00
|
|
|
Node *n;
|
2001-01-24 23:45:33 +08:00
|
|
|
int more;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
t = luaA_index(L, index);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_check(L, ttype(t) == LUA_TTABLE);
|
2001-02-14 00:17:53 +08:00
|
|
|
n = luaH_next(L, hvalue(t), luaA_index(L, -1));
|
2000-08-31 22:08:27 +08:00
|
|
|
if (n) {
|
2001-01-30 01:17:26 +08:00
|
|
|
setkey2obj(L->top-1, n);
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(L->top, val(n));
|
2000-08-31 22:08:27 +08:00
|
|
|
api_incr_top(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
more = 1;
|
2000-08-31 22:08:27 +08:00
|
|
|
}
|
|
|
|
else { /* no more elements */
|
2000-09-06 03:33:32 +08:00
|
|
|
L->top -= 1; /* remove key */
|
2001-01-24 23:45:33 +08:00
|
|
|
more = 0;
|
2000-08-31 22:08:27 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return more;
|
2000-05-27 03:17:57 +08:00
|
|
|
}
|
|
|
|
|
2000-09-01 05:01:43 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getn (lua_State *L, int index) {
|
2001-01-24 23:45:33 +08:00
|
|
|
Hash *h;
|
|
|
|
const TObject *value;
|
|
|
|
int n;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-14 00:17:53 +08:00
|
|
|
h = hvalue(luaA_index(L, index));
|
2001-01-24 23:45:33 +08:00
|
|
|
value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(value) == LUA_TNUMBER)
|
2001-01-24 23:45:33 +08:00
|
|
|
n = (int)nvalue(value);
|
2000-09-01 05:01:43 +08:00
|
|
|
else {
|
2000-12-05 02:33:40 +08:00
|
|
|
lua_Number max = 0;
|
2000-09-01 05:01:43 +08:00
|
|
|
int i = h->size;
|
2001-01-24 23:45:33 +08:00
|
|
|
Node *nd = h->node;
|
2000-09-01 05:01:43 +08:00
|
|
|
while (i--) {
|
2001-01-30 01:17:26 +08:00
|
|
|
if (ttype_key(nd) == LUA_TNUMBER &&
|
2001-01-24 23:45:33 +08:00
|
|
|
ttype(val(nd)) != LUA_TNIL &&
|
2001-01-30 01:17:26 +08:00
|
|
|
nvalue_key(nd) > max)
|
|
|
|
max = nvalue_key(nd);
|
2001-01-24 23:45:33 +08:00
|
|
|
nd++;
|
2000-09-01 05:01:43 +08:00
|
|
|
}
|
2001-01-24 23:45:33 +08:00
|
|
|
n = (int)max;
|
2000-09-01 05:01:43 +08:00
|
|
|
}
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return n;
|
2000-09-01 05:01:43 +08:00
|
|
|
}
|
|
|
|
|
2000-09-06 03:33:32 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_concat (lua_State *L, int n) {
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, n);
|
2001-02-15 01:04:11 +08:00
|
|
|
if (n >= 2) {
|
|
|
|
luaV_strconc(L, n, L->top);
|
|
|
|
L->top -= (n-1);
|
|
|
|
luaC_checkGC(L);
|
|
|
|
}
|
|
|
|
else if (n == 0) { /* push null string */
|
|
|
|
setsvalue(L->top, luaS_newlstr(L, NULL, 0));
|
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
/* else n == 1; nothing to do */
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2000-09-06 03:33:32 +08:00
|
|
|
}
|
|
|
|
|
2000-10-26 20:47:05 +08:00
|
|
|
|
|
|
|
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
2001-01-24 23:45:33 +08:00
|
|
|
TString *ts;
|
|
|
|
void *p;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_LOCK(L);
|
2001-02-01 21:56:49 +08:00
|
|
|
if (size == 0) size = 1;
|
2001-01-24 23:45:33 +08:00
|
|
|
ts = luaS_newudata(L, size, NULL);
|
2001-01-18 23:59:09 +08:00
|
|
|
setuvalue(L->top, ts);
|
2000-10-26 20:47:05 +08:00
|
|
|
api_incr_top(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
p = ts->u.d.value;
|
2001-02-02 23:13:05 +08:00
|
|
|
LUA_UNLOCK(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return p;
|
2000-10-26 20:47:05 +08:00
|
|
|
}
|
|
|
|
|