lua/lapi.c

488 lines
10 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
** $Id: lapi.c,v 1.102 2000/10/02 14:47:43 roberto Exp roberto $
1997-09-16 23:25:59 +04:00
** Lua API
** See Copyright Notice in lua.h
*/
#include <string.h>
#include "lua.h"
1997-09-16 23:25:59 +04:00
#include "lapi.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
1997-09-16 23:25:59 +04:00
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvm.h"
1999-08-17 00:52:00 +04:00
const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
1999-12-14 21:33:29 +03:00
"$Authors: " LUA_AUTHORS " $";
1997-09-16 23:25:59 +04:00
2000-08-28 21:57:04 +04:00
#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
1997-09-16 23:25:59 +04:00
2000-09-11 23:45:27 +04:00
#define api_incr_top(L) incr_top
1997-09-16 23:25:59 +04:00
2000-08-28 21:57:04 +04:00
TObject *luaA_index (lua_State *L, int index) {
return Index(L, index);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
void luaA_pushobject (lua_State *L, const TObject *o) {
*L->top = *o;
incr_top;
}
2000-08-30 00:43:28 +04:00
int lua_stackspace (lua_State *L) {
return (L->stack_last - L->top);
}
1997-09-16 23:25:59 +04:00
/*
2000-08-28 21:57:04 +04:00
** basic stack manipulation
1997-09-16 23:25:59 +04:00
*/
2000-08-28 21:57:04 +04:00
int lua_gettop (lua_State *L) {
return (L->top - L->Cbase);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
void lua_settop (lua_State *L, int index) {
if (index >= 0)
luaD_adjusttop(L, L->Cbase, index);
else
L->top = L->top+index+1; /* index is negative */
}
2000-09-05 23:33:32 +04:00
void lua_remove (lua_State *L, int index) {
StkId p = Index(L, index);
while (++p < L->top) *(p-1) = *p;
2000-09-05 23:33:32 +04:00
L->top--;
}
void lua_insert (lua_State *L, int index) {
2000-09-05 23:33:32 +04:00
StkId p = Index(L, index);
StkId q;
2000-09-18 23:39:26 +04:00
for (q = L->top; q>p; q--)
*q = *(q-1);
2000-09-18 23:39:26 +04:00
*p = *L->top;
1997-09-16 23:25:59 +04:00
}
2000-09-05 23:33:32 +04:00
void lua_pushvalue (lua_State *L, int index) {
2000-08-28 21:57:04 +04:00
*L->top = *Index(L, index);
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
/*
** access functions (stack -> C)
*/
1997-09-16 23:25:59 +04:00
2000-08-28 21:57:04 +04:00
#define btest(L,i,value,default) { \
StkId o; \
if ((i) >= 0) { \
o = L->Cbase+((i)-1); \
if (o >= L->top) return (default); \
} \
else o = L->top+(i); \
return (value); }
1997-09-16 23:25:59 +04:00
2000-08-28 21:57:04 +04:00
#define access(L,i,test,default,value) { \
StkId o; \
if ((i) >= 0) { \
o = L->Cbase+((i)-1); \
if (o >= L->top) return (default); \
} \
else o = L->top+(i); \
return ((test) ? (value) : (default)); }
1997-09-16 23:25:59 +04:00
lua_Type lua_type (lua_State *L, int index) {
btest(L, index, luaO_type(o), LUA_NOVALUE);
}
const char *lua_typename (lua_State *L, lua_Type t) {
static const char *const names[] = {
"NO VALUE", "userdata", "number", "string", "table", "function", "nil"
};
UNUSED(L);
return names[(int)t];
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
int lua_iscfunction (lua_State *L, int index) {
btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
int lua_isnumber (lua_State *L, int index) {
btest(L, index, (tonumber(Index(L, index)) == 0), 0);
1997-09-16 23:25:59 +04:00
}
int lua_isstring (lua_State *L, int index) {
lua_Type t = lua_type(L, index);
return (t == LUA_TSTRING || t == LUA_TNUMBER);
}
2000-08-28 21:57:04 +04:00
int lua_tag (lua_State *L, int index) {
btest(L, index,
2000-09-05 23:33:32 +04:00
((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
luaT_effectivetag(L, o)), LUA_NOTAG);
1999-10-07 23:04:30 +04:00
}
1997-09-16 23:25:59 +04:00
2000-09-05 23:33:32 +04:00
int lua_equal (lua_State *L, int index1, int index2) {
StkId o1 = Index(L, index1);
StkId o2 = Index(L, index2);
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
else return luaO_equalObj(o1, o2);
1997-09-16 23:25:59 +04:00
}
2000-09-05 23:33:32 +04:00
int lua_lessthan (lua_State *L, int index1, int index2) {
StkId o1 = Index(L, index1);
StkId o2 = Index(L, index2);
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
else return luaV_lessthan(L, o1, o2, L->top);
}
1997-09-16 23:25:59 +04:00
2000-08-28 21:57:04 +04:00
double lua_tonumber (lua_State *L, int index) {
access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
const char *lua_tostring (lua_State *L, int index) {
access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
size_t lua_strlen (lua_State *L, int index) {
access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
lua_CFunction lua_tocfunction (lua_State *L, int index) {
access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
void *lua_touserdata (lua_State *L, int index) {
access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
1999-11-11 20:02:40 +03:00
}
const void *lua_topointer (lua_State *L, int index) {
2000-09-05 23:33:32 +04:00
StkId o = Index(L, index);
switch (ttype(o)) {
case TAG_TABLE:
return hvalue(o);
case TAG_CCLOSURE: case TAG_LCLOSURE:
return clvalue(o);
default: return NULL;
}
}
1997-09-16 23:25:59 +04:00
2000-08-28 21:57:04 +04:00
/*
** push functions (C -> stack)
*/
1997-09-16 23:25:59 +04:00
void lua_pushnil (lua_State *L) {
2000-03-10 21:37:44 +03:00
ttype(L->top) = TAG_NIL;
2000-08-28 21:57:04 +04:00
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
void lua_pushnumber (lua_State *L, double n) {
2000-03-10 21:37:44 +03:00
ttype(L->top) = TAG_NUMBER;
1999-12-01 22:50:08 +03:00
nvalue(L->top) = n;
2000-08-28 21:57:04 +04:00
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
2000-05-24 17:54:49 +04:00
void lua_pushlstring (lua_State *L, const char *s, size_t len) {
1999-12-01 22:50:08 +03:00
tsvalue(L->top) = luaS_newlstr(L, s, len);
2000-03-10 21:37:44 +03:00
ttype(L->top) = TAG_STRING;
2000-08-28 21:57:04 +04:00
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
void lua_pushstring (lua_State *L, const char *s) {
1998-03-06 19:54:42 +03:00
if (s == NULL)
lua_pushnil(L);
1998-03-06 19:54:42 +03:00
else
lua_pushlstring(L, s, strlen(s));
1998-03-06 19:54:42 +03:00
}
2000-08-28 21:57:04 +04:00
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
2000-08-28 21:57:04 +04:00
luaV_Cclosure(L, fn, n);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
2000-03-20 22:14:54 +03:00
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
1999-12-01 22:50:08 +03:00
tsvalue(L->top) = luaS_createudata(L, u, tag);
2000-03-10 21:37:44 +03:00
ttype(L->top) = TAG_USERDATA;
2000-08-28 21:57:04 +04:00
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
/*
** get functions (Lua -> stack)
*/
void lua_getglobal (lua_State *L, const char *name) {
2000-09-05 23:33:32 +04:00
StkId top = L->top;
*top = *luaV_getglobal(L, luaS_new(L, name));
2000-09-11 23:45:27 +04:00
L->top = top;
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
2000-09-18 23:39:26 +04:00
void lua_gettable (lua_State *L, int index) {
StkId t = Index(L, index);
2000-09-05 23:33:32 +04:00
StkId top = L->top;
*(top-1) = *luaV_gettable(L, t);
L->top = top; /* tag method may change top */
1997-09-16 23:25:59 +04:00
}
2000-09-18 23:39:26 +04:00
void lua_rawget (lua_State *L, int index) {
StkId t = Index(L, index);
2000-09-05 23:33:32 +04:00
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
2000-08-28 21:57:04 +04:00
}
2000-09-05 23:33:32 +04:00
void lua_rawgeti (lua_State *L, int index, int n) {
StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
*L->top = *luaH_getnum(hvalue(o), n);
2000-08-28 21:57:04 +04:00
api_incr_top(L);
}
2000-09-05 23:33:32 +04:00
void lua_getglobals (lua_State *L) {
hvalue(L->top) = L->gt;
ttype(L->top) = TAG_TABLE;
2000-08-28 21:57:04 +04:00
api_incr_top(L);
}
int lua_getref (lua_State *L, int ref) {
if (ref == LUA_REFNIL)
ttype(L->top) = TAG_NIL;
else if (0 <= ref && ref < L->refSize &&
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
*L->top = L->refArray[ref].o;
else
2000-08-28 21:57:04 +04:00
return 0;
api_incr_top(L);
return 1;
}
void lua_newtable (lua_State *L) {
hvalue(L->top) = luaH_new(L, 0);
ttype(L->top) = TAG_TABLE;
api_incr_top(L);
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
/*
** set functions (stack -> Lua)
*/
void lua_setglobal (lua_State *L, const char *name) {
2000-09-05 23:33:32 +04:00
StkId top = L->top;
luaV_setglobal(L, luaS_new(L, name));
L->top = top-1; /* remove element from the top */
2000-08-28 21:57:04 +04:00
}
2000-09-18 23:39:26 +04:00
void lua_settable (lua_State *L, int index) {
StkId t = Index(L, index);
2000-08-28 21:57:04 +04:00
StkId top = L->top;
2000-09-05 23:33:32 +04:00
luaV_settable(L, t, top-2);
L->top = top-2; /* pop index and value */
2000-08-28 21:57:04 +04:00
}
2000-09-18 23:39:26 +04:00
void lua_rawset (lua_State *L, int index) {
StkId t = Index(L, index);
2000-09-05 23:33:32 +04:00
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
L->top -= 2;
2000-08-28 21:57:04 +04:00
}
2000-09-05 23:33:32 +04:00
void lua_rawseti (lua_State *L, int index, int n) {
StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
L->top--;
2000-08-28 21:57:04 +04:00
}
2000-09-05 23:33:32 +04:00
void lua_setglobals (lua_State *L) {
StkId newtable = --L->top;
LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
L->gt = hvalue(newtable);
2000-08-28 21:57:04 +04:00
}
int lua_ref (lua_State *L, int lock) {
int ref;
if (ttype(L->top-1) == TAG_NIL)
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);
L->nblocks += sizeof(struct Ref);
2000-08-28 21:57:04 +04: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 18:09:31 +04:00
/*
** "do" functions (run Lua code)
** (most of them are in ldo.c)
*/
void lua_rawcall (lua_State *L, int nargs, int nresults) {
luaD_call(L, L->top-(nargs+1), nresults);
}
2000-08-28 21:57:04 +04:00
2000-10-02 18:47:43 +04: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)
int lua_getgcthreshold (lua_State *L) {
return GCscale(L->GCthreshold);
}
int lua_getgccount (lua_State *L) {
return GCscale(L->nblocks);
}
void lua_setgcthreshold (lua_State *L, int newthreshold) {
if (newthreshold > GCscale(ULONG_MAX))
L->GCthreshold = ULONG_MAX;
else
L->GCthreshold = GCunscale(newthreshold);
luaC_checkGC(L);
}
2000-08-28 21:57:04 +04:00
/*
2000-08-31 18:08:27 +04:00
** miscellaneous functions
2000-08-28 21:57:04 +04:00
*/
void lua_settag (lua_State *L, int tag) {
luaT_realtag(L, tag);
1999-12-01 22:50:08 +03:00
switch (ttype(L->top-1)) {
2000-03-28 00:10:21 +04:00
case TAG_TABLE:
hvalue(L->top-1)->htag = tag;
1997-09-16 23:25:59 +04:00
break;
2000-03-10 21:37:44 +03:00
case TAG_USERDATA:
tsvalue(L->top-1)->u.d.tag = tag;
1997-09-16 23:25:59 +04:00
break;
default:
luaO_verror(L, "cannot change the tag of a %.20s",
luaO_typename(L, L->top-1));
1997-09-16 23:25:59 +04:00
}
1999-12-01 22:50:08 +03:00
L->top--;
1997-09-16 23:25:59 +04:00
}
2000-08-28 21:57:04 +04:00
void lua_unref (lua_State *L, int ref) {
if (ref >= 0) {
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
2000-08-28 21:57:04 +04:00
L->refArray[ref].st = L->refFree;
L->refFree = ref;
}
}
2000-09-18 23:39:26 +04:00
int lua_next (lua_State *L, int index) {
StkId t = Index(L, index);
2000-08-31 18:08:27 +04:00
Node *n;
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
2000-08-31 18:08:27 +04:00
n = luaH_next(L, hvalue(t), Index(L, -1));
if (n) {
*(L->top-1) = *key(n);
*L->top = *val(n);
api_incr_top(L);
return 1;
}
else { /* no more elements */
2000-09-05 23:33:32 +04:00
L->top -= 1; /* remove key */
2000-08-31 18:08:27 +04:00
return 0;
}
}
int lua_getn (lua_State *L, int index) {
Hash *h = hvalue(Index(L, index));
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
if (ttype(value) == TAG_NUMBER)
return (int)nvalue(value);
else {
Number max = 0;
int i = h->size;
Node *n = h->node;
while (i--) {
if (ttype(key(n)) == TAG_NUMBER &&
ttype(val(n)) != TAG_NIL &&
nvalue(key(n)) > max)
max = nvalue(key(n));
n++;
}
return (int)max;
}
}
2000-09-05 23:33:32 +04:00
void lua_concat (lua_State *L, int n) {
StkId top = L->top;
luaV_strconc(L, n, top);
L->top = top-(n-1);
2000-10-02 18:47:43 +04:00
luaC_checkGC(L);
2000-09-05 23:33:32 +04:00
}