lua/lapi.c

417 lines
9.5 KiB
C
Raw Normal View History

1997-09-17 03:25:59 +08:00
/*
2000-03-11 02:37:44 +08:00
** $Id: lapi.c,v 1.73 2000/03/03 14:58:26 roberto Exp roberto $
1997-09-17 03:25:59 +08:00
** Lua API
** See Copyright Notice in lua.h
*/
#include <string.h>
#define LUA_REENTRANT
1997-09-17 03:25:59 +08:00
#include "lapi.h"
#include "lauxlib.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lref.h"
#include "lstate.h"
1997-09-17 03:25:59 +08:00
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lua.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
1999-12-15 02:33:29 +08:00
const TObject *luaA_protovalue (const TObject *o) {
2000-01-19 20:00:45 +08:00
switch (ttype(o)) {
2000-03-11 02:37:44 +08:00
case TAG_CCLOSURE: case TAG_LCLOSURE:
return protovalue(o);
default:
return o;
}
}
2000-03-03 22:58:26 +08:00
void luaA_checkCargs (lua_State *L, int nargs) {
if (nargs > L->top-L->Cstack.base)
luaL_verror(L, "Lua API error - "
"expected at least %d arguments in C2lua stack", nargs);
1997-09-17 03:25:59 +08:00
}
1999-12-15 02:33:29 +08:00
lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
1999-12-02 03:50:08 +08:00
luaD_openstack(L, L->Cstack.base);
*L->Cstack.base++ = *o;
return L->Cstack.base-1;
1997-09-17 03:25:59 +08:00
}
lua_Object luaA_putObjectOnTop (lua_State *L) {
1999-12-02 03:50:08 +08:00
luaD_openstack(L, L->Cstack.base);
*L->Cstack.base++ = *(--L->top);
return L->Cstack.base-1;
1997-09-17 03:25:59 +08:00
}
static void top2LC (lua_State *L, int n) {
1999-12-28 01:33:22 +08:00
/* Put the `n' elements on the top as the Lua2C contents */
1999-12-02 03:50:08 +08:00
L->Cstack.base = L->top; /* new base */
L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
L->Cstack.num = n; /* number of results */
}
lua_Object lua_pop (lua_State *L) {
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 1);
return luaA_putObjectOnTop(L);
1997-09-17 03:25:59 +08:00
}
/*
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
1999-12-02 03:50:08 +08:00
** `number' must be 1 to get the first parameter.
1997-09-17 03:25:59 +08:00
*/
lua_Object lua_lua2C (lua_State *L, int number) {
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
return L->Cstack.lua2C+number-1;
1997-09-17 03:25:59 +08:00
}
int lua_callfunction (lua_State *L, lua_Object function) {
1997-09-17 03:25:59 +08:00
if (function == LUA_NOOBJECT)
return 1;
else {
1999-12-02 03:50:08 +08:00
luaD_openstack(L, L->Cstack.base);
2000-01-19 20:00:45 +08:00
*L->Cstack.base = *function;
return luaD_protectedrun(L);
1997-09-17 03:25:59 +08:00
}
}
lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
1999-12-15 02:33:29 +08:00
return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
1997-09-17 03:25:59 +08:00
}
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
1999-12-31 02:27:03 +08:00
TObject *method;
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 1);
1999-12-31 02:27:03 +08:00
method = L->top-1;
2000-03-11 02:37:44 +08:00
if ((ttype(method) != TAG_NIL) && (*lua_type(L, method) != 'f'))
2000-03-03 22:58:26 +08:00
lua_error(L, "Lua API error - tag method must be a function or nil");
1999-12-31 02:27:03 +08:00
luaT_settagmethod(L, tag, event, method);
return luaA_putObjectOnTop(L);
1997-09-17 03:25:59 +08:00
}
lua_Object lua_seterrormethod (lua_State *L) {
lua_Object temp;
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 1);
temp = lua_getglobal(L, "_ERRORMESSAGE");
lua_setglobal(L, "_ERRORMESSAGE");
return temp;
1997-09-17 03:25:59 +08:00
}
lua_Object lua_gettable (lua_State *L) {
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 2);
luaV_gettable(L, L->top--);
return luaA_putObjectOnTop(L);
1997-09-17 03:25:59 +08:00
}
lua_Object lua_rawgettable (lua_State *L) {
2000-02-09 00:39:42 +08:00
lua_Object res;
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 2);
2000-03-11 02:37:44 +08:00
if (ttype(L->top-2) != TAG_ARRAY)
lua_error(L, "indexed expression not a table in rawgettable");
2000-02-09 00:39:42 +08:00
res = luaA_putluaObject(L, luaH_get(L, avalue(L->top-2), L->top-1));
L->top -= 2;
return res;
1997-09-17 03:25:59 +08:00
}
void lua_settable (lua_State *L) {
StkId top;
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 3);
top = L->top;
luaV_settable(L, top-3, top);
L->top = top-3; /* pop table, index, and value */
1997-09-17 03:25:59 +08:00
}
void lua_rawsettable (lua_State *L) {
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 3);
1999-12-02 03:50:08 +08:00
luaV_rawsettable(L, L->top-3);
1997-09-17 03:25:59 +08:00
}
lua_Object lua_createtable (lua_State *L) {
1997-09-17 03:25:59 +08:00
TObject o;
luaC_checkGC(L);
avalue(&o) = luaH_new(L, 0);
2000-03-11 02:37:44 +08:00
ttype(&o) = TAG_ARRAY;
1999-12-15 02:33:29 +08:00
return luaA_putluaObject(L, &o);
1997-09-17 03:25:59 +08:00
}
lua_Object lua_getglobal (lua_State *L, const char *name) {
luaV_getglobal(L, luaS_assertglobalbyname(L, name), L->top++);
return luaA_putObjectOnTop(L);
1997-09-17 03:25:59 +08:00
}
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
GlobalVar *gv = luaS_assertglobalbyname(L, name);
1999-12-15 02:33:29 +08:00
return luaA_putluaObject(L, &gv->value);
1997-09-17 03:25:59 +08:00
}
void lua_setglobal (lua_State *L, const char *name) {
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 1);
luaV_setglobal(L, luaS_assertglobalbyname(L, name), L->top--);
1997-09-17 03:25:59 +08:00
}
void lua_rawsetglobal (lua_State *L, const char *name) {
GlobalVar *gv = luaS_assertglobalbyname(L, name);
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 1);
1999-12-02 03:50:08 +08:00
gv->value = *(--L->top);
1997-09-17 03:25:59 +08:00
}
const char *lua_type (lua_State *L, lua_Object o) {
UNUSED(L);
1999-12-15 02:33:29 +08:00
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
1999-10-08 03:04:30 +08:00
}
1997-09-17 03:25:59 +08:00
int lua_isnil (lua_State *L, lua_Object o) {
UNUSED(L);
2000-03-11 02:37:44 +08:00
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_NIL);
1997-09-17 03:25:59 +08:00
}
int lua_istable (lua_State *L, lua_Object o) {
UNUSED(L);
2000-03-11 02:37:44 +08:00
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_ARRAY);
1997-09-17 03:25:59 +08:00
}
int lua_isuserdata (lua_State *L, lua_Object o) {
UNUSED(L);
2000-03-11 02:37:44 +08:00
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_USERDATA);
1997-09-17 03:25:59 +08:00
}
int lua_iscfunction (lua_State *L, lua_Object o) {
2000-03-11 02:37:44 +08:00
return (lua_tag(L, o) == TAG_CPROTO);
1997-09-17 03:25:59 +08:00
}
int lua_isnumber (lua_State *L, lua_Object o) {
UNUSED(L);
return (o != LUA_NOOBJECT) && (tonumber(o) == 0);
1997-09-17 03:25:59 +08:00
}
int lua_isstring (lua_State *L, lua_Object o) {
UNUSED(L);
2000-03-11 02:37:44 +08:00
return (o != LUA_NOOBJECT && (ttype(o) == TAG_STRING ||
ttype(o) == TAG_NUMBER));
1997-09-17 03:25:59 +08:00
}
int lua_isfunction (lua_State *L, lua_Object o) {
return *lua_type(L, o) == 'f';
1997-09-17 03:25:59 +08:00
}
int lua_equal(lua_State *L, lua_Object o1, lua_Object o2) {
UNUSED(L);
if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT)
return (o1 == o2);
2000-01-19 20:00:45 +08:00
else return luaO_equalObj(o1, o2);
1999-11-12 01:02:40 +08:00
}
1997-09-17 03:25:59 +08:00
double lua_getnumber (lua_State *L, lua_Object obj) {
UNUSED(L);
if (obj == LUA_NOOBJECT || tonumber(obj))
return 0.0;
else return (nvalue(obj));
1997-09-17 03:25:59 +08:00
}
const char *lua_getstring (lua_State *L, lua_Object obj) {
luaC_checkGC(L); /* `tostring' may create a new string */
if (obj == LUA_NOOBJECT || tostring(L, obj))
1997-09-17 03:25:59 +08:00
return NULL;
else return (svalue(obj));
1997-09-17 03:25:59 +08:00
}
long lua_strlen (lua_State *L, lua_Object obj) {
if (obj == LUA_NOOBJECT || tostring(L, obj))
1998-03-07 00:54:42 +08:00
return 0L;
else return (tsvalue(obj)->u.s.len);
1998-03-07 00:54:42 +08:00
}
void *lua_getuserdata (lua_State *L, lua_Object obj) {
UNUSED(L);
2000-03-11 02:37:44 +08:00
if (obj == LUA_NOOBJECT || ttype(obj) != TAG_USERDATA)
1997-09-17 03:25:59 +08:00
return NULL;
else return tsvalue(obj)->u.d.value;
1997-09-17 03:25:59 +08:00
}
lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
if (!lua_iscfunction(L, obj))
1997-10-25 01:17:24 +08:00
return NULL;
else return fvalue(luaA_protovalue(obj));
1997-09-17 03:25:59 +08:00
}
void lua_pushnil (lua_State *L) {
2000-03-11 02:37:44 +08:00
ttype(L->top) = TAG_NIL;
1997-09-17 03:25:59 +08:00
incr_top;
}
void lua_pushnumber (lua_State *L, double n) {
2000-03-11 02:37:44 +08:00
ttype(L->top) = TAG_NUMBER;
1999-12-02 03:50:08 +08:00
nvalue(L->top) = n;
1997-09-17 03:25:59 +08:00
incr_top;
}
void lua_pushlstring (lua_State *L, const char *s, long len) {
1999-12-02 03:50:08 +08:00
tsvalue(L->top) = luaS_newlstr(L, s, len);
2000-03-11 02:37:44 +08:00
ttype(L->top) = TAG_STRING;
1997-09-17 03:25:59 +08:00
incr_top;
luaC_checkGC(L);
1997-09-17 03:25:59 +08:00
}
void lua_pushstring (lua_State *L, const char *s) {
1998-03-07 00:54:42 +08:00
if (s == NULL)
lua_pushnil(L);
1998-03-07 00:54:42 +08:00
else
lua_pushlstring(L, s, strlen(s));
1998-03-07 00:54:42 +08:00
}
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
if (fn == NULL)
2000-03-03 22:58:26 +08:00
lua_error(L, "Lua API error - attempt to push a NULL Cfunction");
luaA_checkCargs(L, n);
2000-03-11 02:37:44 +08:00
ttype(L->top) = TAG_CPROTO;
1999-12-02 03:50:08 +08:00
fvalue(L->top) = fn;
incr_top;
luaV_closure(L, n);
luaC_checkGC(L);
1997-09-17 03:25:59 +08:00
}
void lua_pushusertag (lua_State *L, void *u, int tag) {
1997-09-17 03:25:59 +08:00
if (tag < 0 && tag != LUA_ANYTAG)
luaT_realtag(L, tag); /* error if tag is not valid */
1999-12-02 03:50:08 +08:00
tsvalue(L->top) = luaS_createudata(L, u, tag);
2000-03-11 02:37:44 +08:00
ttype(L->top) = TAG_USERDATA;
1997-09-17 03:25:59 +08:00
incr_top;
luaC_checkGC(L);
1997-09-17 03:25:59 +08:00
}
void luaA_pushobject (lua_State *L, const TObject *o) {
1999-12-02 03:50:08 +08:00
*L->top = *o;
1997-09-17 03:25:59 +08:00
incr_top;
}
void lua_pushobject (lua_State *L, lua_Object o) {
1997-09-17 03:25:59 +08:00
if (o == LUA_NOOBJECT)
2000-03-03 22:58:26 +08:00
lua_error(L, "Lua API error - attempt to push a NOOBJECT");
2000-01-19 20:00:45 +08:00
*L->top = *o;
incr_top;
1997-09-17 03:25:59 +08:00
}
int lua_tag (lua_State *L, lua_Object o) {
UNUSED(L);
if (o == LUA_NOOBJECT)
2000-03-11 02:37:44 +08:00
return TAG_NIL;
else if (ttype(o) == TAG_USERDATA) /* to allow `old' tags (deprecated) */
return o->value.ts->u.d.tag;
else
return luaT_effectivetag(o);
1997-09-17 03:25:59 +08:00
}
void lua_settag (lua_State *L, int tag) {
2000-03-03 22:58:26 +08:00
luaA_checkCargs(L, 1);
luaT_realtag(L, tag);
1999-12-02 03:50:08 +08:00
switch (ttype(L->top-1)) {
2000-03-11 02:37:44 +08:00
case TAG_ARRAY:
1999-12-02 03:50:08 +08:00
(L->top-1)->value.a->htag = tag;
1997-09-17 03:25:59 +08:00
break;
2000-03-11 02:37:44 +08:00
case TAG_USERDATA:
1999-12-02 03:50:08 +08:00
(L->top-1)->value.ts->u.d.tag = tag;
1997-09-17 03:25:59 +08:00
break;
default:
luaL_verror(L, "cannot change the tag of a %.20s",
1999-12-15 02:33:29 +08:00
luaO_typename(L->top-1));
1997-09-17 03:25:59 +08:00
}
1999-12-02 03:50:08 +08:00
L->top--;
1997-09-17 03:25:59 +08:00
}
2000-03-11 02:37:44 +08:00
GlobalVar *luaA_nextvar (lua_State *L, TString *ts) {
GlobalVar *gv;
if (ts == NULL)
gv = L->rootglobal; /* first variable */
1999-02-23 03:13:12 +08:00
else {
/* check whether name is in global var list */
luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected");
gv = ts->u.s.gv->next; /* get next */
1999-02-23 03:13:12 +08:00
}
2000-03-11 02:37:44 +08:00
while (gv && gv->value.ttype == TAG_NIL) /* skip globals with nil */
gv = gv->next;
if (gv) {
2000-03-11 02:37:44 +08:00
ttype(L->top) = TAG_STRING; tsvalue(L->top) = gv->name;
incr_top;
luaA_pushobject(L, &gv->value);
}
return gv;
1999-02-23 03:13:12 +08:00
}
const char *lua_nextvar (lua_State *L, const char *varname) {
2000-03-11 02:37:44 +08:00
TString *ts = (varname == NULL) ? NULL : luaS_new(L, varname);
GlobalVar *gv = luaA_nextvar(L, ts);
if (gv) {
top2LC(L, 2);
return gv->name->str;
1999-02-23 03:13:12 +08:00
}
else {
top2LC(L, 0);
1999-02-23 03:13:12 +08:00
return NULL;
}
}
int luaA_next (lua_State *L, const Hash *t, int i) {
1999-10-15 03:13:31 +08:00
int tsize = t->size;
1999-03-02 01:49:04 +08:00
for (; i<tsize; i++) {
Node *n = node(t, i);
2000-03-11 02:37:44 +08:00
if (ttype(val(n)) != TAG_NIL) {
luaA_pushobject(L, key(n));
luaA_pushobject(L, val(n));
1999-03-02 01:49:04 +08:00
return i+1; /* index to be used next time */
}
}
1999-03-02 01:49:04 +08:00
return 0; /* no more elements */
}
int lua_next (lua_State *L, lua_Object t, int i) {
2000-03-11 02:37:44 +08:00
if (ttype(t) != TAG_ARRAY)
2000-03-03 22:58:26 +08:00
lua_error(L, "Lua API error - object is not a table in `lua_next'");
i = luaA_next(L, avalue(t), i);
top2LC(L, (i==0) ? 0 : 2);
return i;
1999-02-23 03:13:12 +08:00
}