1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-05-14 16:24:20 +04:00
|
|
|
** $Id: lapi.c,v 1.44 1999/05/11 20:08:20 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Lua API
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.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"
|
|
|
|
|
|
|
|
|
|
|
|
char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
1999-02-23 17:57:28 +03:00
|
|
|
"$Authors: " LUA_AUTHORS " $";
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
1998-08-21 21:43:44 +04:00
|
|
|
TObject *luaA_Address (lua_Object o) {
|
|
|
|
return (o != LUA_NOOBJECT) ? Address(o) : NULL;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-03 18:45:15 +03:00
|
|
|
static lua_Type normalized_type (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void set_normalized (TObject *d, TObject *s)
|
|
|
|
{
|
|
|
|
d->value = s->value;
|
|
|
|
d->ttype = normalized_type(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-15 19:17:20 +03:00
|
|
|
static TObject *luaA_protovalue (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
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
void luaA_packresults (void)
|
|
|
|
{
|
1997-11-19 20:29:23 +03:00
|
|
|
luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-23 17:57:28 +03:00
|
|
|
int luaA_passresults (void) {
|
|
|
|
L->Cstack.base = L->Cstack.lua2C; /* position of first result */
|
1997-11-19 20:29:23 +03:00
|
|
|
return L->Cstack.num;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void checkCparams (int nParams)
|
|
|
|
{
|
1997-11-19 20:29:23 +03:00
|
|
|
if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
|
1997-09-16 23:25:59 +04:00
|
|
|
lua_error("API error - wrong number of arguments in C2lua stack");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-23 17:57:28 +03:00
|
|
|
static lua_Object put_luaObject (TObject *o) {
|
1997-11-19 20:29:23 +03:00
|
|
|
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
|
|
|
|
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-02-23 17:57:28 +03:00
|
|
|
static lua_Object put_luaObjectonTop (void) {
|
1997-11-19 20:29:23 +03:00
|
|
|
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
|
|
|
|
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-02-23 17:57:28 +03:00
|
|
|
static void top2LC (int n) {
|
|
|
|
/* 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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_pop (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
checkCparams(1);
|
|
|
|
return put_luaObjectonTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
|
|
|
** 'number' must be 1 to get the first parameter.
|
|
|
|
*/
|
|
|
|
lua_Object lua_lua2C (int number)
|
|
|
|
{
|
1997-11-19 20:29:23 +03:00
|
|
|
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
|
|
|
|
/* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lua_callfunction (lua_Object function)
|
|
|
|
{
|
|
|
|
if (function == LUA_NOOBJECT)
|
|
|
|
return 1;
|
|
|
|
else {
|
1997-11-19 20:29:23 +03:00
|
|
|
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
|
1997-12-11 17:48:46 +03:00
|
|
|
set_normalized(L->stack.stack+L->Cstack.base, Address(function));
|
1997-09-16 23:25:59 +04:00
|
|
|
return luaD_protectedrun(MULT_RET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_gettagmethod (int tag, char *event)
|
|
|
|
{
|
|
|
|
return put_luaObject(luaT_gettagmethod(tag, event));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_settagmethod (int tag, char *event)
|
|
|
|
{
|
|
|
|
checkCparams(1);
|
1997-11-19 20:29:23 +03:00
|
|
|
luaT_settagmethod(tag, event, L->stack.top-1);
|
1997-09-16 23:25:59 +04:00
|
|
|
return put_luaObjectonTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-21 21:43:44 +04:00
|
|
|
lua_Object lua_seterrormethod (void) {
|
|
|
|
lua_Object temp;
|
1997-09-16 23:25:59 +04:00
|
|
|
checkCparams(1);
|
1998-08-21 21:43:44 +04:00
|
|
|
temp = lua_getglobal("_ERRORMESSAGE");
|
|
|
|
lua_setglobal("_ERRORMESSAGE");
|
|
|
|
return temp;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_gettable (void)
|
|
|
|
{
|
|
|
|
checkCparams(2);
|
|
|
|
luaV_gettable();
|
|
|
|
return put_luaObjectonTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-12 00:08:20 +04:00
|
|
|
lua_Object lua_rawgettable (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
checkCparams(2);
|
1997-11-19 20:29:23 +03:00
|
|
|
if (ttype(L->stack.top-2) != LUA_T_ARRAY)
|
1997-12-09 16:50:08 +03:00
|
|
|
lua_error("indexed expression not a table in rawgettable");
|
1999-05-12 00:08:20 +04:00
|
|
|
*(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1);
|
|
|
|
--L->stack.top;
|
1997-09-16 23:25:59 +04:00
|
|
|
return put_luaObjectonTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 20:26:49 +03:00
|
|
|
void lua_settable (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
checkCparams(3);
|
1999-02-08 20:07:59 +03:00
|
|
|
luaV_settable(L->stack.top-3);
|
|
|
|
L->stack.top -= 2; /* pop table and index */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 20:26:49 +03:00
|
|
|
void lua_rawsettable (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
checkCparams(3);
|
1998-12-30 20:26:49 +03:00
|
|
|
luaV_rawsettable(L->stack.top-3);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_createtable (void)
|
|
|
|
{
|
|
|
|
TObject o;
|
|
|
|
luaC_checkGC();
|
|
|
|
avalue(&o) = luaH_new(0);
|
|
|
|
ttype(&o) = LUA_T_ARRAY;
|
|
|
|
return put_luaObject(&o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_getglobal (char *name)
|
|
|
|
{
|
|
|
|
luaD_checkstack(2); /* may need that to call T.M. */
|
1997-09-26 19:02:26 +04:00
|
|
|
luaV_getglobal(luaS_new(name));
|
1997-09-16 23:25:59 +04:00
|
|
|
return put_luaObjectonTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object lua_rawgetglobal (char *name)
|
|
|
|
{
|
1997-09-26 19:02:26 +04:00
|
|
|
TaggedString *ts = luaS_new(name);
|
1998-03-06 19:54:42 +03:00
|
|
|
return put_luaObject(&ts->u.s.globalval);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lua_setglobal (char *name)
|
|
|
|
{
|
|
|
|
checkCparams(1);
|
|
|
|
luaD_checkstack(2); /* may need that to call T.M. */
|
1997-09-26 19:02:26 +04:00
|
|
|
luaV_setglobal(luaS_new(name));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lua_rawsetglobal (char *name)
|
|
|
|
{
|
1997-09-26 19:02:26 +04:00
|
|
|
TaggedString *ts = luaS_new(name);
|
1997-09-16 23:25:59 +04:00
|
|
|
checkCparams(1);
|
1997-11-19 20:29:23 +03:00
|
|
|
luaS_rawsetglobal(ts, --L->stack.top);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_isnil (lua_Object o)
|
|
|
|
{
|
|
|
|
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_istable (lua_Object o)
|
|
|
|
{
|
|
|
|
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_isuserdata (lua_Object o)
|
|
|
|
{
|
|
|
|
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_iscfunction (lua_Object o)
|
|
|
|
{
|
1997-12-15 19:17:20 +03:00
|
|
|
return (lua_tag(o) == LUA_T_CPROTO);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int lua_isnumber (lua_Object o)
|
|
|
|
{
|
|
|
|
return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_isstring (lua_Object o)
|
|
|
|
{
|
|
|
|
int t = lua_tag(o);
|
|
|
|
return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_isfunction (lua_Object o)
|
|
|
|
{
|
1997-12-15 19:17:20 +03:00
|
|
|
int t = lua_tag(o);
|
|
|
|
return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-26 21:53:45 +03:00
|
|
|
double lua_getnumber (lua_Object object)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
|
|
|
if (object == LUA_NOOBJECT) return 0.0;
|
1997-11-21 22:00:46 +03:00
|
|
|
if (tonumber(Address(object))) return 0.0;
|
1997-09-16 23:25:59 +04:00
|
|
|
else return (nvalue(Address(object)));
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lua_getstring (lua_Object object)
|
|
|
|
{
|
1998-01-27 22:13:45 +03:00
|
|
|
luaC_checkGC(); /* "tostring" may create a new string */
|
1997-09-16 23:25:59 +04:00
|
|
|
if (object == LUA_NOOBJECT || tostring(Address(object)))
|
|
|
|
return NULL;
|
|
|
|
else return (svalue(Address(object)));
|
|
|
|
}
|
|
|
|
|
1998-03-06 21:47:42 +03:00
|
|
|
long lua_strlen (lua_Object object)
|
1998-03-06 19:54:42 +03:00
|
|
|
{
|
|
|
|
luaC_checkGC(); /* "tostring" may create a new string */
|
|
|
|
if (object == LUA_NOOBJECT || tostring(Address(object)))
|
|
|
|
return 0L;
|
|
|
|
else return (tsvalue(Address(object))->u.s.len);
|
|
|
|
}
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
void *lua_getuserdata (lua_Object object)
|
|
|
|
{
|
|
|
|
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
|
|
|
|
return NULL;
|
1997-09-26 19:02:26 +04:00
|
|
|
else return tsvalue(Address(object))->u.d.v;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_CFunction lua_getcfunction (lua_Object object)
|
|
|
|
{
|
1997-10-24 21:17:24 +04:00
|
|
|
if (!lua_iscfunction(object))
|
|
|
|
return NULL;
|
1997-12-15 19:17:20 +03:00
|
|
|
else return fvalue(luaA_protovalue(Address(object)));
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lua_pushnil (void)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
1997-11-26 21:53:45 +03:00
|
|
|
void lua_pushnumber (double n)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
1998-03-06 21:47:42 +03:00
|
|
|
void lua_pushlstring (char *s, long len)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
1998-03-06 19:54:42 +03:00
|
|
|
tsvalue(L->stack.top) = luaS_newlstr(s, len);
|
|
|
|
ttype(L->stack.top) = LUA_T_STRING;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
|
|
|
luaC_checkGC();
|
|
|
|
}
|
|
|
|
|
1998-03-06 19:54:42 +03:00
|
|
|
void lua_pushstring (char *s)
|
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
lua_pushnil();
|
|
|
|
else
|
1998-03-06 21:47:42 +03:00
|
|
|
lua_pushlstring(s, strlen(s));
|
1998-03-06 19:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-06-06 02:17:44 +04:00
|
|
|
void lua_pushcclosure (lua_CFunction fn, int n)
|
1997-09-16 23:25:59 +04:00
|
|
|
{
|
1997-11-27 21:25:14 +03:00
|
|
|
if (fn == NULL)
|
|
|
|
lua_error("API error - attempt to push a NULL Cfunction");
|
|
|
|
checkCparams(n);
|
|
|
|
ttype(L->stack.top) = LUA_T_CPROTO;
|
|
|
|
fvalue(L->stack.top) = fn;
|
|
|
|
incr_top;
|
|
|
|
luaV_closure(n);
|
1998-01-27 22:13:45 +03:00
|
|
|
luaC_checkGC();
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void lua_pushusertag (void *u, int tag)
|
|
|
|
{
|
|
|
|
if (tag < 0 && tag != LUA_ANYTAG)
|
|
|
|
luaT_realtag(tag); /* error if tag is not valid */
|
1997-11-19 20:29:23 +03:00
|
|
|
tsvalue(L->stack.top) = luaS_createudata(u, tag);
|
|
|
|
ttype(L->stack.top) = LUA_T_USERDATA;
|
1997-09-16 23:25:59 +04:00
|
|
|
incr_top;
|
|
|
|
luaC_checkGC();
|
|
|
|
}
|
|
|
|
|
|
|
|
void luaA_pushobject (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-05-12 00:08:20 +04:00
|
|
|
void lua_pushobject (lua_Object o) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (o == LUA_NOOBJECT)
|
|
|
|
lua_error("API error - attempt to push a NOOBJECT");
|
1999-05-12 00:08:20 +04:00
|
|
|
set_normalized(L->stack.top, Address(o));
|
|
|
|
incr_top;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lua_tag (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 {
|
|
|
|
TObject *o = Address(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:
|
1998-03-10 00:49:52 +03:00
|
|
|
LUA_INTERNALERROR("invalid type");
|
1997-12-15 19:17:20 +03:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return t;
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lua_settag (int tag)
|
|
|
|
{
|
|
|
|
checkCparams(1);
|
|
|
|
luaT_realtag(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:
|
1997-11-19 21:16:33 +03:00
|
|
|
luaL_verror("cannot change the tag of a %.20s",
|
1998-07-12 20:16:02 +04:00
|
|
|
luaO_typename(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-02-22 22:13:12 +03:00
|
|
|
TaggedString *luaA_nextvar (TaggedString *g) {
|
|
|
|
if (g == NULL)
|
|
|
|
g = (TaggedString *)L->rootglobal.next; /* first variable */
|
|
|
|
else {
|
|
|
|
/* check whether name is in global var list */
|
|
|
|
luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
|
|
|
|
g = (TaggedString *)g->head.next; /* get next */
|
|
|
|
}
|
|
|
|
while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
|
|
|
|
g = (TaggedString *)g->head.next;
|
1999-02-23 17:57:28 +03:00
|
|
|
if (g) {
|
|
|
|
ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g;
|
|
|
|
incr_top;
|
|
|
|
luaA_pushobject(&g->u.s.globalval);
|
|
|
|
}
|
1999-02-22 22:13:12 +03:00
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *lua_nextvar (char *varname) {
|
|
|
|
TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
|
|
|
|
g = luaA_nextvar(g);
|
|
|
|
if (g) {
|
1999-02-23 17:57:28 +03:00
|
|
|
top2LC(2);
|
1999-02-22 22:13:12 +03:00
|
|
|
return g->str;
|
|
|
|
}
|
1999-02-23 17:57:28 +03:00
|
|
|
else {
|
|
|
|
top2LC(0);
|
1999-02-22 22:13:12 +03:00
|
|
|
return NULL;
|
1999-02-23 17:57:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaA_next (Hash *t, int i) {
|
|
|
|
int tsize = nhash(t);
|
1999-03-01 20:49:04 +03:00
|
|
|
for (; i<tsize; i++) {
|
|
|
|
Node *n = node(t, i);
|
|
|
|
if (ttype(val(n)) != LUA_T_NIL) {
|
|
|
|
luaA_pushobject(ref(n));
|
|
|
|
luaA_pushobject(val(n));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lua_next (lua_Object o, int i) {
|
|
|
|
TObject *t = Address(o);
|
|
|
|
if (ttype(t) != LUA_T_ARRAY)
|
1999-02-26 00:07:26 +03:00
|
|
|
lua_error("API error - object is not a table in `lua_next'");
|
1999-02-23 17:57:28 +03:00
|
|
|
i = luaA_next(avalue(t), i);
|
|
|
|
top2LC((i==0) ? 0 : 2);
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
lua_State *lua_setstate (lua_State *st) {
|
|
|
|
lua_State *old = lua_state;
|
|
|
|
lua_state = st;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_LHFunction lua_setlinehook (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_CHFunction lua_setcallhook (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_setdebug (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
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
lua_Function lua_stackedfunction (int level)
|
|
|
|
{
|
|
|
|
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)
|
1997-11-19 20:29:23 +03:00
|
|
|
return Ref(L->stack.stack+i);
|
1997-12-15 19:17:20 +03:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
return LUA_NOOBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-09-07 22:59:59 +04:00
|
|
|
int lua_nups (lua_Function func) {
|
|
|
|
TObject *o = luaA_Address(func);
|
|
|
|
return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
int lua_currentline (lua_Function func)
|
|
|
|
{
|
|
|
|
TObject *f = Address(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-02-03 19:42:42 +03:00
|
|
|
lua_Object lua_getlocal (lua_Function func, int local_number, char **name) {
|
1997-12-15 19:17:20 +03:00
|
|
|
/* check whether func is a Lua function */
|
|
|
|
if (lua_tag(func) != LUA_T_PROTO)
|
1997-09-16 23:25:59 +04:00
|
|
|
return LUA_NOOBJECT;
|
1997-10-24 21:17:24 +04:00
|
|
|
else {
|
1997-12-15 19:17:20 +03:00
|
|
|
TObject *f = Address(func);
|
|
|
|
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
1997-10-24 21:17:24 +04:00
|
|
|
*name = luaF_getlocalname(fp, local_number, lua_currentline(func));
|
|
|
|
if (*name) {
|
|
|
|
/* if "*name", there must be a LUA_T_LINE */
|
|
|
|
/* therefore, f+2 points to function base */
|
1999-02-03 19:42:42 +03:00
|
|
|
return put_luaObject((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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lua_setlocal (lua_Function func, int local_number)
|
|
|
|
{
|
1997-12-15 19:17:20 +03:00
|
|
|
/* check whether func is a Lua function */
|
|
|
|
if (lua_tag(func) != LUA_T_PROTO)
|
1997-09-16 23:25:59 +04:00
|
|
|
return 0;
|
1997-12-15 19:17:20 +03:00
|
|
|
else {
|
|
|
|
TObject *f = Address(func);
|
|
|
|
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
|
|
|
char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
|
|
|
|
checkCparams(1);
|
|
|
|
--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-03-05 00:23:39 +03:00
|
|
|
void lua_funcinfo (lua_Object func, char **source, int *linedefined) {
|
1997-12-11 17:48:46 +03:00
|
|
|
if (!lua_isfunction(func))
|
1999-05-14 16:24:20 +04:00
|
|
|
lua_error("API error - `funcinfo' called with a non-function value");
|
1997-10-24 21:17:24 +04:00
|
|
|
else {
|
1997-12-15 19:17:20 +03:00
|
|
|
TObject *f = luaA_protovalue(Address(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int checkfunc (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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *lua_getobjname (lua_Object o, char **name)
|
|
|
|
{ /* try to find a name for given function */
|
1999-02-26 00:07:26 +03:00
|
|
|
set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */
|
1999-03-26 16:14:00 +03:00
|
|
|
if ((*name = luaS_travsymbol(checkfunc)) != NULL)
|
1997-09-16 23:25:59 +04:00
|
|
|
return "global";
|
1999-03-26 16:14:00 +03:00
|
|
|
else if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
|
|
|
|
return "tag-method";
|
1997-09-16 23:25:59 +04:00
|
|
|
else return "";
|
|
|
|
}
|
|
|
|
|
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-05-12 00:08:20 +04:00
|
|
|
#define MAX_C_BLOCKS 1000
|
1999-05-11 18:19:32 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void lua_beginblock (void) {
|
1999-05-12 00:08:20 +04:00
|
|
|
luaM_growvector(L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
|
|
|
|
"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-05-11 18:19:32 +04:00
|
|
|
void lua_endblock (void) {
|
1997-11-19 20:29:23 +03:00
|
|
|
--L->numCblocks;
|
|
|
|
L->Cstack = L->Cblocks[L->numCblocks];
|
|
|
|
luaD_adjusttop(L->Cstack.base);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-11 18:19:32 +04:00
|
|
|
int lua_ref (int lock) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int ref;
|
|
|
|
checkCparams(1);
|
1997-11-19 20:29:23 +03:00
|
|
|
ref = luaC_ref(L->stack.top-1, lock);
|
|
|
|
L->stack.top--;
|
1997-09-16 23:25:59 +04:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-11 18:19:32 +04:00
|
|
|
lua_Object lua_getref (int ref) {
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject *o = luaC_getref(ref);
|
|
|
|
return (o ? put_luaObject(o) : LUA_NOOBJECT);
|
|
|
|
}
|
|
|
|
|
1999-01-15 16:11:22 +03:00
|
|
|
/* }====================================================== */
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
1998-01-02 20:46:32 +03:00
|
|
|
#ifdef LUA_COMPAT2_5
|
1997-09-16 23:25:59 +04:00
|
|
|
/*
|
|
|
|
** API: set a function as a fallback
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
|
|
|
|
{
|
1997-11-19 20:29:23 +03:00
|
|
|
StkId base = (L->stack.top-L->stack.stack)-nParams;
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_openstack(nParams);
|
1997-11-19 20:29:23 +03:00
|
|
|
L->stack.stack[base].ttype = LUA_T_CPROTO;
|
|
|
|
L->stack.stack[base].value.f = f;
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_call(base+1, nResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_Object lua_setfallback (char *name, lua_CFunction fallback)
|
|
|
|
{
|
|
|
|
lua_pushstring(name);
|
|
|
|
lua_pushcfunction(fallback);
|
|
|
|
do_unprotectedrun(luaT_setfallback, 2, 1);
|
|
|
|
return put_luaObjectonTop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|