1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-11-22 16:12:07 +03:00
|
|
|
** $Id: lapi.c,v 1.56 1999/11/11 17:02:40 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Lua API
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
1999-10-04 21:51:04 +04:00
|
|
|
#include "lref.h"
|
1997-11-19 20:29:23 +03:00
|
|
|
#include "lstate.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
#include "lua.h"
|
|
|
|
#include "luadebug.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
|
|
|
"$Authors: " LUA_AUTHORS " $";
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
TObject *luaA_Address (lua_State *L, lua_Object o) {
|
|
|
|
return (o != LUA_NOOBJECT) ? Address(L, o) : NULL;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static lua_Type normalized_type (const TObject *o) {
|
1997-12-11 17:48:46 +03:00
|
|
|
int t = ttype(o);
|
|
|
|
switch (t) {
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_PMARK:
|
|
|
|
return LUA_T_PROTO;
|
|
|
|
case LUA_T_CMARK:
|
|
|
|
return LUA_T_CPROTO;
|
1998-01-09 17:44:55 +03:00
|
|
|
case LUA_T_CLMARK:
|
|
|
|
return LUA_T_CLOSURE;
|
1997-12-11 17:48:46 +03:00
|
|
|
default:
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void set_normalized (TObject *d, const TObject *s) {
|
1997-12-11 17:48:46 +03:00
|
|
|
d->value = s->value;
|
|
|
|
d->ttype = normalized_type(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static const TObject *luaA_protovalue (const TObject *o) {
|
1997-12-22 20:52:20 +03:00
|
|
|
return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
|
1997-12-15 19:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void checkCparams (lua_State *L, int nParams) {
|
1997-11-19 20:29:23 +03:00
|
|
|
if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_error(L, "API error - wrong number of arguments in C2lua stack");
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static lua_Object put_luaObject (lua_State *L, const TObject *o) {
|
|
|
|
luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
|
1997-11-19 20:29:23 +03:00
|
|
|
L->stack.stack[L->Cstack.base++] = *o;
|
|
|
|
return L->Cstack.base; /* this is +1 real position (see Ref) */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object luaA_putObjectOnTop (lua_State *L) {
|
|
|
|
luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
|
1997-11-19 20:29:23 +03:00
|
|
|
L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
|
|
|
|
return L->Cstack.base; /* this is +1 real position (see Ref) */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void top2LC (lua_State *L, int n) {
|
1999-02-23 17:57:28 +03:00
|
|
|
/* Put the 'n' elements on the top as the Lua2C contents */
|
|
|
|
L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */
|
|
|
|
L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
|
|
|
|
L->Cstack.num = n; /* number of results */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_pop (lua_State *L) {
|
|
|
|
checkCparams(L, 1);
|
|
|
|
return luaA_putObjectOnTop(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
|
|
|
** 'number' must be 1 to get the first parameter.
|
|
|
|
*/
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_lua2C (lua_State *L, int number) {
|
1997-11-19 20:29:23 +03:00
|
|
|
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
|
1999-11-22 16:12:07 +03:00
|
|
|
/* Ref(L, L->stack.stack+(L->Cstack.lua2C+number-1)) ==
|
1997-11-19 20:29:23 +03:00
|
|
|
L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
|
|
|
|
return L->Cstack.lua2C+number;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_callfunction (lua_State *L, lua_Object function) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (function == LUA_NOOBJECT)
|
|
|
|
return 1;
|
|
|
|
else {
|
1999-11-22 16:12:07 +03:00
|
|
|
luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
|
|
|
|
set_normalized(L->stack.stack+L->Cstack.base, Address(L, function));
|
|
|
|
return luaD_protectedrun(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
|
|
|
|
return put_luaObject(L, luaT_gettagmethod(L, tag, event));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
|
|
|
|
checkCparams(L, 1);
|
|
|
|
luaT_settagmethod(L, tag, event, L->stack.top-1);
|
|
|
|
return luaA_putObjectOnTop(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_seterrormethod (lua_State *L) {
|
1998-08-21 21:43:44 +04:00
|
|
|
lua_Object temp;
|
1999-11-22 16:12:07 +03:00
|
|
|
checkCparams(L, 1);
|
|
|
|
temp = lua_getglobal(L, "_ERRORMESSAGE");
|
|
|
|
lua_setglobal(L, "_ERRORMESSAGE");
|
1998-08-21 21:43:44 +04:00
|
|
|
return temp;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_gettable (lua_State *L) {
|
|
|
|
checkCparams(L, 2);
|
|
|
|
luaV_gettable(L);
|
|
|
|
return luaA_putObjectOnTop(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_rawgettable (lua_State *L) {
|
|
|
|
checkCparams(L, 2);
|
1997-11-19 20:29:23 +03:00
|
|
|
if (ttype(L->stack.top-2) != LUA_T_ARRAY)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_error(L, "indexed expression not a table in rawgettable");
|
|
|
|
*(L->stack.top-2) = *luaH_get(L, avalue(L->stack.top-2), L->stack.top-1);
|
1999-05-12 00:08:20 +04:00
|
|
|
--L->stack.top;
|
1999-11-22 16:12:07 +03:00
|
|
|
return luaA_putObjectOnTop(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_settable (lua_State *L) {
|
|
|
|
checkCparams(L, 3);
|
|
|
|
luaV_settable(L, L->stack.top-3);
|
1999-02-08 20:07:59 +03:00
|
|
|
L->stack.top -= 2; /* pop table and index */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_rawsettable (lua_State *L) {
|
|
|
|
checkCparams(L, 3);
|
|
|
|
luaV_rawsettable(L, L->stack.top-3);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_createtable (lua_State *L) {
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject o;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaC_checkGC(L);
|
|
|
|
avalue(&o) = luaH_new(L, 0);
|
1997-09-16 23:25:59 +04:00
|
|
|
ttype(&o) = LUA_T_ARRAY;
|
1999-11-22 16:12:07 +03:00
|
|
|
return put_luaObject(L, &o);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_getglobal (lua_State *L, const char *name) {
|
|
|
|
luaD_checkstack(L, 2); /* may need that to call T.M. */
|
|
|
|
luaV_getglobal(L, luaS_assertglobalbyname(L, name));
|
|
|
|
return luaA_putObjectOnTop(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
|
|
|
|
GlobalVar *gv = luaS_assertglobalbyname(L, name);
|
|
|
|
return put_luaObject(L, &gv->value);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_setglobal (lua_State *L, const char *name) {
|
|
|
|
checkCparams(L, 1);
|
|
|
|
luaD_checkstack(L, 2); /* may need that to call T.M. */
|
|
|
|
luaV_setglobal(L, luaS_assertglobalbyname(L, name));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_rawsetglobal (lua_State *L, const char *name) {
|
|
|
|
GlobalVar *gv = luaS_assertglobalbyname(L, name);
|
|
|
|
checkCparams(L, 1);
|
1999-11-04 20:23:12 +03:00
|
|
|
gv->value = *(--L->stack.top);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
const char *lua_type (lua_State *L, lua_Object o) {
|
|
|
|
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, Address(L, o));
|
1999-10-07 23:04:30 +04:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_isnil (lua_State *L, lua_Object o) {
|
|
|
|
return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_NIL);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_istable (lua_State *L, lua_Object o) {
|
|
|
|
return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_ARRAY);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_isuserdata (lua_State *L, lua_Object o) {
|
|
|
|
return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_USERDATA);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_iscfunction (lua_State *L, lua_Object o) {
|
|
|
|
return (lua_tag(L, o) == LUA_T_CPROTO);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_isnumber (lua_State *L, lua_Object o) {
|
|
|
|
return (o != LUA_NOOBJECT) && (tonumber(Address(L, o)) == 0);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_isstring (lua_State *L, lua_Object o) {
|
|
|
|
int t = lua_tag(L, o);
|
1997-09-16 23:25:59 +04:00
|
|
|
return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_isfunction (lua_State *L, lua_Object o) {
|
|
|
|
int t = lua_tag(L, o);
|
1997-12-15 19:17:20 +03:00
|
|
|
return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_equalobj (lua_State *L, lua_Object o1, lua_Object o2) {
|
1999-11-11 20:02:40 +03:00
|
|
|
if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT) return 0;
|
1999-11-22 16:12:07 +03:00
|
|
|
else return luaO_equalObj(Address(L, o1), Address(L, o2));
|
1999-11-11 20:02:40 +03:00
|
|
|
}
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
double lua_getnumber (lua_State *L, lua_Object object) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (object == LUA_NOOBJECT) return 0.0;
|
1999-11-22 16:12:07 +03:00
|
|
|
if (tonumber(Address(L, object))) return 0.0;
|
|
|
|
else return (nvalue(Address(L, object)));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
const char *lua_getstring (lua_State *L, lua_Object object) {
|
|
|
|
luaC_checkGC(L); /* `tostring' may create a new string */
|
|
|
|
if (object == LUA_NOOBJECT || tostring(L, Address(L, object)))
|
1997-09-16 23:25:59 +04:00
|
|
|
return NULL;
|
1999-11-22 16:12:07 +03:00
|
|
|
else return (svalue(Address(L, object)));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
long lua_strlen (lua_State *L, lua_Object object) {
|
|
|
|
luaC_checkGC(L); /* `tostring' may create a new string */
|
|
|
|
if (object == LUA_NOOBJECT || tostring(L, Address(L, object)))
|
1998-03-06 19:54:42 +03:00
|
|
|
return 0L;
|
1999-11-22 16:12:07 +03:00
|
|
|
else return (tsvalue(Address(L, object))->u.s.len);
|
1998-03-06 19:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void *lua_getuserdata (lua_State *L, lua_Object object) {
|
|
|
|
if (object == LUA_NOOBJECT || ttype(Address(L, object)) != LUA_T_USERDATA)
|
1997-09-16 23:25:59 +04:00
|
|
|
return NULL;
|
1999-11-22 16:12:07 +03:00
|
|
|
else return tsvalue(Address(L, object))->u.d.value;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_CFunction lua_getcfunction (lua_State *L, lua_Object object) {
|
|
|
|
if (!lua_iscfunction(L, object))
|
1997-10-24 21:17:24 +04:00
|
|
|
return NULL;
|
1999-11-22 16:12:07 +03:00
|
|
|
else return fvalue(luaA_protovalue(Address(L, object)));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushnil (lua_State *L) {
|
1997-11-19 20:29:23 +03:00
|
|
|
ttype(L->stack.top) = LUA_T_NIL;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushnumber (lua_State *L, double n) {
|
1997-11-19 20:29:23 +03:00
|
|
|
ttype(L->stack.top) = LUA_T_NUMBER;
|
|
|
|
nvalue(L->stack.top) = n;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushlstring (lua_State *L, const char *s, long len) {
|
|
|
|
tsvalue(L->stack.top) = luaS_newlstr(L, s, len);
|
1998-03-06 19:54:42 +03:00
|
|
|
ttype(L->stack.top) = LUA_T_STRING;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaC_checkGC(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushstring (lua_State *L, const char *s) {
|
1998-03-06 19:54:42 +03:00
|
|
|
if (s == NULL)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_pushnil(L);
|
1998-03-06 19:54:42 +03:00
|
|
|
else
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_pushlstring(L, s, strlen(s));
|
1998-03-06 19:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
1997-11-27 21:25:14 +03:00
|
|
|
if (fn == NULL)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_error(L, "API error - attempt to push a NULL Cfunction");
|
|
|
|
checkCparams(L, n);
|
1997-11-27 21:25:14 +03:00
|
|
|
ttype(L->stack.top) = LUA_T_CPROTO;
|
|
|
|
fvalue(L->stack.top) = fn;
|
|
|
|
incr_top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaV_closure(L, n);
|
|
|
|
luaC_checkGC(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushusertag (lua_State *L, void *u, int tag) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (tag < 0 && tag != LUA_ANYTAG)
|
1999-11-22 16:12:07 +03:00
|
|
|
luaT_realtag(L, tag); /* error if tag is not valid */
|
|
|
|
tsvalue(L->stack.top) = luaS_createudata(L, u, tag);
|
1997-11-19 20:29:23 +03:00
|
|
|
ttype(L->stack.top) = LUA_T_USERDATA;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaC_checkGC(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaA_pushobject (lua_State *L, const TObject *o) {
|
1997-11-19 20:29:23 +03:00
|
|
|
*L->stack.top = *o;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_pushobject (lua_State *L, lua_Object o) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (o == LUA_NOOBJECT)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_error(L, "API error - attempt to push a NOOBJECT");
|
|
|
|
set_normalized(L->stack.top, Address(L, o));
|
1999-05-12 00:08:20 +04:00
|
|
|
incr_top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_tag (lua_State *L, lua_Object lo) {
|
1997-12-15 19:17:20 +03:00
|
|
|
if (lo == LUA_NOOBJECT)
|
|
|
|
return LUA_T_NIL;
|
1997-09-16 23:25:59 +04:00
|
|
|
else {
|
1999-11-22 16:12:07 +03:00
|
|
|
const TObject *o = Address(L, lo);
|
1997-12-15 19:17:20 +03:00
|
|
|
int t;
|
|
|
|
switch (t = ttype(o)) {
|
|
|
|
case LUA_T_USERDATA:
|
|
|
|
return o->value.ts->u.d.tag;
|
|
|
|
case LUA_T_ARRAY:
|
|
|
|
return o->value.a->htag;
|
|
|
|
case LUA_T_PMARK:
|
|
|
|
return LUA_T_PROTO;
|
|
|
|
case LUA_T_CMARK:
|
|
|
|
return LUA_T_CPROTO;
|
1998-01-09 17:44:55 +03:00
|
|
|
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
|
|
|
return o->value.cl->consts[0].ttype;
|
1997-12-15 19:17:20 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
case LUA_T_LINE:
|
1999-11-22 16:12:07 +03:00
|
|
|
LUA_INTERNALERROR(L, "invalid type");
|
1997-12-15 19:17:20 +03:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return t;
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_settag (lua_State *L, int tag) {
|
|
|
|
checkCparams(L, 1);
|
|
|
|
luaT_realtag(L, tag);
|
1997-11-19 20:29:23 +03:00
|
|
|
switch (ttype(L->stack.top-1)) {
|
1997-09-16 23:25:59 +04:00
|
|
|
case LUA_T_ARRAY:
|
1997-11-19 20:29:23 +03:00
|
|
|
(L->stack.top-1)->value.a->htag = tag;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
case LUA_T_USERDATA:
|
1997-11-19 20:29:23 +03:00
|
|
|
(L->stack.top-1)->value.ts->u.d.tag = tag;
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
default:
|
1999-11-22 16:12:07 +03:00
|
|
|
luaL_verror(L, "cannot change the tag of a %.20s",
|
|
|
|
luaO_typename(L, L->stack.top-1));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
1997-11-19 20:29:23 +03:00
|
|
|
L->stack.top--;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
GlobalVar *luaA_nextvar (lua_State *L, TaggedString *ts) {
|
1999-11-04 20:23:12 +03:00
|
|
|
GlobalVar *gv;
|
|
|
|
if (ts == NULL)
|
|
|
|
gv = L->rootglobal; /* first variable */
|
1999-02-22 22:13:12 +03:00
|
|
|
else {
|
|
|
|
/* check whether name is in global var list */
|
1999-11-22 16:12:07 +03:00
|
|
|
luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected");
|
1999-11-04 20:23:12 +03:00
|
|
|
gv = ts->u.s.gv->next; /* get next */
|
1999-02-22 22:13:12 +03:00
|
|
|
}
|
1999-11-04 20:23:12 +03:00
|
|
|
while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */
|
|
|
|
gv = gv->next;
|
|
|
|
if (gv) {
|
|
|
|
ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name;
|
1999-02-23 17:57:28 +03:00
|
|
|
incr_top;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaA_pushobject(L, &gv->value);
|
1999-02-23 17:57:28 +03:00
|
|
|
}
|
1999-11-04 20:23:12 +03:00
|
|
|
return gv;
|
1999-02-22 22:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
const char *lua_nextvar (lua_State *L, const char *varname) {
|
|
|
|
TaggedString *ts = (varname == NULL) ? NULL : luaS_new(L, varname);
|
|
|
|
GlobalVar *gv = luaA_nextvar(L, ts);
|
1999-11-04 20:23:12 +03:00
|
|
|
if (gv) {
|
1999-11-22 16:12:07 +03:00
|
|
|
top2LC(L, 2);
|
1999-11-04 20:23:12 +03:00
|
|
|
return gv->name->str;
|
1999-02-22 22:13:12 +03:00
|
|
|
}
|
1999-02-23 17:57:28 +03:00
|
|
|
else {
|
1999-11-22 16:12:07 +03:00
|
|
|
top2LC(L, 0);
|
1999-02-22 22:13:12 +03:00
|
|
|
return NULL;
|
1999-02-23 17:57:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int luaA_next (lua_State *L, const Hash *t, int i) {
|
1999-10-14 23:13:31 +04:00
|
|
|
int tsize = t->size;
|
1999-03-01 20:49:04 +03:00
|
|
|
for (; i<tsize; i++) {
|
1999-11-22 16:12:07 +03:00
|
|
|
Node *n = node(L, t, i);
|
|
|
|
if (ttype(val(L, n)) != LUA_T_NIL) {
|
|
|
|
luaA_pushobject(L, key(L, n));
|
|
|
|
luaA_pushobject(L, val(L, n));
|
1999-03-01 20:49:04 +03:00
|
|
|
return i+1; /* index to be used next time */
|
|
|
|
}
|
1999-02-23 17:57:28 +03:00
|
|
|
}
|
1999-03-01 20:49:04 +03:00
|
|
|
return 0; /* no more elements */
|
1999-02-23 17:57:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_next (lua_State *L, lua_Object o, int i) {
|
|
|
|
const TObject *t = Address(L, o);
|
1999-02-23 17:57:28 +03:00
|
|
|
if (ttype(t) != LUA_T_ARRAY)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_error(L, "API error - object is not a table in `lua_next'");
|
|
|
|
i = luaA_next(L, avalue(t), i);
|
|
|
|
top2LC(L, (i==0) ? 0 : 2);
|
1999-02-23 17:57:28 +03:00
|
|
|
return i;
|
1999-02-22 22:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-15 16:11:22 +03:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-01-15 16:11:22 +03:00
|
|
|
** {======================================================
|
1999-02-22 22:13:12 +03:00
|
|
|
** To manipulate some state information
|
1997-09-16 23:25:59 +04:00
|
|
|
** =======================================================
|
1999-01-15 16:11:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
|
|
|
|
lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
|
1999-02-04 20:47:59 +03:00
|
|
|
lua_LHFunction old = L->linehook;
|
|
|
|
L->linehook = func;
|
1999-01-15 16:11:22 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
|
1999-02-04 20:47:59 +03:00
|
|
|
lua_CHFunction old = L->callhook;
|
|
|
|
L->callhook = func;
|
1999-01-15 16:11:22 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_setdebug (lua_State *L, int debug) {
|
1999-02-04 20:47:59 +03:00
|
|
|
int old = L->debug;
|
|
|
|
L->debug = debug;
|
1999-01-15 16:11:22 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
1997-09-16 23:25:59 +04:00
|
|
|
** Debug interface
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Function lua_stackedfunction (lua_State *L, int level) {
|
1997-09-16 23:25:59 +04:00
|
|
|
StkId i;
|
1997-12-15 19:17:20 +03:00
|
|
|
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
|
|
|
|
int t = L->stack.stack[i].ttype;
|
|
|
|
if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
|
1997-09-16 23:25:59 +04:00
|
|
|
if (level-- == 0)
|
1999-11-22 16:12:07 +03:00
|
|
|
return Ref(L, L->stack.stack+i);
|
1997-12-15 19:17:20 +03:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
return LUA_NOOBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_nups (lua_State *L, lua_Function func) {
|
|
|
|
const TObject *o = luaA_Address(L, func);
|
1998-09-07 22:59:59 +04:00
|
|
|
return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_currentline (lua_State *L, lua_Function func) {
|
|
|
|
const TObject *f = Address(L, func);
|
1997-12-15 19:17:20 +03:00
|
|
|
return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
|
|
|
|
(f+1)->value.i : -1;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_getlocal (lua_State *L, lua_Function func, int local_number,
|
1999-08-17 00:52:00 +04:00
|
|
|
const char **name) {
|
1997-12-15 19:17:20 +03:00
|
|
|
/* check whether func is a Lua function */
|
1999-11-22 16:12:07 +03:00
|
|
|
if (lua_tag(L, func) != LUA_T_PROTO)
|
1997-09-16 23:25:59 +04:00
|
|
|
return LUA_NOOBJECT;
|
1997-10-24 21:17:24 +04:00
|
|
|
else {
|
1999-11-22 16:12:07 +03:00
|
|
|
TObject *f = Address(L, func);
|
1997-12-15 19:17:20 +03:00
|
|
|
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
1999-11-22 16:12:07 +03:00
|
|
|
*name = luaF_getlocalname(fp, local_number, lua_currentline(L, func));
|
1997-10-24 21:17:24 +04:00
|
|
|
if (*name) {
|
|
|
|
/* if "*name", there must be a LUA_T_LINE */
|
|
|
|
/* therefore, f+2 points to function base */
|
1999-11-22 16:12:07 +03:00
|
|
|
return put_luaObject(L, (f+2)+(local_number-1));
|
1997-10-24 21:17:24 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return LUA_NOOBJECT;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_setlocal (lua_State *L, lua_Function func, int local_number) {
|
1997-12-15 19:17:20 +03:00
|
|
|
/* check whether func is a Lua function */
|
1999-11-22 16:12:07 +03:00
|
|
|
if (lua_tag(L, func) != LUA_T_PROTO)
|
1997-09-16 23:25:59 +04:00
|
|
|
return 0;
|
1997-12-15 19:17:20 +03:00
|
|
|
else {
|
1999-11-22 16:12:07 +03:00
|
|
|
TObject *f = Address(L, func);
|
1997-12-15 19:17:20 +03:00
|
|
|
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
1999-11-22 16:12:07 +03:00
|
|
|
const char *name = luaF_getlocalname(fp, local_number,
|
|
|
|
lua_currentline(L, func));
|
|
|
|
checkCparams(L, 1);
|
1997-12-15 19:17:20 +03:00
|
|
|
--L->stack.top;
|
|
|
|
if (name) {
|
|
|
|
/* if "name", there must be a LUA_T_LINE */
|
|
|
|
/* therefore, f+2 points to function base */
|
|
|
|
*((f+2)+(local_number-1)) = *L->stack.top;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_funcinfo (lua_State *L, lua_Object func,
|
|
|
|
const char **source, int *linedefined) {
|
|
|
|
if (!lua_isfunction(L, func))
|
|
|
|
lua_error(L, "API error - `funcinfo' called with a non-function value");
|
1997-10-24 21:17:24 +04:00
|
|
|
else {
|
1999-11-22 16:12:07 +03:00
|
|
|
const TObject *f = luaA_protovalue(Address(L, func));
|
1997-12-22 20:52:20 +03:00
|
|
|
if (normalized_type(f) == LUA_T_PROTO) {
|
1999-03-05 00:23:39 +03:00
|
|
|
*source = tfvalue(f)->source->str;
|
1997-10-24 21:17:24 +04:00
|
|
|
*linedefined = tfvalue(f)->lineDefined;
|
|
|
|
}
|
|
|
|
else {
|
1999-03-05 00:23:39 +03:00
|
|
|
*source = "(C)";
|
1997-10-24 21:17:24 +04:00
|
|
|
*linedefined = -1;
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static int checkfunc (lua_State *L, TObject *o) {
|
1997-12-11 17:48:46 +03:00
|
|
|
return luaO_equalObj(o, L->stack.top);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
|
1999-08-17 00:52:00 +04:00
|
|
|
/* try to find a name for given function */
|
1999-11-04 20:23:12 +03:00
|
|
|
GlobalVar *g;
|
1999-11-22 16:12:07 +03:00
|
|
|
set_normalized(L->stack.top, Address(L, o)); /* to be used by `checkfunc' */
|
1999-11-04 20:23:12 +03:00
|
|
|
for (g=L->rootglobal; g; g=g->next) {
|
1999-11-22 16:12:07 +03:00
|
|
|
if (checkfunc(L, &g->value)) {
|
1999-11-04 20:23:12 +03:00
|
|
|
*name = g->name->str;
|
1999-10-04 21:51:04 +04:00
|
|
|
return "global";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not found: try tag methods */
|
1999-11-22 16:12:07 +03:00
|
|
|
if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
|
1999-03-26 16:14:00 +03:00
|
|
|
return "tag-method";
|
1999-10-04 21:51:04 +04:00
|
|
|
else return ""; /* not found at all */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-01-15 16:11:22 +03:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-01-15 16:11:22 +03:00
|
|
|
** {======================================================
|
1997-09-16 23:25:59 +04:00
|
|
|
** BLOCK mechanism
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-05-11 18:19:32 +04:00
|
|
|
#ifndef MAX_C_BLOCKS
|
1999-06-17 21:04:03 +04:00
|
|
|
#define MAX_C_BLOCKS 1000 /* arbitrary limit */
|
1999-05-11 18:19:32 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_beginblock (lua_State *L) {
|
|
|
|
luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
|
1999-05-12 00:08:20 +04:00
|
|
|
"too many nested blocks", MAX_C_BLOCKS);
|
1997-11-19 20:29:23 +03:00
|
|
|
L->Cblocks[L->numCblocks] = L->Cstack;
|
|
|
|
L->numCblocks++;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_endblock (lua_State *L) {
|
1997-11-19 20:29:23 +03:00
|
|
|
--L->numCblocks;
|
|
|
|
L->Cstack = L->Cblocks[L->numCblocks];
|
1999-11-22 16:12:07 +03:00
|
|
|
luaD_adjusttop(L, L->Cstack.base);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int lua_ref (lua_State *L, int lock) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int ref;
|
1999-11-22 16:12:07 +03:00
|
|
|
checkCparams(L, 1);
|
|
|
|
ref = luaR_ref(L, L->stack.top-1, lock);
|
1997-11-19 20:29:23 +03:00
|
|
|
L->stack.top--;
|
1997-09-16 23:25:59 +04:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_Object lua_getref (lua_State *L, int ref) {
|
|
|
|
const TObject *o = luaR_getref(L, ref);
|
|
|
|
return (o ? put_luaObject(L, o) : LUA_NOOBJECT);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
1999-01-15 16:11:22 +03:00
|
|
|
/* }====================================================== */
|
|
|
|
|