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

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");
}