|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
-** $Id: lapi.c,v 1.46 1999/06/17 17:04:03 roberto Exp roberto $
|
|
|
+** $Id: lapi.c,v 1.47 1999/06/22 20:37:23 roberto Exp roberto $
|
|
|
** Lua API
|
|
|
** See Copyright Notice in lua.h
|
|
|
*/
|
|
@@ -24,8 +24,8 @@
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
-char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
|
|
- "$Authors: " LUA_AUTHORS " $";
|
|
|
+const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
|
|
+ "$Authors: " LUA_AUTHORS " $";
|
|
|
|
|
|
|
|
|
|
|
@@ -34,8 +34,7 @@ TObject *luaA_Address (lua_Object o) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static lua_Type normalized_type (TObject *o)
|
|
|
-{
|
|
|
+static lua_Type normalized_type (const TObject *o) {
|
|
|
int t = ttype(o);
|
|
|
switch (t) {
|
|
|
case LUA_T_PMARK:
|
|
@@ -50,21 +49,18 @@ static lua_Type normalized_type (TObject *o)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void set_normalized (TObject *d, TObject *s)
|
|
|
-{
|
|
|
+static void set_normalized (TObject *d, const TObject *s) {
|
|
|
d->value = s->value;
|
|
|
d->ttype = normalized_type(s);
|
|
|
}
|
|
|
|
|
|
|
|
|
-static TObject *luaA_protovalue (TObject *o)
|
|
|
-{
|
|
|
+static const TObject *luaA_protovalue (const TObject *o) {
|
|
|
return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
|
|
|
}
|
|
|
|
|
|
|
|
|
-void luaA_packresults (void)
|
|
|
-{
|
|
|
+void luaA_packresults (void) {
|
|
|
luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
|
|
|
incr_top;
|
|
|
}
|
|
@@ -76,14 +72,13 @@ int luaA_passresults (void) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void checkCparams (int nParams)
|
|
|
-{
|
|
|
+static void checkCparams (int nParams) {
|
|
|
if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
|
|
|
lua_error("API error - wrong number of arguments in C2lua stack");
|
|
|
}
|
|
|
|
|
|
|
|
|
-static lua_Object put_luaObject (TObject *o) {
|
|
|
+static lua_Object put_luaObject (const TObject *o) {
|
|
|
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) */
|
|
@@ -115,8 +110,7 @@ lua_Object lua_pop (void) {
|
|
|
** 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)
|
|
|
-{
|
|
|
+lua_Object lua_lua2C (int number) {
|
|
|
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 == */
|
|
@@ -124,8 +118,7 @@ lua_Object lua_lua2C (int number)
|
|
|
}
|
|
|
|
|
|
|
|
|
-int lua_callfunction (lua_Object function)
|
|
|
-{
|
|
|
+int lua_callfunction (lua_Object function) {
|
|
|
if (function == LUA_NOOBJECT)
|
|
|
return 1;
|
|
|
else {
|
|
@@ -136,14 +129,12 @@ int lua_callfunction (lua_Object function)
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_gettagmethod (int tag, char *event)
|
|
|
-{
|
|
|
+lua_Object lua_gettagmethod (int tag, const char *event) {
|
|
|
return put_luaObject(luaT_gettagmethod(tag, event));
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_settagmethod (int tag, char *event)
|
|
|
-{
|
|
|
+lua_Object lua_settagmethod (int tag, const char *event) {
|
|
|
checkCparams(1);
|
|
|
luaT_settagmethod(tag, event, L->stack.top-1);
|
|
|
return put_luaObjectonTop();
|
|
@@ -159,8 +150,7 @@ lua_Object lua_seterrormethod (void) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_gettable (void)
|
|
|
-{
|
|
|
+lua_Object lua_gettable (void) {
|
|
|
checkCparams(2);
|
|
|
luaV_gettable();
|
|
|
return put_luaObjectonTop();
|
|
@@ -190,8 +180,7 @@ void lua_rawsettable (void) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_createtable (void)
|
|
|
-{
|
|
|
+lua_Object lua_createtable (void) {
|
|
|
TObject o;
|
|
|
luaC_checkGC();
|
|
|
avalue(&o) = luaH_new(0);
|
|
@@ -200,31 +189,27 @@ lua_Object lua_createtable (void)
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_getglobal (char *name)
|
|
|
-{
|
|
|
+lua_Object lua_getglobal (const char *name) {
|
|
|
luaD_checkstack(2); /* may need that to call T.M. */
|
|
|
luaV_getglobal(luaS_new(name));
|
|
|
return put_luaObjectonTop();
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_rawgetglobal (char *name)
|
|
|
-{
|
|
|
+lua_Object lua_rawgetglobal (const char *name) {
|
|
|
TaggedString *ts = luaS_new(name);
|
|
|
return put_luaObject(&ts->u.s.globalval);
|
|
|
}
|
|
|
|
|
|
|
|
|
-void lua_setglobal (char *name)
|
|
|
-{
|
|
|
+void lua_setglobal (const char *name) {
|
|
|
checkCparams(1);
|
|
|
luaD_checkstack(2); /* may need that to call T.M. */
|
|
|
luaV_setglobal(luaS_new(name));
|
|
|
}
|
|
|
|
|
|
|
|
|
-void lua_rawsetglobal (char *name)
|
|
|
-{
|
|
|
+void lua_rawsetglobal (const char *name) {
|
|
|
TaggedString *ts = luaS_new(name);
|
|
|
checkCparams(1);
|
|
|
luaS_rawsetglobal(ts, --L->stack.top);
|
|
@@ -232,113 +217,96 @@ void lua_rawsetglobal (char *name)
|
|
|
|
|
|
|
|
|
|
|
|
-int lua_isnil (lua_Object o)
|
|
|
-{
|
|
|
+int lua_isnil (lua_Object o) {
|
|
|
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
|
|
|
}
|
|
|
|
|
|
-int lua_istable (lua_Object o)
|
|
|
-{
|
|
|
+int lua_istable (lua_Object o) {
|
|
|
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
|
|
|
}
|
|
|
|
|
|
-int lua_isuserdata (lua_Object o)
|
|
|
-{
|
|
|
+int lua_isuserdata (lua_Object o) {
|
|
|
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
|
|
|
}
|
|
|
|
|
|
-int lua_iscfunction (lua_Object o)
|
|
|
-{
|
|
|
+int lua_iscfunction (lua_Object o) {
|
|
|
return (lua_tag(o) == LUA_T_CPROTO);
|
|
|
}
|
|
|
|
|
|
-int lua_isnumber (lua_Object o)
|
|
|
-{
|
|
|
+int lua_isnumber (lua_Object o) {
|
|
|
return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
|
|
|
}
|
|
|
|
|
|
-int lua_isstring (lua_Object o)
|
|
|
-{
|
|
|
+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)
|
|
|
-{
|
|
|
+int lua_isfunction (lua_Object o) {
|
|
|
int t = lua_tag(o);
|
|
|
return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
|
|
|
}
|
|
|
|
|
|
|
|
|
-double lua_getnumber (lua_Object object)
|
|
|
-{
|
|
|
+double lua_getnumber (lua_Object object) {
|
|
|
if (object == LUA_NOOBJECT) return 0.0;
|
|
|
if (tonumber(Address(object))) return 0.0;
|
|
|
else return (nvalue(Address(object)));
|
|
|
}
|
|
|
|
|
|
-char *lua_getstring (lua_Object object)
|
|
|
-{
|
|
|
+const char *lua_getstring (lua_Object object) {
|
|
|
luaC_checkGC(); /* "tostring" may create a new string */
|
|
|
if (object == LUA_NOOBJECT || tostring(Address(object)))
|
|
|
return NULL;
|
|
|
else return (svalue(Address(object)));
|
|
|
}
|
|
|
|
|
|
-long lua_strlen (lua_Object object)
|
|
|
-{
|
|
|
+long lua_strlen (lua_Object object) {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
-void *lua_getuserdata (lua_Object object)
|
|
|
-{
|
|
|
+void *lua_getuserdata (lua_Object object) {
|
|
|
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
|
|
|
return NULL;
|
|
|
else return tsvalue(Address(object))->u.d.v;
|
|
|
}
|
|
|
|
|
|
-lua_CFunction lua_getcfunction (lua_Object object)
|
|
|
-{
|
|
|
+lua_CFunction lua_getcfunction (lua_Object object) {
|
|
|
if (!lua_iscfunction(object))
|
|
|
return NULL;
|
|
|
else return fvalue(luaA_protovalue(Address(object)));
|
|
|
}
|
|
|
|
|
|
|
|
|
-void lua_pushnil (void)
|
|
|
-{
|
|
|
+void lua_pushnil (void) {
|
|
|
ttype(L->stack.top) = LUA_T_NIL;
|
|
|
incr_top;
|
|
|
}
|
|
|
|
|
|
-void lua_pushnumber (double n)
|
|
|
-{
|
|
|
+void lua_pushnumber (double n) {
|
|
|
ttype(L->stack.top) = LUA_T_NUMBER;
|
|
|
nvalue(L->stack.top) = n;
|
|
|
incr_top;
|
|
|
}
|
|
|
|
|
|
-void lua_pushlstring (char *s, long len)
|
|
|
-{
|
|
|
+void lua_pushlstring (const char *s, long len) {
|
|
|
tsvalue(L->stack.top) = luaS_newlstr(s, len);
|
|
|
ttype(L->stack.top) = LUA_T_STRING;
|
|
|
incr_top;
|
|
|
luaC_checkGC();
|
|
|
}
|
|
|
|
|
|
-void lua_pushstring (char *s)
|
|
|
-{
|
|
|
+void lua_pushstring (const char *s) {
|
|
|
if (s == NULL)
|
|
|
lua_pushnil();
|
|
|
else
|
|
|
lua_pushlstring(s, strlen(s));
|
|
|
}
|
|
|
|
|
|
-void lua_pushcclosure (lua_CFunction fn, int n)
|
|
|
-{
|
|
|
+void lua_pushcclosure (lua_CFunction fn, int n) {
|
|
|
if (fn == NULL)
|
|
|
lua_error("API error - attempt to push a NULL Cfunction");
|
|
|
checkCparams(n);
|
|
@@ -349,8 +317,7 @@ void lua_pushcclosure (lua_CFunction fn, int n)
|
|
|
luaC_checkGC();
|
|
|
}
|
|
|
|
|
|
-void lua_pushusertag (void *u, int tag)
|
|
|
-{
|
|
|
+void lua_pushusertag (void *u, int tag) {
|
|
|
if (tag < 0 && tag != LUA_ANYTAG)
|
|
|
luaT_realtag(tag); /* error if tag is not valid */
|
|
|
tsvalue(L->stack.top) = luaS_createudata(u, tag);
|
|
@@ -359,8 +326,7 @@ void lua_pushusertag (void *u, int tag)
|
|
|
luaC_checkGC();
|
|
|
}
|
|
|
|
|
|
-void luaA_pushobject (TObject *o)
|
|
|
-{
|
|
|
+void luaA_pushobject (const TObject *o) {
|
|
|
*L->stack.top = *o;
|
|
|
incr_top;
|
|
|
}
|
|
@@ -373,12 +339,11 @@ void lua_pushobject (lua_Object o) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-int lua_tag (lua_Object lo)
|
|
|
-{
|
|
|
+int lua_tag (lua_Object lo) {
|
|
|
if (lo == LUA_NOOBJECT)
|
|
|
return LUA_T_NIL;
|
|
|
else {
|
|
|
- TObject *o = Address(lo);
|
|
|
+ const TObject *o = Address(lo);
|
|
|
int t;
|
|
|
switch (t = ttype(o)) {
|
|
|
case LUA_T_USERDATA:
|
|
@@ -402,8 +367,7 @@ int lua_tag (lua_Object lo)
|
|
|
}
|
|
|
|
|
|
|
|
|
-void lua_settag (int tag)
|
|
|
-{
|
|
|
+void lua_settag (int tag) {
|
|
|
checkCparams(1);
|
|
|
luaT_realtag(tag);
|
|
|
switch (ttype(L->stack.top-1)) {
|
|
@@ -440,7 +404,7 @@ TaggedString *luaA_nextvar (TaggedString *g) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-char *lua_nextvar (char *varname) {
|
|
|
+const char *lua_nextvar (const char *varname) {
|
|
|
TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
|
|
|
g = luaA_nextvar(g);
|
|
|
if (g) {
|
|
@@ -454,7 +418,7 @@ char *lua_nextvar (char *varname) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-int luaA_next (Hash *t, int i) {
|
|
|
+int luaA_next (const Hash *t, int i) {
|
|
|
int tsize = nhash(t);
|
|
|
for (; i<tsize; i++) {
|
|
|
Node *n = node(t, i);
|
|
@@ -469,7 +433,7 @@ int luaA_next (Hash *t, int i) {
|
|
|
|
|
|
|
|
|
int lua_next (lua_Object o, int i) {
|
|
|
- TObject *t = Address(o);
|
|
|
+ const TObject *t = Address(o);
|
|
|
if (ttype(t) != LUA_T_ARRAY)
|
|
|
lua_error("API error - object is not a table in `lua_next'");
|
|
|
i = luaA_next(avalue(t), i);
|
|
@@ -519,8 +483,7 @@ int lua_setdebug (int debug) {
|
|
|
*/
|
|
|
|
|
|
|
|
|
-lua_Function lua_stackedfunction (int level)
|
|
|
-{
|
|
|
+lua_Function lua_stackedfunction (int level) {
|
|
|
StkId i;
|
|
|
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
|
|
|
int t = L->stack.stack[i].ttype;
|
|
@@ -533,20 +496,20 @@ lua_Function lua_stackedfunction (int level)
|
|
|
|
|
|
|
|
|
int lua_nups (lua_Function func) {
|
|
|
- TObject *o = luaA_Address(func);
|
|
|
+ const TObject *o = luaA_Address(func);
|
|
|
return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
|
|
|
}
|
|
|
|
|
|
|
|
|
-int lua_currentline (lua_Function func)
|
|
|
-{
|
|
|
- TObject *f = Address(func);
|
|
|
+int lua_currentline (lua_Function func) {
|
|
|
+ const TObject *f = Address(func);
|
|
|
return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
|
|
|
(f+1)->value.i : -1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-lua_Object lua_getlocal (lua_Function func, int local_number, char **name) {
|
|
|
+lua_Object lua_getlocal (lua_Function func, int local_number,
|
|
|
+ const char **name) {
|
|
|
/* check whether func is a Lua function */
|
|
|
if (lua_tag(func) != LUA_T_PROTO)
|
|
|
return LUA_NOOBJECT;
|
|
@@ -565,15 +528,14 @@ lua_Object lua_getlocal (lua_Function func, int local_number, char **name) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-int lua_setlocal (lua_Function func, int local_number)
|
|
|
-{
|
|
|
+int lua_setlocal (lua_Function func, int local_number) {
|
|
|
/* check whether func is a Lua function */
|
|
|
if (lua_tag(func) != LUA_T_PROTO)
|
|
|
return 0;
|
|
|
else {
|
|
|
TObject *f = Address(func);
|
|
|
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
|
|
- char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
|
|
|
+ const char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
|
|
|
checkCparams(1);
|
|
|
--L->stack.top;
|
|
|
if (name) {
|
|
@@ -588,11 +550,11 @@ int lua_setlocal (lua_Function func, int local_number)
|
|
|
}
|
|
|
|
|
|
|
|
|
-void lua_funcinfo (lua_Object func, char **source, int *linedefined) {
|
|
|
+void lua_funcinfo (lua_Object func, const char **source, int *linedefined) {
|
|
|
if (!lua_isfunction(func))
|
|
|
lua_error("API error - `funcinfo' called with a non-function value");
|
|
|
else {
|
|
|
- TObject *f = luaA_protovalue(Address(func));
|
|
|
+ const TObject *f = luaA_protovalue(Address(func));
|
|
|
if (normalized_type(f) == LUA_T_PROTO) {
|
|
|
*source = tfvalue(f)->source->str;
|
|
|
*linedefined = tfvalue(f)->lineDefined;
|
|
@@ -605,14 +567,13 @@ void lua_funcinfo (lua_Object func, char **source, int *linedefined) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int checkfunc (TObject *o)
|
|
|
-{
|
|
|
+static int checkfunc (TObject *o) {
|
|
|
return luaO_equalObj(o, L->stack.top);
|
|
|
}
|
|
|
|
|
|
|
|
|
-char *lua_getobjname (lua_Object o, char **name)
|
|
|
-{ /* try to find a name for given function */
|
|
|
+const char *lua_getobjname (lua_Object o, const char **name) {
|
|
|
+ /* try to find a name for given function */
|
|
|
set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */
|
|
|
if ((*name = luaS_travsymbol(checkfunc)) != NULL)
|
|
|
return "global";
|
|
@@ -662,7 +623,7 @@ int lua_ref (int lock) {
|
|
|
|
|
|
|
|
|
lua_Object lua_getref (int ref) {
|
|
|
- TObject *o = luaC_getref(ref);
|
|
|
+ const TObject *o = luaC_getref(ref);
|
|
|
return (o ? put_luaObject(o) : LUA_NOOBJECT);
|
|
|
}
|
|
|
|