many details + code redistribution

This commit is contained in:
Roberto Ierusalimschy 1999-12-14 16:33:29 -02:00
parent e6d56cd2d8
commit 1b15206cf9
13 changed files with 170 additions and 529 deletions

243
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.62 1999/12/02 16:24:45 roberto Exp roberto $
** $Id: lapi.c,v 1.63 1999/12/06 12:03:45 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -22,7 +22,6 @@
#include "ltable.h"
#include "ltm.h"
#include "lua.h"
#include "luadebug.h"
#include "lvm.h"
@ -31,7 +30,7 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
static lua_Type normalized_type (const TObject *o) {
lua_Type luaA_normalizedtype (const TObject *o) {
int t = ttype(o);
switch (t) {
case LUA_T_PMARK:
@ -46,24 +45,24 @@ static lua_Type normalized_type (const TObject *o) {
}
static void set_normalized (TObject *d, const TObject *s) {
void luaA_setnormalized (TObject *d, const TObject *s) {
d->value = s->value;
d->ttype = normalized_type(s);
d->ttype = luaA_normalizedtype(s);
}
static const TObject *luaA_protovalue (const TObject *o) {
return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
const TObject *luaA_protovalue (const TObject *o) {
return (luaA_normalizedtype(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
}
static void checkCparams (lua_State *L, int nParams) {
void luaA_checkCparams (lua_State *L, int nParams) {
if (nParams > L->top-L->Cstack.base)
lua_error(L, "API error - wrong number of arguments in C2lua stack");
}
static lua_Object put_luaObject (lua_State *L, const TObject *o) {
lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
luaD_openstack(L, L->Cstack.base);
*L->Cstack.base++ = *o;
return L->Cstack.base-1;
@ -86,7 +85,7 @@ static void top2LC (lua_State *L, int n) {
lua_Object lua_pop (lua_State *L) {
checkCparams(L, 1);
luaA_checkCparams(L, 1);
return luaA_putObjectOnTop(L);
}
@ -106,19 +105,19 @@ int lua_callfunction (lua_State *L, lua_Object function) {
return 1;
else {
luaD_openstack(L, L->Cstack.base);
set_normalized(L->Cstack.base, function);
luaA_setnormalized(L->Cstack.base, function);
return luaD_protectedrun(L);
}
}
lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
return put_luaObject(L, luaT_gettagmethod(L, tag, event));
return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
}
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
checkCparams(L, 1);
luaA_checkCparams(L, 1);
luaT_settagmethod(L, tag, event, L->top-1);
return luaA_putObjectOnTop(L);
}
@ -126,7 +125,7 @@ lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
lua_Object lua_seterrormethod (lua_State *L) {
lua_Object temp;
checkCparams(L, 1);
luaA_checkCparams(L, 1);
temp = lua_getglobal(L, "_ERRORMESSAGE");
lua_setglobal(L, "_ERRORMESSAGE");
return temp;
@ -134,14 +133,14 @@ lua_Object lua_seterrormethod (lua_State *L) {
lua_Object lua_gettable (lua_State *L) {
checkCparams(L, 2);
luaA_checkCparams(L, 2);
luaV_gettable(L);
return luaA_putObjectOnTop(L);
}
lua_Object lua_rawgettable (lua_State *L) {
checkCparams(L, 2);
luaA_checkCparams(L, 2);
if (ttype(L->top-2) != LUA_T_ARRAY)
lua_error(L, "indexed expression not a table in rawgettable");
*(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1);
@ -151,14 +150,14 @@ lua_Object lua_rawgettable (lua_State *L) {
void lua_settable (lua_State *L) {
checkCparams(L, 3);
luaA_checkCparams(L, 3);
luaV_settable(L, L->top-3);
L->top -= 2; /* pop table and index */
}
void lua_rawsettable (lua_State *L) {
checkCparams(L, 3);
luaA_checkCparams(L, 3);
luaV_rawsettable(L, L->top-3);
}
@ -168,12 +167,12 @@ lua_Object lua_createtable (lua_State *L) {
luaC_checkGC(L);
avalue(&o) = luaH_new(L, 0);
ttype(&o) = LUA_T_ARRAY;
return put_luaObject(L, &o);
return luaA_putluaObject(L, &o);
}
lua_Object lua_getglobal (lua_State *L, const char *name) {
luaD_checkstack(L, 2); /* may need that to call T.M. */
luaD_checkstack(L, 2); /* may need that to call a tag method */
luaV_getglobal(L, luaS_assertglobalbyname(L, name));
return luaA_putObjectOnTop(L);
}
@ -181,27 +180,27 @@ lua_Object lua_getglobal (lua_State *L, const char *name) {
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
GlobalVar *gv = luaS_assertglobalbyname(L, name);
return put_luaObject(L, &gv->value);
return luaA_putluaObject(L, &gv->value);
}
void lua_setglobal (lua_State *L, const char *name) {
checkCparams(L, 1);
luaD_checkstack(L, 2); /* may need that to call T.M. */
luaA_checkCparams(L, 1);
luaD_checkstack(L, 2); /* may need that to call a tag method */
luaV_setglobal(L, luaS_assertglobalbyname(L, name));
}
void lua_rawsetglobal (lua_State *L, const char *name) {
GlobalVar *gv = luaS_assertglobalbyname(L, name);
checkCparams(L, 1);
luaA_checkCparams(L, 1);
gv->value = *(--L->top);
}
const char *lua_type (lua_State *L, lua_Object o) {
UNUSED(L);
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, o);
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
}
int lua_isnil (lua_State *L, lua_Object o) {
@ -220,7 +219,6 @@ int lua_isuserdata (lua_State *L, lua_Object o) {
}
int lua_iscfunction (lua_State *L, lua_Object o) {
UNUSED(L);
return (lua_tag(L, o) == LUA_T_CPROTO);
}
@ -261,7 +259,6 @@ const char *lua_getstring (lua_State *L, lua_Object obj) {
}
long lua_strlen (lua_State *L, lua_Object obj) {
UNUSED(L);
if (obj == LUA_NOOBJECT || tostring(L, obj))
return 0L;
else return (tsvalue(obj)->u.s.len);
@ -309,7 +306,7 @@ void lua_pushstring (lua_State *L, const char *s) {
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
if (fn == NULL)
lua_error(L, "API error - attempt to push a NULL Cfunction");
checkCparams(L, n);
luaA_checkCparams(L, n);
ttype(L->top) = LUA_T_CPROTO;
fvalue(L->top) = fn;
incr_top;
@ -334,7 +331,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
void lua_pushobject (lua_State *L, lua_Object o) {
if (o == LUA_NOOBJECT)
lua_error(L, "API error - attempt to push a NOOBJECT");
set_normalized(L->top, o);
luaA_setnormalized(L->top, o);
incr_top;
}
@ -368,7 +365,7 @@ int lua_tag (lua_State *L, lua_Object o) {
void lua_settag (lua_State *L, int tag) {
checkCparams(L, 1);
luaA_checkCparams(L, 1);
luaT_realtag(L, tag);
switch (ttype(L->top-1)) {
case LUA_T_ARRAY:
@ -379,7 +376,7 @@ void lua_settag (lua_State *L, int tag) {
break;
default:
luaL_verror(L, "cannot change the tag of a %.20s",
luaO_typename(L, L->top-1));
luaO_typename(L->top-1));
}
L->top--;
}
@ -442,187 +439,3 @@ int lua_next (lua_State *L, lua_Object t, int i) {
}
/*
** {======================================================
** To manipulate some state information
** =======================================================
*/
lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
lua_LHFunction old = L->linehook;
L->linehook = func;
return old;
}
lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
lua_CHFunction old = L->callhook;
L->callhook = func;
return old;
}
int lua_setdebug (lua_State *L, int debug) {
int old = L->debug;
L->debug = debug;
return old;
}
/* }====================================================== */
/*
** {======================================================
** Debug interface
** =======================================================
*/
lua_Function lua_stackedfunction (lua_State *L, int level) {
int i;
for (i = (L->top-1)-L->stack; i>=0; i--) {
int t = L->stack[i].ttype;
if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
if (level-- == 0)
return L->stack+i;
}
return LUA_NOOBJECT;
}
int lua_nups (lua_State *L, lua_Function f) {
UNUSED(L);
return (!f || normalized_type(f) != LUA_T_CLOSURE) ? 0 : f->value.cl->nelems;
}
int lua_currentline (lua_State *L, lua_Function f) {
return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
}
lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
const char **name) {
/* check whether `f' is a Lua function */
if (lua_tag(L, f) != LUA_T_PROTO)
return LUA_NOOBJECT;
else {
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
*name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
if (*name) {
/* if "*name", there must be a LUA_T_LINE */
/* therefore, f+2 points to function base */
return put_luaObject(L, (f+2)+(local_number-1));
}
else
return LUA_NOOBJECT;
}
}
int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
/* check whether `f' is a Lua function */
if (lua_tag(L, f) != LUA_T_PROTO)
return 0;
else {
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
const char *name = luaF_getlocalname(fp, local_number,
lua_currentline(L, f));
checkCparams(L, 1);
--L->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->top;
return 1;
}
else
return 0;
}
}
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");
else {
const TObject *f = luaA_protovalue(func);
if (normalized_type(f) == LUA_T_PROTO) {
*source = tfvalue(f)->source->str;
*linedefined = tfvalue(f)->lineDefined;
}
else {
*source = "(C)";
*linedefined = -1;
}
}
}
static int checkfunc (lua_State *L, TObject *o) {
return luaO_equalObj(o, L->top);
}
const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
/* try to find a name for given function */
GlobalVar *g;
set_normalized(L->top, o); /* to be used by `checkfunc' */
for (g=L->rootglobal; g; g=g->next) {
if (checkfunc(L, &g->value)) {
*name = g->name->str;
return "global";
}
}
/* not found: try tag methods */
if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
return "tag-method";
else return ""; /* not found at all */
}
/* }====================================================== */
/*
** {======================================================
** BLOCK mechanism
** =======================================================
*/
void lua_beginblock (lua_State *L) {
luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
"too many nested blocks", L->stacksize);
L->Cblocks[L->numCblocks] = L->Cstack;
L->numCblocks++;
}
void lua_endblock (lua_State *L) {
if (L->numCblocks <= 0)
lua_error(L, "API error - no block to end");
--L->numCblocks;
L->Cstack = L->Cblocks[L->numCblocks];
L->top = L->Cstack.base;
}
int lua_ref (lua_State *L, int lock) {
int ref;
checkCparams(L, 1);
ref = luaR_ref(L, L->top-1, lock);
L->top--;
return ref;
}
lua_Object lua_getref (lua_State *L, int ref) {
const TObject *o = luaR_getref(L, ref);
return (o ? put_luaObject(L, o) : LUA_NOOBJECT);
}
/* }====================================================== */

7
lapi.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.h,v 1.9 1999/11/22 13:12:07 roberto Exp roberto $
** $Id: lapi.h,v 1.10 1999/12/02 16:24:45 roberto Exp roberto $
** Auxiliary functions from Lua API
** See Copyright Notice in lua.h
*/
@ -11,9 +11,14 @@
#include "lobject.h"
lua_Type luaA_normalizedtype (const TObject *o);
void luaA_setnormalized (TObject *d, const TObject *s);
void luaA_checkCparams (lua_State *L, int nParams);
const TObject *luaA_protovalue (const TObject *o);
void luaA_pushobject (lua_State *L, const TObject *o);
GlobalVar *luaA_nextvar (lua_State *L, TaggedString *g);
int luaA_next (lua_State *L, const Hash *t, int i);
lua_Object luaA_putluaObject (lua_State *L, const TObject *o);
lua_Object luaA_putObjectOnTop (lua_State *L);
#endif

View File

@ -1,5 +1,5 @@
/*
** $Id: lbuiltin.c,v 1.82 1999/12/06 11:42:18 roberto Exp roberto $
** $Id: lbuiltin.c,v 1.83 1999/12/07 12:05:34 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/
@ -29,6 +29,17 @@
#ifdef DEBUG
/*
** function defined in ltests.c, to open some internal-test functions
*/
void luaB_opentests (lua_State *L);
#else
#define luaB_opentests(L) /* do nothing */
#endif
/*
** {======================================================
** Auxiliary functions
@ -87,7 +98,7 @@ static Hash *gettable (lua_State *L, int arg) {
** If your system does not support "stderr", redefine this function, or
** redefine _ERRORMESSAGE so that it won't need _ALERT.
*/
static void luaB_alert (lua_State *L) {
void luaB_alert (lua_State *L) {
fputs(luaL_check_string(L, 1), stderr);
}
@ -96,7 +107,7 @@ static void luaB_alert (lua_State *L) {
** Standard implementation of _ERRORMESSAGE.
** The library "iolib" redefines _ERRORMESSAGE for better error information.
*/
static void error_message (lua_State *L) {
void luaB_ERRORMESSAGE (lua_State *L) {
lua_Object al = lua_rawgetglobal(L, "_ALERT");
if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */
char buff[600];
@ -117,7 +128,7 @@ static void error_message (lua_State *L) {
#define MAXPRINT 40 /* arbitrary limit */
#endif
static void luaB_print (lua_State *L) {
void luaB_print (lua_State *L) {
lua_Object args[MAXPRINT];
lua_Object obj;
int n = 0;
@ -140,7 +151,7 @@ static void luaB_print (lua_State *L) {
}
static void luaB_tonumber (lua_State *L) {
void luaB_tonumber (lua_State *L) {
int base = luaL_opt_int(L, 2, 10);
if (base == 10) { /* standard conversion */
lua_Object o = lua_getparam(L, 1);
@ -161,66 +172,66 @@ static void luaB_tonumber (lua_State *L) {
}
static void luaB_error (lua_State *L) {
void luaB_error (lua_State *L) {
lua_error(L, lua_getstring(L, lua_getparam(L, 1)));
}
static void luaB_setglobal (lua_State *L) {
void luaB_setglobal (lua_State *L) {
const char *n = luaL_check_string(L, 1);
lua_Object value = luaL_nonnullarg(L, 2);
lua_pushobject(L, value);
lua_setglobal(L, n);
}
static void luaB_rawsetglobal (lua_State *L) {
void luaB_rawsetglobal (lua_State *L) {
const char *n = luaL_check_string(L, 1);
lua_Object value = luaL_nonnullarg(L, 2);
lua_pushobject(L, value);
lua_rawsetglobal(L, n);
}
static void luaB_getglobal (lua_State *L) {
void luaB_getglobal (lua_State *L) {
lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1)));
}
static void luaB_rawgetglobal (lua_State *L) {
void luaB_rawgetglobal (lua_State *L) {
lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1)));
}
static void luaB_tag (lua_State *L) {
void luaB_tag (lua_State *L) {
lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1)));
}
static void luaB_settag (lua_State *L) {
void luaB_settag (lua_State *L) {
lua_Object o = luaL_tablearg(L, 1);
lua_pushobject(L, o);
lua_settag(L, luaL_check_int(L, 2));
lua_pushobject(L, o); /* return first argument */
}
static void luaB_newtag (lua_State *L) {
void luaB_newtag (lua_State *L) {
lua_pushnumber(L, lua_newtag(L));
}
static void luaB_copytagmethods (lua_State *L) {
void luaB_copytagmethods (lua_State *L) {
lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
luaL_check_int(L, 2)));
}
static void luaB_rawgettable (lua_State *L) {
void luaB_rawgettable (lua_State *L) {
lua_pushobject(L, luaL_nonnullarg(L, 1));
lua_pushobject(L, luaL_nonnullarg(L, 2));
lua_pushobject(L, lua_rawgettable(L));
}
static void luaB_rawsettable (lua_State *L) {
void luaB_rawsettable (lua_State *L) {
lua_pushobject(L, luaL_nonnullarg(L, 1));
lua_pushobject(L, luaL_nonnullarg(L, 2));
lua_pushobject(L, luaL_nonnullarg(L, 3));
lua_rawsettable(L);
}
static void luaB_settagmethod (lua_State *L) {
void luaB_settagmethod (lua_State *L) {
int tag = luaL_check_int(L, 1);
const char *event = luaL_check_string(L, 2);
lua_Object nf = luaL_nonnullarg(L, 3);
@ -232,23 +243,23 @@ static void luaB_settagmethod (lua_State *L) {
lua_pushobject(L, lua_settagmethod(L, tag, event));
}
static void luaB_gettagmethod (lua_State *L) {
void luaB_gettagmethod (lua_State *L) {
lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1),
luaL_check_string(L, 2)));
}
static void luaB_seterrormethod (lua_State *L) {
void luaB_seterrormethod (lua_State *L) {
lua_Object nf = luaL_functionarg(L, 1);
lua_pushobject(L, nf);
lua_pushobject(L, lua_seterrormethod(L));
}
static void luaB_collectgarbage (lua_State *L) {
void luaB_collectgarbage (lua_State *L) {
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
}
static void luaB_type (lua_State *L) {
void luaB_type (lua_State *L) {
lua_Object o = luaL_nonnullarg(L, 1);
lua_pushstring(L, lua_type(L, o));
}
@ -270,7 +281,7 @@ static void passresults (lua_State *L) {
lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
}
static void luaB_dostring (lua_State *L) {
void luaB_dostring (lua_State *L) {
long l;
const char *s = luaL_check_lstr(L, 1, &l);
if (*s == ID_CHUNK)
@ -281,7 +292,7 @@ static void luaB_dostring (lua_State *L) {
}
static void luaB_dofile (lua_State *L) {
void luaB_dofile (lua_State *L) {
const char *fname = luaL_opt_string(L, 1, NULL);
if (lua_dofile(L, fname) == 0)
passresults(L);
@ -289,7 +300,7 @@ static void luaB_dofile (lua_State *L) {
}
static void luaB_call (lua_State *L) {
void luaB_call (lua_State *L) {
lua_Object f = luaL_nonnullarg(L, 1);
const Hash *arg = gettable(L, 2);
const char *options = luaL_opt_string(L, 3, "");
@ -328,7 +339,7 @@ static void luaB_call (lua_State *L) {
}
static void luaB_nextvar (lua_State *L) {
void luaB_nextvar (lua_State *L) {
lua_Object o = luaL_nonnullarg(L, 1);
TaggedString *g;
if (ttype(o) == LUA_T_NIL)
@ -342,7 +353,7 @@ static void luaB_nextvar (lua_State *L) {
}
static void luaB_next (lua_State *L) {
void luaB_next (lua_State *L) {
const Hash *a = gettable(L, 1);
lua_Object k = luaL_nonnullarg(L, 2);
int i; /* will get first element after `i' */
@ -357,7 +368,7 @@ static void luaB_next (lua_State *L) {
}
static void luaB_tostring (lua_State *L) {
void luaB_tostring (lua_State *L) {
lua_Object o = lua_getparam(L, 1);
char buff[64];
switch (ttype(o)) {
@ -405,14 +416,14 @@ static void luaB_tostring (lua_State *L) {
** =======================================================
*/
static void luaB_assert (lua_State *L) {
void luaB_assert (lua_State *L) {
lua_Object p = lua_getparam(L, 1);
if (p == LUA_NOOBJECT || lua_isnil(L, p))
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
}
static void luaB_foreachi (lua_State *L) {
void luaB_foreachi (lua_State *L) {
const Hash *t = gettable(L, 1);
int n = (int)getnarg(L, t);
int i;
@ -430,7 +441,7 @@ static void luaB_foreachi (lua_State *L) {
}
static void luaB_foreach (lua_State *L) {
void luaB_foreach (lua_State *L) {
const Hash *a = gettable(L, 1);
lua_Object f = luaL_functionarg(L, 2);
int i;
@ -450,7 +461,7 @@ static void luaB_foreach (lua_State *L) {
}
static void luaB_foreachvar (lua_State *L) {
void luaB_foreachvar (lua_State *L) {
lua_Object f = luaL_functionarg(L, 1);
GlobalVar *gv;
luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
@ -472,12 +483,12 @@ static void luaB_foreachvar (lua_State *L) {
}
static void luaB_getn (lua_State *L) {
void luaB_getn (lua_State *L) {
lua_pushnumber(L, getnarg(L, gettable(L, 1)));
}
static void luaB_tinsert (lua_State *L) {
void luaB_tinsert (lua_State *L) {
Hash *a = gettable(L, 1);
lua_Object v = lua_getparam(L, 3);
int n = (int)getnarg(L, a);
@ -495,7 +506,7 @@ static void luaB_tinsert (lua_State *L) {
}
static void luaB_tremove (lua_State *L) {
void luaB_tremove (lua_State *L) {
Hash *a = gettable(L, 1);
int n = (int)getnarg(L, a);
int pos = luaL_opt_int(L, 2, n);
@ -583,7 +594,7 @@ static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
L->top--; /* remove pivot from stack */
}
static void luaB_sort (lua_State *L) {
void luaB_sort (lua_State *L) {
lua_Object t = lua_getparam(L, 1);
Hash *a = gettable(L, 1);
int n = (int)getnarg(L, a);
@ -601,255 +612,9 @@ static void luaB_sort (lua_State *L) {
/* }====================================================== */
#ifdef DEBUG
/*
** {======================================================
** some DEBUG functions
** (for internal debugging of the Lua implementation)
** =======================================================
*/
static void mem_query (lua_State *L) {
lua_pushnumber(L, totalmem);
lua_pushnumber(L, numblocks);
}
static void hash_query (lua_State *L) {
lua_Object o = luaL_nonnullarg(L, 1);
if (lua_getparam(L, 2) == LUA_NOOBJECT) {
luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected");
lua_pushnumber(L, tsvalue(o)->hash);
}
else {
const Hash *t = avalue(luaL_tablearg(L, 2));
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
}
}
static void table_query (lua_State *L) {
const Hash *t = avalue(luaL_tablearg(L, 1));
int i = luaL_opt_int(L, 2, -1);
if (i == -1) {
lua_pushnumber(L, t->size);
lua_pushnumber(L, t->firstfree - t->node);
}
else if (i < t->size) {
luaA_pushobject(L, &t->node[i].key);
luaA_pushobject(L, &t->node[i].val);
if (t->node[i].next)
lua_pushnumber(L, t->node[i].next - t->node);
}
}
static void query_strings (lua_State *L) {
int h = luaL_check_int(L, 1) - 1;
int s = luaL_opt_int(L, 2, 0) - 1;
if (s==-1) {
if (h < NUM_HASHS) {
lua_pushnumber(L, L->string_root[h].nuse);
lua_pushnumber(L, L->string_root[h].size);
}
}
else {
TaggedString *ts = L->string_root[h].hash[s];
for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) {
if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>");
else lua_pushstring(L, ts->str);
}
}
}
static const char *delimits = " \t\n,;";
static void skip (const char **pc) {
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
}
static int getnum (const char **pc) {
int res = 0;
skip(pc);
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
return res;
}
static int getreg (lua_State *L, const char **pc) {
skip(pc);
if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register");
return getnum(pc);
}
static const char *getname (const char **pc) {
static char buff[30];
int i = 0;
skip(pc);
while (**pc != '\0' && !strchr(delimits, **pc))
buff[i++] = *(*pc)++;
buff[i] = '\0';
return buff;
}
#define EQ(s1) (strcmp(s1, inst) == 0)
static void testC (lua_State *L) {
lua_Object reg[10];
const char *pc = luaL_check_string(L, 1);
for (;;) {
const char *inst = getname(&pc);
if EQ("") return;
else if EQ("pushnum") {
lua_pushnumber(L, getnum(&pc));
}
else if EQ("createtable") {
reg[getreg(L, &pc)] = lua_createtable(L);
}
else if EQ("closure") {
lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc)));
lua_pushcclosure(L, f, getnum(&pc));
}
else if EQ("pop") {
reg[getreg(L, &pc)] = lua_pop(L);
}
else if EQ("getglobal") {
int n = getreg(L, &pc);
reg[n] = lua_getglobal(L, getname(&pc));
}
else if EQ("rawgetglobal") {
int n = getreg(L, &pc);
reg[n] = lua_rawgetglobal(L, getname(&pc));
}
else if EQ("ref") {
lua_pushnumber(L, lua_ref(L, 0));
reg[getreg(L, &pc)] = lua_pop(L);
}
else if EQ("reflock") {
lua_pushnumber(L, lua_ref(L, 1));
reg[getreg(L, &pc)] = lua_pop(L);
}
else if EQ("getref") {
int n = getreg(L, &pc);
reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
}
else if EQ("unref") {
lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
}
else if EQ("getparam") {
int n = getreg(L, &pc);
reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the commmand itself */
}
else if EQ("getresult") {
int n = getreg(L, &pc);
reg[n] = lua_getparam(L, getnum(&pc));
}
else if EQ("setglobal") {
lua_setglobal(L, getname(&pc));
}
else if EQ("rawsetglobal") {
lua_rawsetglobal(L, getname(&pc));
}
else if EQ("pushstring") {
lua_pushstring(L, getname(&pc));
}
else if EQ("pushreg") {
lua_pushobject(L, reg[getreg(L, &pc)]);
}
else if EQ("call") {
lua_call(L, getname(&pc));
}
else if EQ("gettable") {
reg[getreg(L, &pc)] = lua_gettable(L);
}
else if EQ("rawgettable") {
reg[getreg(L, &pc)] = lua_rawgettable(L);
}
else if EQ("settable") {
lua_settable(L);
}
else if EQ("rawsettable") {
lua_rawsettable(L);
}
else if EQ("nextvar") {
lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getreg(L, &pc)])));
}
else if EQ("next") {
int n = getreg(L, &pc);
n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
lua_pushnumber(L, n);
}
else if EQ("equal") {
int n1 = getreg(L, &pc);
int n2 = getreg(L, &pc);
lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
}
else if EQ("pushusertag") {
int val = getreg(L, &pc);
int tag = getreg(L, &pc);
lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
(int)lua_getnumber(L, reg[tag]));
}
else if EQ("udataval") {
int n = getreg(L, &pc);
lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
reg[n] = lua_pop(L);
}
else if EQ("settagmethod") {
int n = getreg(L, &pc);
lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
}
else if EQ("beginblock") {
lua_beginblock(L);
}
else if EQ("endblock") {
lua_endblock(L);
}
else if EQ("newstate") {
int stacksize = getnum(&pc);
lua_State *L1 = lua_newstate("stack", stacksize,
"builtin", getnum(&pc), NULL);
lua_pushuserdata(L, L1);
}
else if EQ("closestate") {
lua_close(lua_getuserdata(L, reg[getreg(L, &pc)]));
}
else if EQ("doremote") {
lua_Object ol1 = reg[getreg(L, &pc)];
lua_Object str = reg[getreg(L, &pc)];
lua_State *L1;
lua_Object temp;
int i;
if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
lua_error(L, "bad arguments for `doremote'");
L1 = lua_getuserdata(L, ol1);
lua_dostring(L1, lua_getstring(L, str));
i = 1;
while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
lua_pushstring(L, lua_getstring(L1, temp));
}
else luaL_verror(L, "unknown command in `testC': %.20s", inst);
}
}
/* }====================================================== */
#endif
static const struct luaL_reg builtin_funcs[] = {
#ifdef DEBUG
{"hash", hash_query},
{"querystr", query_strings},
{"querytab", table_query},
{"testC", testC},
{"totalmem", mem_query},
#endif
{"_ALERT", luaB_alert},
{"_ERRORMESSAGE", error_message},
{"_ERRORMESSAGE", luaB_ERRORMESSAGE},
{"call", luaB_call},
{"collectgarbage", luaB_collectgarbage},
{"copytagmethods", luaB_copytagmethods},
@ -891,6 +656,7 @@ void luaB_predefine (lua_State *L) {
luaS_newfixedstring(L, tableEM);
luaS_newfixedstring(L, memEM);
luaL_openl(L, builtin_funcs);
luaB_opentests(L); /* internal test functions (when DEBUG is on) */
lua_pushstring(L, LUA_VERSION);
lua_setglobal(L, "_VERSION");
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lbuiltin.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
** $Id: lbuiltin.h,v 1.2 1999/11/22 13:12:07 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/
@ -9,6 +9,42 @@
#include "lua.h"
void luaB_alert (lua_State *L);
void luaB_ERRORMESSAGE (lua_State *L);
void luaB_print (lua_State *L);
void luaB_tonumber (lua_State *L);
void luaB_error (lua_State *L);
void luaB_setglobal (lua_State *L);
void luaB_rawsetglobal (lua_State *L);
void luaB_getglobal (lua_State *L);
void luaB_rawgetglobal (lua_State *L);
void luaB_tag (lua_State *L);
void luaB_settag (lua_State *L);
void luaB_newtag (lua_State *L);
void luaB_copytagmethods (lua_State *L);
void luaB_rawgettable (lua_State *L);
void luaB_rawsettable (lua_State *L);
void luaB_settagmethod (lua_State *L);
void luaB_gettagmethod (lua_State *L);
void luaB_seterrormethod (lua_State *L);
void luaB_collectgarbage (lua_State *L);
void luaB_type (lua_State *L);
void luaB_dostring (lua_State *L);
void luaB_dofile (lua_State *L);
void luaB_call (lua_State *L);
void luaB_nextvar (lua_State *L);
void luaB_next (lua_State *L);
void luaB_tostring (lua_State *L);
void luaB_assert (lua_State *L);
void luaB_foreachi (lua_State *L);
void luaB_foreach (lua_State *L);
void luaB_foreachvar (lua_State *L);
void luaB_getn (lua_State *L);
void luaB_tinsert (lua_State *L);
void luaB_tremove (lua_State *L);
void luaB_sort (lua_State *L);
void luaB_predefine (lua_State *L);

4
lgc.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lgc.c,v 1.34 1999/11/26 18:59:20 roberto Exp roberto $
** $Id: lgc.c,v 1.35 1999/12/01 19:50:08 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@ -23,7 +23,7 @@
static int markobject (lua_State *L, TObject *o);
/* mark a string; marks bigger than 1 cannot be changed */
/* mark a string; marks larger than 1 cannot be changed */
#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}

8
llex.c
View File

@ -1,5 +1,5 @@
/*
** $Id: llex.c,v 1.44 1999/11/22 13:12:07 roberto Exp roberto $
** $Id: llex.c,v 1.45 1999/12/02 16:41:29 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@ -64,7 +64,7 @@ void luaX_error (LexState *ls, const char *s) {
void luaX_token2str (int token, char *s) {
if (token < 255) {
if (token < 256) {
s[0] = (char)token;
s[1] = '\0';
}
@ -225,7 +225,7 @@ static void inclinenumber (lua_State *L, LexState *LS) {
/*
** =======================================================
** LEXICAL ANALIZER
** LEXICAL ANALYZER
** =======================================================
*/
@ -407,7 +407,7 @@ int luaX_lex (LexState *LS) {
}
save(L, '\0');
if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r))
luaX_error(LS, "invalid numeric format");
luaX_error(LS, "malformed number");
return NUMBER;
case EOZ:

View File

@ -1,5 +1,5 @@
/*
** $Id: lmathlib.c,v 1.20 1999/11/22 13:12:07 roberto Exp roberto $
** $Id: lmathlib.c,v 1.21 1999/11/22 17:39:51 roberto Exp roberto $
** Lua standard mathematical library
** See Copyright Notice in lua.h
*/
@ -144,7 +144,7 @@ static void math_max (lua_State *L) {
static void math_random (lua_State *L) {
/* the '%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */
lua_pushnumber(L, r); /* real between 0 & 1 */

6
lmem.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lmem.c,v 1.20 1999/11/22 13:12:07 roberto Exp roberto $
** $Id: lmem.c,v 1.21 1999/11/29 16:38:48 roberto Exp roberto $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
@ -31,10 +31,10 @@ void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
int inc, int size, const char *errormsg, unsigned long limit) {
unsigned long newn = nelems+inc;
if (newn >= limit) lua_error(L, errormsg);
if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
(nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
return block; /* do not need to reallocate */
else /* it crossed a power of 2 boundary; grow to next power */
else /* it crossed a power-of-2 boundary; grow to next power */
return luaM_realloc(L, block, luaO_power2(newn)*size);
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 1.25 1999/11/22 13:12:07 roberto Exp roberto $
** $Id: lobject.c,v 1.26 1999/11/26 18:59:20 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -23,7 +23,7 @@ const TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
/*
** returns smaller power of 2 bigger than `n' (minimum is MINPOWER2)
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
*/
unsigned long luaO_power2 (unsigned long n) {
unsigned long p = MINPOWER2;

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 1.38 1999/11/26 18:59:20 roberto Exp roberto $
** $Id: lobject.h,v 1.39 1999/12/02 16:24:45 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -197,7 +197,7 @@ typedef struct Hash {
extern const char *const luaO_typenames[];
#define luaO_typename(L, o) luaO_typenames[-ttype(o)]
#define luaO_typename(o) luaO_typenames[-ttype(o)]
#define MINPOWER2 4 /* minimum size for "growing" vectors */

40
lref.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lref.c,v 1.2 1999/11/10 15:37:50 roberto Exp roberto $
** $Id: lref.c,v 1.3 1999/11/22 13:12:07 roberto Exp roberto $
** REF mechanism
** See Copyright Notice in lua.h
*/
@ -7,15 +7,17 @@
#define LUA_REENTRANT
#include "lapi.h"
#include "lmem.h"
#include "lref.h"
#include "lstate.h"
#include "lua.h"
int luaR_ref (lua_State *L, const TObject *o, int lock) {
int lua_ref (lua_State *L, int lock) {
int ref;
if (ttype(o) == LUA_T_NIL)
luaA_checkCparams(L, 1);
if (ttype(L->top-1) == LUA_T_NIL)
ref = LUA_REFNIL;
else {
if (L->refFree != NONEXT) { /* is there a free place? */
@ -26,9 +28,10 @@ int luaR_ref (lua_State *L, const TObject *o, int lock) {
luaM_growvector(L, L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT);
ref = L->refSize++;
}
L->refArray[ref].o = *o;
L->refArray[ref].o = *(L->top-1);
L->refArray[ref].st = lock ? LOCK : HOLD;
}
L->top--;
return ref;
}
@ -43,17 +46,38 @@ void lua_unref (lua_State *L, int ref) {
}
const TObject *luaR_getref (lua_State *L, int ref) {
lua_Object lua_getref (lua_State *L, int ref) {
if (ref == LUA_REFNIL)
return &luaO_nilobject;
return luaA_putluaObject(L, &luaO_nilobject);
else if (0 <= ref && ref < L->refSize &&
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
return &L->refArray[ref].o;
return luaA_putluaObject(L, &L->refArray[ref].o);
else
return NULL;
return LUA_NOOBJECT;
}
void lua_beginblock (lua_State *L) {
luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
"too many nested blocks", L->stacksize);
L->Cblocks[L->numCblocks] = L->Cstack;
L->numCblocks++;
}
void lua_endblock (lua_State *L) {
if (L->numCblocks <= 0)
lua_error(L, "API error - no block to end");
--L->numCblocks;
L->Cstack = L->Cblocks[L->numCblocks];
L->top = L->Cstack.base;
}
static int ismarked (const TObject *o) {
/* valid only for locked objects */
switch (o->ttype) {

5
lref.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lref.h,v 1.2 1999/11/10 15:37:50 roberto Exp roberto $
** $Id: lref.h,v 1.3 1999/11/22 13:12:07 roberto Exp roberto $
** REF mechanism
** See Copyright Notice in lua.h
*/
@ -22,9 +22,6 @@ struct ref {
};
int luaR_ref (lua_State *L, const TObject *o, int lock);
const TObject *luaR_getref (lua_State *L, int ref);
void luaR_invalidaterefs (lua_State *L);
#endif

4
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 1.71 1999/12/06 19:30:53 roberto Exp roberto $
** $Id: lvm.c,v 1.72 1999/12/09 20:01:48 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -64,7 +64,7 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
if (ttype(obj) != LUA_T_NUMBER)
return 1;
else {
char s[32]; /* 16 digits, signal, point and \0 (+ some extra...) */
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
sprintf(s, "%.16g", (double)nvalue(obj));
tsvalue(obj) = luaS_new(L, s);
ttype(obj) = LUA_T_STRING;