1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2000-10-30 20:50:09 +08:00
|
|
|
** $Id: lapi.c,v 1.109 2000/10/26 12:47:05 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
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
|
1997-09-17 03:25:59 +08:00
|
|
|
|
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) {
|
|
|
|
return Index(L, 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) {
|
|
|
|
if (index >= 0) {
|
|
|
|
TObject *o = L->Cbase+(index-1);
|
|
|
|
if (o >= L->top) return NULL;
|
|
|
|
else return o;
|
|
|
|
}
|
|
|
|
else return L->top+index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
void luaA_pushobject (lua_State *L, const TObject *o) {
|
|
|
|
*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) {
|
2000-08-30 04:43:28 +08:00
|
|
|
return (L->stack_last - L->top);
|
|
|
|
}
|
|
|
|
|
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) {
|
2000-08-29 01:57:04 +08:00
|
|
|
return (L->top - L->Cbase);
|
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) {
|
2000-08-29 01:57:04 +08:00
|
|
|
if (index >= 0)
|
|
|
|
luaD_adjusttop(L, L->Cbase, index);
|
|
|
|
else
|
2000-09-01 04:23:40 +08:00
|
|
|
L->top = L->top+index+1; /* index is negative */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_remove (lua_State *L, int index) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId p = luaA_index(L, index);
|
2000-09-01 04:23:40 +08:00
|
|
|
while (++p < L->top) *(p-1) = *p;
|
2000-09-06 03:33:32 +08:00
|
|
|
L->top--;
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId p = luaA_index(L, index);
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId q;
|
2000-09-19 03:39:26 +08:00
|
|
|
for (q = L->top; q>p; q--)
|
2000-09-01 04:23:40 +08:00
|
|
|
*q = *(q-1);
|
2000-09-19 03:39:26 +08:00
|
|
|
*p = *L->top;
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
*L->top = *luaA_index(L, index);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL) ? LUA_TNONE : ttype(o);
|
2000-10-03 04:10:55 +08:00
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API const char *lua_typename (lua_State *L, int t) {
|
2000-10-03 04:10:55 +08:00
|
|
|
UNUSED(L);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (t == LUA_TNONE) ? "no value" : luaO_typenames[t];
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_iscfunction (lua_State *L, int index) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL) ? 0 : iscfunction(o);
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
TObject *o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL) ? 0 : (tonumber(o) == 0);
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL) ? LUA_NOTAG : luaT_tag(o);
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o1 = luaA_indexAcceptable(L, index1);
|
|
|
|
StkId o2 = luaA_indexAcceptable(L, index2);
|
|
|
|
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
|
2000-08-29 22:52:27 +08:00
|
|
|
else return luaO_equalObj(o1, o2);
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o1 = luaA_indexAcceptable(L, index1);
|
|
|
|
StkId o2 = luaA_indexAcceptable(L, index2);
|
|
|
|
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
|
2000-09-06 03:33:32 +08:00
|
|
|
else return luaV_lessthan(L, o1, o2, L->top);
|
|
|
|
}
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API double lua_tonumber (lua_State *L, int index) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL || tonumber(o)) ? 0 : nvalue(o);
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL || tostring(L, o)) ? NULL : svalue(o);
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
2000-10-30 20:50:09 +08:00
|
|
|
return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
|
|
|
|
tsvalue(o)->u.d.value;
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId o = luaA_indexAcceptable(L, index);
|
|
|
|
if (o == NULL) return NULL;
|
2000-09-01 05:01:43 +08:00
|
|
|
switch (ttype(o)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TTABLE:
|
2000-09-01 05:01:43 +08:00
|
|
|
return hvalue(o);
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
2000-09-01 05:01:43 +08:00
|
|
|
return clvalue(o);
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2000-10-05 20:14:08 +08:00
|
|
|
ttype(L->top) = LUA_TNIL;
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(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_pushnumber (lua_State *L, double n) {
|
1999-12-02 03:50:08 +08:00
|
|
|
nvalue(L->top) = n;
|
2000-10-07 03:29:26 +08:00
|
|
|
ttype(L->top) = LUA_TNUMBER;
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(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) {
|
1999-12-02 03:50:08 +08:00
|
|
|
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
2000-10-05 20:14:08 +08:00
|
|
|
ttype(L->top) = LUA_TSTRING;
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(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) {
|
2000-08-29 01:57:04 +08:00
|
|
|
luaV_Cclosure(L, fn, n);
|
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_pushusertag (lua_State *L, void *u, int tag) {
|
|
|
|
/* ORDER LUA_T */
|
2000-10-05 20:14:08 +08:00
|
|
|
if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
|
2000-09-12 04:29:27 +08:00
|
|
|
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
|
1999-12-02 03:50:08 +08:00
|
|
|
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
2000-10-05 20:14:08 +08:00
|
|
|
ttype(L->top) = LUA_TUSERDATA;
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
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) {
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId top = L->top;
|
|
|
|
*top = *luaV_getglobal(L, luaS_new(L, name));
|
2000-09-12 03:45:27 +08:00
|
|
|
L->top = top;
|
|
|
|
api_incr_top(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) {
|
2000-09-19 03:39:26 +08:00
|
|
|
StkId t = Index(L, index);
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId top = L->top;
|
|
|
|
*(top-1) = *luaV_gettable(L, t);
|
|
|
|
L->top = top; /* tag method may change top */
|
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) {
|
2000-09-19 03:39:26 +08:00
|
|
|
StkId t = Index(L, index);
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
2000-09-06 03:33:32 +08:00
|
|
|
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
|
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) {
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId o = Index(L, index);
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
|
2000-09-06 03:33:32 +08:00
|
|
|
*L->top = *luaH_getnum(hvalue(o), n);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_getglobals (lua_State *L) {
|
2000-09-06 03:33:32 +08:00
|
|
|
hvalue(L->top) = L->gt;
|
2000-10-05 20:14:08 +08:00
|
|
|
ttype(L->top) = LUA_TTABLE;
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getref (lua_State *L, int ref) {
|
2000-08-29 01:57:04 +08:00
|
|
|
if (ref == LUA_REFNIL)
|
2000-10-05 20:14:08 +08:00
|
|
|
ttype(L->top) = LUA_TNIL;
|
2000-08-29 01:57:04 +08:00
|
|
|
else if (0 <= ref && ref < L->refSize &&
|
|
|
|
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
|
|
|
*L->top = L->refArray[ref].o;
|
1999-12-24 02:19:57 +08:00
|
|
|
else
|
2000-08-29 01:57:04 +08:00
|
|
|
return 0;
|
|
|
|
api_incr_top(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_newtable (lua_State *L) {
|
2000-08-29 01:57:04 +08:00
|
|
|
hvalue(L->top) = luaH_new(L, 0);
|
2000-10-05 20:14:08 +08:00
|
|
|
ttype(L->top) = LUA_TTABLE;
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(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) {
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId top = L->top;
|
|
|
|
luaV_setglobal(L, luaS_new(L, name));
|
|
|
|
L->top = top-1; /* remove element from the top */
|
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) {
|
2000-09-19 03:39:26 +08:00
|
|
|
StkId t = Index(L, index);
|
2000-08-29 01:57:04 +08:00
|
|
|
StkId top = L->top;
|
2000-09-06 03:33:32 +08:00
|
|
|
luaV_settable(L, t, top-2);
|
|
|
|
L->top = top-2; /* pop index and value */
|
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) {
|
2000-09-19 03:39:26 +08:00
|
|
|
StkId t = Index(L, index);
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
2000-09-06 03:33:32 +08:00
|
|
|
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
|
|
|
|
L->top -= 2;
|
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) {
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId o = Index(L, index);
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
|
2000-09-06 03:33:32 +08:00
|
|
|
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
|
|
|
|
L->top--;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_setglobals (lua_State *L) {
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId newtable = --L->top;
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
|
2000-09-06 03:33:32 +08:00
|
|
|
L->gt = hvalue(newtable);
|
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;
|
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 {
|
|
|
|
if (L->refFree != NONEXT) { /* is there a free place? */
|
|
|
|
ref = L->refFree;
|
|
|
|
L->refFree = L->refArray[ref].st;
|
|
|
|
}
|
|
|
|
else { /* no more free places */
|
|
|
|
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
|
|
|
|
"reference table overflow", MAX_INT);
|
2000-09-29 20:42:13 +08:00
|
|
|
L->nblocks += sizeof(struct Ref);
|
2000-08-29 01:57:04 +08:00
|
|
|
ref = L->refSize++;
|
|
|
|
}
|
|
|
|
L->refArray[ref].o = *(L->top-1);
|
|
|
|
L->refArray[ref].st = lock ? LOCK : HOLD;
|
|
|
|
}
|
|
|
|
L->top--;
|
|
|
|
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) {
|
2000-09-14 22:09:31 +08:00
|
|
|
luaD_call(L, L->top-(nargs+1), nresults);
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
#define GCunscale(x) ((unsigned long)(x)<<10)
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getgcthreshold (lua_State *L) {
|
2000-10-02 22:47:43 +08:00
|
|
|
return GCscale(L->GCthreshold);
|
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getgccount (lua_State *L) {
|
2000-10-02 22:47:43 +08:00
|
|
|
return GCscale(L->nblocks);
|
|
|
|
}
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
|
2000-10-02 22:47:43 +08:00
|
|
|
if (newthreshold > GCscale(ULONG_MAX))
|
|
|
|
L->GCthreshold = ULONG_MAX;
|
|
|
|
else
|
|
|
|
L->GCthreshold = GCunscale(newthreshold);
|
|
|
|
luaC_checkGC(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_settag (lua_State *L, int tag) {
|
1999-11-22 21:12:07 +08:00
|
|
|
luaT_realtag(L, tag);
|
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",
|
2000-10-05 20:14:08 +08:00
|
|
|
luaO_typename(L->top-1));
|
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) {
|
2000-08-29 01:57:04 +08:00
|
|
|
if (ref >= 0) {
|
2000-09-01 04:23:40 +08:00
|
|
|
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
|
2000-08-29 01:57:04 +08:00
|
|
|
L->refArray[ref].st = L->refFree;
|
|
|
|
L->refFree = ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_next (lua_State *L, int index) {
|
2000-10-07 03:29:26 +08:00
|
|
|
StkId t = luaA_index(L, index);
|
2000-08-31 22:08:27 +08:00
|
|
|
Node *n;
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
2000-10-07 03:29:26 +08:00
|
|
|
n = luaH_next(L, hvalue(t), luaA_index(L, -1));
|
2000-08-31 22:08:27 +08:00
|
|
|
if (n) {
|
|
|
|
*(L->top-1) = *key(n);
|
|
|
|
*L->top = *val(n);
|
|
|
|
api_incr_top(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else { /* no more elements */
|
2000-09-06 03:33:32 +08:00
|
|
|
L->top -= 1; /* remove key */
|
2000-08-31 22:08:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
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) {
|
2000-10-07 03:29:26 +08:00
|
|
|
Hash *h = hvalue(luaA_index(L, index));
|
2000-09-01 05:01:43 +08:00
|
|
|
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(value) == LUA_TNUMBER)
|
2000-09-01 05:01:43 +08:00
|
|
|
return (int)nvalue(value);
|
|
|
|
else {
|
|
|
|
Number max = 0;
|
|
|
|
int i = h->size;
|
|
|
|
Node *n = h->node;
|
|
|
|
while (i--) {
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(key(n)) == LUA_TNUMBER &&
|
|
|
|
ttype(val(n)) != LUA_TNIL &&
|
2000-09-01 05:01:43 +08:00
|
|
|
nvalue(key(n)) > max)
|
|
|
|
max = nvalue(key(n));
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return (int)max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2000-09-06 03:33:32 +08:00
|
|
|
StkId top = L->top;
|
|
|
|
luaV_strconc(L, n, top);
|
|
|
|
L->top = top-(n-1);
|
2000-10-02 22:47:43 +08:00
|
|
|
luaC_checkGC(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) {
|
|
|
|
TString *ts = luaS_newudata(L, size, NULL);
|
|
|
|
tsvalue(L->top) = ts;
|
|
|
|
ttype(L->top) = LUA_TUSERDATA;
|
|
|
|
api_incr_top(L);
|
|
|
|
return ts->u.d.value;
|
|
|
|
}
|
|
|
|
|