mirror of
https://github.com/lua/lua.git
synced 2026-07-27 00:19:07 +00:00
Compare commits
63 Commits
v3.0-alpha
...
v3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef3c45d523 | ||
|
|
20a48a818f | ||
|
|
c9e6ed1d9f | ||
|
|
56fdab67d8 | ||
|
|
3a98c8670d | ||
|
|
da585783e3 | ||
|
|
e81f184164 | ||
|
|
3e43275308 | ||
|
|
f97307b548 | ||
|
|
6402bfb1f8 | ||
|
|
27b6bbdcf3 | ||
|
|
5a198de3c1 | ||
|
|
c5d7ba7347 | ||
|
|
f84c2ebc4a | ||
|
|
c8897f2b08 | ||
|
|
6bb2cac3db | ||
|
|
88f020b626 | ||
|
|
a38f093f05 | ||
|
|
b6adfdd01b | ||
|
|
b90b4bbd3a | ||
|
|
f6d95140ae | ||
|
|
eca17b24ea | ||
|
|
6b61582e47 | ||
|
|
60bdc370d9 | ||
|
|
b9e31f73ee | ||
|
|
ea98620d98 | ||
|
|
6b78040840 | ||
|
|
e931c7c0f6 | ||
|
|
70160320b1 | ||
|
|
bfbf56f15a | ||
|
|
732ef82c72 | ||
|
|
2a1da64872 | ||
|
|
3c5d71cf77 | ||
|
|
1848bcc15b | ||
|
|
4d2de484f6 | ||
|
|
312f78d925 | ||
|
|
e4b69d6c9c | ||
|
|
c9a2dfeb2c | ||
|
|
9fe5be3acf | ||
|
|
611680af08 | ||
|
|
cb1d8f0fa0 | ||
|
|
33f4fef410 | ||
|
|
e77534c08f | ||
|
|
a22b54bbb6 | ||
|
|
414ebe8ce1 | ||
|
|
33ace03b5a | ||
|
|
085181a08a | ||
|
|
dd22ea4da5 | ||
|
|
5fdcfeb353 | ||
|
|
d68d8287aa | ||
|
|
e1249970c2 | ||
|
|
9d6f4e48a6 | ||
|
|
b546a042e8 | ||
|
|
bd9e68cfcd | ||
|
|
9747f3c87a | ||
|
|
12d9731a49 | ||
|
|
4aade854bb | ||
|
|
369dd65318 | ||
|
|
209602ac31 | ||
|
|
6251d889ca | ||
|
|
faec0b0824 | ||
|
|
64d93e7cf9 | ||
|
|
d73c5bcfe7 |
10
auxlib.c
10
auxlib.c
@@ -1,4 +1,4 @@
|
||||
char *rcs_auxlib="$Id: auxlib.c,v 1.3 1997/04/06 14:08:08 roberto Exp roberto $";
|
||||
char *rcs_auxlib="$Id: auxlib.c,v 1.4 1997/04/07 14:48:53 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
@@ -22,11 +22,11 @@ int luaI_findstring (char *name, char *list[])
|
||||
|
||||
void luaL_arg_check(int cond, int numarg, char *extramsg)
|
||||
{
|
||||
char *funcname;
|
||||
lua_getobjname(lua_stackedfunction(0), &funcname);
|
||||
if (funcname == NULL)
|
||||
funcname = "???";
|
||||
if (!cond) {
|
||||
char *funcname;
|
||||
lua_getobjname(lua_stackedfunction(0), &funcname);
|
||||
if (funcname == NULL)
|
||||
funcname = "???";
|
||||
if (extramsg == NULL)
|
||||
luaL_verror("bad argument #%d to function `%s'", numarg, funcname);
|
||||
else
|
||||
|
||||
79
fallback.c
79
fallback.c
@@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_fallback="$Id: fallback.c,v 2.3 1997/04/06 14:08:08 roberto Exp roberto $";
|
||||
char *rcs_fallback="$Id: fallback.c,v 2.8 1997/06/17 17:27:07 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -111,24 +111,26 @@ static int luaI_checkevent (char *name, char *list[])
|
||||
}
|
||||
|
||||
|
||||
static struct IM {
|
||||
TObject int_method[IM_N];
|
||||
} *luaI_IMtable = NULL;
|
||||
struct IM *luaI_IMtable = NULL;
|
||||
|
||||
static int IMtable_size = 0;
|
||||
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
|
||||
|
||||
|
||||
/* events in LUA_T_LINE are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
|
||||
{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
|
||||
{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
|
||||
{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
|
||||
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
|
||||
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
|
||||
{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
|
||||
{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
|
||||
};
|
||||
|
||||
static int validevent (lua_Type t, int e)
|
||||
@@ -141,7 +143,7 @@ static void init_entry (int tag)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
luaI_IMtable[-tag].int_method[i].ttype = LUA_T_NIL;
|
||||
ttype(luaI_getim(tag, i)) = LUA_T_NIL;
|
||||
}
|
||||
|
||||
void luaI_initfallbacks (void)
|
||||
@@ -188,38 +190,35 @@ void luaI_settag (int tag, TObject *o)
|
||||
case LUA_T_ARRAY:
|
||||
o->value.a->htag = tag;
|
||||
break;
|
||||
case LUA_T_USERDATA:
|
||||
o->value.ts->tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaI_tag (TObject *o)
|
||||
int luaI_efectivetag (TObject *o)
|
||||
{
|
||||
lua_Type t = ttype(o);
|
||||
if (t == LUA_T_USERDATA)
|
||||
return o->value.ts->tag;
|
||||
if (t == LUA_T_USERDATA) {
|
||||
int tag = o->value.ts->tag;
|
||||
return (tag >= 0) ? LUA_T_USERDATA : tag;
|
||||
}
|
||||
else if (t == LUA_T_ARRAY)
|
||||
return o->value.a->htag;
|
||||
else return t;
|
||||
}
|
||||
|
||||
|
||||
TObject *luaI_getim (int tag, IMS event)
|
||||
{
|
||||
if (tag > LUA_T_USERDATA)
|
||||
tag = LUA_T_USERDATA; /* default for non-registered tags */
|
||||
return &luaI_IMtable[-tag].int_method[event];
|
||||
}
|
||||
|
||||
|
||||
void luaI_gettagmethod (void)
|
||||
{
|
||||
int t = (int)luaL_check_number(1);
|
||||
int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
|
||||
checktag(t);
|
||||
if (validevent(t, e))
|
||||
luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
|
||||
luaI_pushobject(luaI_getim(t,e));
|
||||
}
|
||||
|
||||
|
||||
@@ -234,12 +233,19 @@ void luaI_settagmethod (void)
|
||||
luaI_eventname[e], t);
|
||||
luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
|
||||
3, "function expected");
|
||||
luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
|
||||
luaI_IMtable[-t].int_method[e] = *luaI_Address(func);
|
||||
luaI_pushobject(luaI_getim(t,e));
|
||||
*luaI_getim(t, e) = *luaI_Address(func);
|
||||
}
|
||||
|
||||
|
||||
static TObject errorim = {LUA_T_NIL, {NULL}};
|
||||
static void stderrorim (void)
|
||||
{
|
||||
lua_Object s = lua_getparam(1);
|
||||
if (lua_isstring(s))
|
||||
fprintf(stderr, "lua: %s\n", lua_getstring(s));
|
||||
}
|
||||
|
||||
static TObject errorim = {LUA_T_CFUNCTION, {stderrorim}};
|
||||
|
||||
|
||||
TObject *luaI_geterrorim (void)
|
||||
@@ -264,7 +270,7 @@ char *luaI_travfallbacks (int (*fn)(TObject *))
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
|
||||
int t;
|
||||
for (t=0; t>=last_tag; t--)
|
||||
if (fn(&luaI_IMtable[-t].int_method[e]))
|
||||
if (fn(luaI_getim(t,e)))
|
||||
return luaI_eventname[e];
|
||||
}
|
||||
return NULL;
|
||||
@@ -275,7 +281,7 @@ char *luaI_travfallbacks (int (*fn)(TObject *))
|
||||
* ===================================================================
|
||||
* compatibility with old fallback system
|
||||
*/
|
||||
|
||||
#if LUA_COMPAT2_5
|
||||
|
||||
static void errorFB (void)
|
||||
{
|
||||
@@ -301,9 +307,10 @@ static void fillvalids (IMS e, TObject *func)
|
||||
int t;
|
||||
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
|
||||
if (validevent(t, e))
|
||||
luaI_IMtable[-t].int_method[e] = *func;
|
||||
*luaI_getim(t, e) = *func;
|
||||
}
|
||||
|
||||
|
||||
void luaI_setfallback (void)
|
||||
{
|
||||
static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
|
||||
@@ -320,13 +327,13 @@ void luaI_setfallback (void)
|
||||
replace = errorFB;
|
||||
break;
|
||||
case 1: /* old getglobal fallback */
|
||||
oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL];
|
||||
luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func);
|
||||
oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
|
||||
*luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
|
||||
replace = nilFB;
|
||||
break;
|
||||
case 2: { /* old arith fallback */
|
||||
int i;
|
||||
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW];
|
||||
oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
|
||||
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
||||
fillvalids(i, luaI_Address(func));
|
||||
replace = typeFB;
|
||||
@@ -334,7 +341,7 @@ void luaI_setfallback (void)
|
||||
}
|
||||
case 3: { /* old order fallback */
|
||||
int i;
|
||||
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT];
|
||||
oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
|
||||
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
||||
fillvalids(i, luaI_Address(func));
|
||||
replace = typeFB;
|
||||
@@ -343,7 +350,7 @@ void luaI_setfallback (void)
|
||||
default: {
|
||||
int e;
|
||||
if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
|
||||
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e];
|
||||
oldfunc = *luaI_getim(LUA_T_LINE, e);
|
||||
fillvalids(e, luaI_Address(func));
|
||||
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
||||
}
|
||||
@@ -358,4 +365,4 @@ void luaI_setfallback (void)
|
||||
else
|
||||
lua_pushcfunction(replace);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
13
fallback.h
13
fallback.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: fallback.h,v 1.21 1997/04/02 23:04:12 roberto Exp roberto $
|
||||
** $Id: fallback.h,v 1.22 1997/04/04 22:24:51 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef fallback_h
|
||||
@@ -35,8 +35,15 @@ typedef enum {
|
||||
|
||||
#define IM_N 18
|
||||
|
||||
|
||||
extern struct IM {
|
||||
TObject int_method[IM_N];
|
||||
} *luaI_IMtable;
|
||||
|
||||
extern char *luaI_eventname[];
|
||||
|
||||
#define luaI_getim(tag,event) (&luaI_IMtable[-(tag)].int_method[event])
|
||||
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_efectivetag(o),(e)))
|
||||
|
||||
void luaI_setfallback (void);
|
||||
int luaI_ref (TObject *object, int lock);
|
||||
@@ -47,10 +54,8 @@ char *luaI_travfallbacks (int (*fn)(TObject *));
|
||||
|
||||
void luaI_settag (int tag, TObject *o);
|
||||
void luaI_realtag (int tag);
|
||||
TObject *luaI_getim (int tag, IMS event);
|
||||
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_tag(o),(e)))
|
||||
TObject *luaI_geterrorim (void);
|
||||
int luaI_tag (TObject *o);
|
||||
int luaI_efectivetag (TObject *o);
|
||||
void luaI_settagmethod (void);
|
||||
void luaI_gettagmethod (void);
|
||||
void luaI_seterrormethod (void);
|
||||
|
||||
34
func.c
34
func.c
@@ -5,6 +5,7 @@
|
||||
#include "luamem.h"
|
||||
#include "func.h"
|
||||
#include "opcode.h"
|
||||
#include "inout.h"
|
||||
|
||||
|
||||
static TFunc *function_root = NULL;
|
||||
@@ -23,7 +24,7 @@ void luaI_initTFunc (TFunc *f)
|
||||
f->size = 0;
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->fileName = NULL;
|
||||
f->fileName = lua_parsedfile;
|
||||
f->locvars = NULL;
|
||||
}
|
||||
|
||||
@@ -49,35 +50,44 @@ void luaI_freefunc (TFunc *f)
|
||||
luaI_free (f);
|
||||
}
|
||||
|
||||
|
||||
void luaI_funcfree (TFunc *l)
|
||||
{
|
||||
while (l) {
|
||||
TFunc *next = l->next;
|
||||
luaI_freefunc(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Garbage collection function.
|
||||
** This function traverse the function list freeing unindexed functions
|
||||
*/
|
||||
Long luaI_funccollector (void)
|
||||
TFunc *luaI_funccollector (long *acum)
|
||||
{
|
||||
TFunc *curr = function_root;
|
||||
TFunc *prev = NULL;
|
||||
Long counter = 0;
|
||||
while (curr)
|
||||
{
|
||||
TFunc *frees = NULL;
|
||||
long counter = 0;
|
||||
while (curr) {
|
||||
TFunc *next = curr->next;
|
||||
if (!curr->marked)
|
||||
{
|
||||
if (!curr->marked) {
|
||||
if (prev == NULL)
|
||||
function_root = next;
|
||||
else
|
||||
prev->next = next;
|
||||
luaI_freefunc (curr);
|
||||
curr->next = frees;
|
||||
frees = curr;
|
||||
++counter;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
curr->marked = 0;
|
||||
prev = curr;
|
||||
}
|
||||
curr = next;
|
||||
}
|
||||
return counter;
|
||||
*acum += counter;
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
|
||||
5
func.h
5
func.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: func.h,v 1.7 1996/03/08 12:04:04 roberto Exp roberto $
|
||||
** $Id: func.h,v 1.8 1996/03/14 15:54:20 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef func_h
|
||||
@@ -30,7 +30,8 @@ typedef struct TFunc
|
||||
LocVar *locvars;
|
||||
} TFunc;
|
||||
|
||||
Long luaI_funccollector (void);
|
||||
TFunc *luaI_funccollector (long *cont);
|
||||
void luaI_funcfree (TFunc *l);
|
||||
void luaI_insertfunction (TFunc *f);
|
||||
|
||||
void luaI_initTFunc (TFunc *f);
|
||||
|
||||
253
hash.c
253
hash.c
@@ -3,7 +3,7 @@
|
||||
** hash manager for lua
|
||||
*/
|
||||
|
||||
char *rcs_hash="$Id: hash.c,v 2.40 1997/04/04 15:35:37 roberto Exp roberto $";
|
||||
char *rcs_hash="$Id: hash.c,v 2.42 1997/05/08 20:43:30 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include "luamem.h"
|
||||
@@ -33,7 +33,7 @@ static Hash *listhead = NULL;
|
||||
|
||||
/* hash dimensions values */
|
||||
static Long dimensions[] =
|
||||
{3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
|
||||
{5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
|
||||
12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
|
||||
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
|
||||
|
||||
@@ -49,27 +49,6 @@ int luaI_redimension (int nhash)
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
static int hashindex (Hash *t, TObject *ref) /* hash function */
|
||||
{
|
||||
long int h;
|
||||
switch (ttype(ref)) {
|
||||
case LUA_T_NUMBER:
|
||||
h = (long int)nvalue(ref); break;
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
h = tsvalue(ref)->hash; break;
|
||||
case LUA_T_FUNCTION:
|
||||
h = (IntPoint)ref->value.tf; break;
|
||||
case LUA_T_CFUNCTION:
|
||||
h = (IntPoint)fvalue(ref); break;
|
||||
case LUA_T_ARRAY:
|
||||
h = (IntPoint)avalue(ref); break;
|
||||
default:
|
||||
lua_error ("unexpected type to index table");
|
||||
h = 0; /* UNREACHEABLE */
|
||||
}
|
||||
if (h < 0) h = -h;
|
||||
return h%nhash(t); /* make it a valid index */
|
||||
}
|
||||
|
||||
int lua_equalObj (TObject *t1, TObject *t2)
|
||||
{
|
||||
@@ -88,16 +67,44 @@ int lua_equalObj (TObject *t1, TObject *t2)
|
||||
}
|
||||
}
|
||||
|
||||
static int present (Hash *t, TObject *ref)
|
||||
{
|
||||
int h = hashindex(t, ref);
|
||||
while (ttype(ref(node(t, h))) != LUA_T_NIL)
|
||||
{
|
||||
if (lua_equalObj(ref, ref(node(t, h))))
|
||||
return h;
|
||||
h = (h+1) % nhash(t);
|
||||
}
|
||||
return h;
|
||||
|
||||
static long int hashindex (TObject *ref)
|
||||
{
|
||||
long int h;
|
||||
switch (ttype(ref)) {
|
||||
case LUA_T_NUMBER:
|
||||
h = (long int)nvalue(ref); break;
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
h = tsvalue(ref)->hash; break;
|
||||
case LUA_T_FUNCTION:
|
||||
h = (IntPoint)ref->value.tf; break;
|
||||
case LUA_T_CFUNCTION:
|
||||
h = (IntPoint)fvalue(ref); break;
|
||||
case LUA_T_ARRAY:
|
||||
h = (IntPoint)avalue(ref); break;
|
||||
default:
|
||||
lua_error ("unexpected type to index table");
|
||||
h = 0; /* UNREACHEABLE */
|
||||
}
|
||||
if (h < 0) h = -h;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
static int present (Hash *t, TObject *key)
|
||||
{
|
||||
long int h = hashindex(key);
|
||||
int tsize = nhash(t);
|
||||
int h1 = h%tsize;
|
||||
TObject *rf = ref(node(t, h1));
|
||||
if (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf)) {
|
||||
int h2 = h%(tsize-2) + 1;
|
||||
do {
|
||||
h1 = (h1+h2)%tsize;
|
||||
rf = ref(node(t, h1));
|
||||
} while (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf));
|
||||
}
|
||||
return h1;
|
||||
}
|
||||
|
||||
|
||||
@@ -160,46 +167,50 @@ void lua_hashmark (Hash *h)
|
||||
}
|
||||
|
||||
|
||||
void luaI_hashcallIM (void)
|
||||
void luaI_hashcallIM (Hash *l)
|
||||
{
|
||||
Hash *curr_array;
|
||||
TObject t;
|
||||
ttype(&t) = LUA_T_ARRAY;
|
||||
for (curr_array = listhead; curr_array; curr_array = curr_array->next)
|
||||
if (markarray(curr_array) != 1)
|
||||
{
|
||||
avalue(&t) = curr_array;
|
||||
luaI_gcIM(&t);
|
||||
}
|
||||
for (; l; l=l->next) {
|
||||
avalue(&t) = l;
|
||||
luaI_gcIM(&t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaI_hashfree (Hash *frees)
|
||||
{
|
||||
while (frees) {
|
||||
Hash *next = frees->next;
|
||||
hashdelete(frees);
|
||||
frees = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection to arrays
|
||||
** Delete all unmarked arrays.
|
||||
*/
|
||||
Long lua_hashcollector (void)
|
||||
Hash *luaI_hashcollector (long *acum)
|
||||
{
|
||||
Hash *curr_array = listhead, *prev = NULL;
|
||||
Long counter = 0;
|
||||
while (curr_array != NULL)
|
||||
{
|
||||
Hash *next = curr_array->next;
|
||||
if (markarray(curr_array) != 1)
|
||||
{
|
||||
if (prev == NULL) listhead = next;
|
||||
else prev->next = next;
|
||||
hashdelete(curr_array);
|
||||
++counter;
|
||||
Hash *curr_array = listhead, *prev = NULL, *frees = NULL;
|
||||
long counter = 0;
|
||||
while (curr_array != NULL) {
|
||||
Hash *next = curr_array->next;
|
||||
if (markarray(curr_array) != 1) {
|
||||
if (prev == NULL)
|
||||
listhead = next;
|
||||
else
|
||||
prev->next = next;
|
||||
curr_array->next = frees;
|
||||
frees = curr_array;
|
||||
++counter;
|
||||
}
|
||||
else {
|
||||
markarray(curr_array) = 0;
|
||||
prev = curr_array;
|
||||
}
|
||||
curr_array = next;
|
||||
}
|
||||
else
|
||||
{
|
||||
markarray(curr_array) = 0;
|
||||
prev = curr_array;
|
||||
}
|
||||
curr_array = next;
|
||||
}
|
||||
return counter;
|
||||
*acum += counter;
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
@@ -221,22 +232,35 @@ Hash *lua_createarray (int nhash)
|
||||
|
||||
|
||||
/*
|
||||
** Re-hash
|
||||
** Rehash:
|
||||
** Check if table has deleted slots. It it has, it does not need to
|
||||
** grow, since rehash will reuse them.
|
||||
*/
|
||||
static int emptyslots (Hash *t)
|
||||
{
|
||||
int i;
|
||||
for (i=nhash(t)-1; i>=0; i--) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void rehash (Hash *t)
|
||||
{
|
||||
int i;
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
nhash(t) = luaI_redimension(nhash(t));
|
||||
nodevector(t) = hashnodecreate(nhash(t));
|
||||
for (i=0; i<nold; i++)
|
||||
{
|
||||
Node *n = vold+i;
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
|
||||
*node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
|
||||
}
|
||||
luaI_free(vold);
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
int i;
|
||||
if (!emptyslots(t))
|
||||
nhash(t) = luaI_redimension(nhash(t));
|
||||
nodevector(t) = hashnodecreate(nhash(t));
|
||||
for (i=0; i<nold; i++) {
|
||||
Node *n = vold+i;
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
|
||||
*node(t, present(t, ref(n))) = *n; /* copy old node to new hash */
|
||||
}
|
||||
luaI_free(vold);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -257,23 +281,17 @@ TObject *lua_hashget (Hash *t, TObject *ref)
|
||||
*/
|
||||
TObject *lua_hashdefine (Hash *t, TObject *ref)
|
||||
{
|
||||
int h;
|
||||
Node *n;
|
||||
h = present(t, ref);
|
||||
n = node(t, h);
|
||||
if (ttype(ref(n)) == LUA_T_NIL)
|
||||
{
|
||||
nuse(t)++;
|
||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
|
||||
{
|
||||
rehash(t);
|
||||
h = present(t, ref);
|
||||
n = node(t, h);
|
||||
Node *n = node(t, present(t, ref));
|
||||
if (ttype(ref(n)) == LUA_T_NIL) {
|
||||
nuse(t)++;
|
||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
|
||||
rehash(t);
|
||||
n = node(t, present(t, ref));
|
||||
}
|
||||
*ref(n) = *ref;
|
||||
ttype(val(n)) = LUA_T_NIL;
|
||||
}
|
||||
*ref(n) = *ref;
|
||||
ttype(val(n)) = LUA_T_NIL;
|
||||
}
|
||||
return (val(n));
|
||||
return (val(n));
|
||||
}
|
||||
|
||||
|
||||
@@ -285,33 +303,30 @@ TObject *lua_hashdefine (Hash *t, TObject *ref)
|
||||
*/
|
||||
static void hashnext (Hash *t, int i)
|
||||
{
|
||||
if (i >= nhash(t))
|
||||
return;
|
||||
while (ttype(ref(node(t,i))) == LUA_T_NIL ||
|
||||
ttype(val(node(t,i))) == LUA_T_NIL)
|
||||
{
|
||||
if (++i >= nhash(t))
|
||||
return;
|
||||
}
|
||||
luaI_pushobject(ref(node(t,i)));
|
||||
luaI_pushobject(val(node(t,i)));
|
||||
Node *n;
|
||||
int tsize = nhash(t);
|
||||
if (i >= tsize)
|
||||
return;
|
||||
n = node(t, i);
|
||||
while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
|
||||
if (++i >= tsize)
|
||||
return;
|
||||
n = node(t, i);
|
||||
}
|
||||
luaI_pushobject(ref(node(t,i)));
|
||||
luaI_pushobject(val(node(t,i)));
|
||||
}
|
||||
|
||||
void lua_next (void)
|
||||
{
|
||||
Hash *t;
|
||||
lua_Object o = lua_getparam(1);
|
||||
lua_Object r = lua_getparam(2);
|
||||
luaL_arg_check(lua_istable(o), 1, "table expected");
|
||||
luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
|
||||
t = avalue(luaI_Address(o));
|
||||
if (lua_isnil(r))
|
||||
{
|
||||
hashnext(t, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
int h = present (t, luaI_Address(r));
|
||||
hashnext(t, h+1);
|
||||
}
|
||||
Hash *t;
|
||||
lua_Object o = lua_getparam(1);
|
||||
lua_Object r = lua_getparam(2);
|
||||
luaL_arg_check(lua_istable(o), 1, "table expected");
|
||||
luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
|
||||
t = avalue(luaI_Address(o));
|
||||
if (lua_isnil(r))
|
||||
hashnext(t, 0);
|
||||
else
|
||||
hashnext(t, present(t, luaI_Address(r))+1);
|
||||
}
|
||||
|
||||
7
hash.h
7
hash.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
** hash.h
|
||||
** hash manager for lua
|
||||
** $Id: hash.h,v 2.14 1997/03/19 19:41:10 roberto Exp roberto $
|
||||
** $Id: hash.h,v 2.15 1997/03/31 14:02:58 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef hash_h
|
||||
@@ -29,8 +29,9 @@ int lua_equalObj (TObject *t1, TObject *t2);
|
||||
int luaI_redimension (int nhash);
|
||||
Hash *lua_createarray (int nhash);
|
||||
void lua_hashmark (Hash *h);
|
||||
Long lua_hashcollector (void);
|
||||
void luaI_hashcallIM (void);
|
||||
Hash *luaI_hashcollector (long *count);
|
||||
void luaI_hashcallIM (Hash *l);
|
||||
void luaI_hashfree (Hash *frees);
|
||||
TObject *lua_hashget (Hash *t, TObject *ref);
|
||||
TObject *lua_hashdefine (Hash *t, TObject *ref);
|
||||
void lua_next (void);
|
||||
|
||||
229
inout.c
229
inout.c
@@ -5,22 +5,24 @@
|
||||
** Also provides some predefined lua functions.
|
||||
*/
|
||||
|
||||
char *rcs_inout="$Id: inout.c,v 2.56 1997/04/06 14:08:08 roberto Exp roberto $";
|
||||
char *rcs_inout="$Id: inout.c,v 2.68 1997/06/26 20:47:43 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "auxlib.h"
|
||||
#include "lex.h"
|
||||
#include "opcode.h"
|
||||
#include "fallback.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "lex.h"
|
||||
#include "lua.h"
|
||||
#include "luamem.h"
|
||||
#include "luamem.h"
|
||||
#include "opcode.h"
|
||||
#include "table.h"
|
||||
#include "tree.h"
|
||||
#include "lua.h"
|
||||
#include "hash.h"
|
||||
#include "luamem.h"
|
||||
#include "fallback.h"
|
||||
#include "luamem.h"
|
||||
#include "undump.h"
|
||||
#include "zio.h"
|
||||
|
||||
|
||||
/* Exported variables */
|
||||
@@ -34,80 +36,87 @@ char *luaI_typenames[] = { /* ORDER LUA_T */
|
||||
NULL
|
||||
};
|
||||
|
||||
static FILE *fp;
|
||||
static char *st;
|
||||
|
||||
/*
|
||||
** Function to get the next character from the input file
|
||||
*/
|
||||
static int fileinput (void)
|
||||
|
||||
void luaI_setparsedfile (char *name)
|
||||
{
|
||||
int c = fgetc(fp);
|
||||
return (c == EOF) ? 0 : c;
|
||||
lua_parsedfile = luaI_createfixedstring(name)->str;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to get the next character from the input string
|
||||
*/
|
||||
static int stringinput (void)
|
||||
{
|
||||
return *st++;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to open a file to be input unit.
|
||||
** Return the file.
|
||||
*/
|
||||
FILE *lua_openfile (char *fn)
|
||||
int lua_doFILE (FILE *f, int bin)
|
||||
{
|
||||
lua_setinput (fileinput);
|
||||
if (fn == NULL)
|
||||
{
|
||||
fp = stdin;
|
||||
fn = "(stdin)";
|
||||
}
|
||||
else
|
||||
fp = fopen (fn, "r");
|
||||
if (fp == NULL)
|
||||
return NULL;
|
||||
lua_parsedfile = luaI_createfixedstring(fn)->str;
|
||||
return fp;
|
||||
}
|
||||
ZIO z;
|
||||
luaZ_Fopen(&z, f);
|
||||
if (bin)
|
||||
return luaI_undump(&z);
|
||||
else {
|
||||
lua_setinput(&z);
|
||||
return lua_domain();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to close an opened file
|
||||
*/
|
||||
void lua_closefile (void)
|
||||
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
if (fp != NULL && fp != stdin)
|
||||
{
|
||||
fclose (fp);
|
||||
fp = NULL;
|
||||
}
|
||||
}
|
||||
int status;
|
||||
int c;
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
luaI_setparsedfile(filename?filename:"(stdin)");
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
if (c == ID_CHUNK) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
status = lua_doFILE(f, 1);
|
||||
}
|
||||
else {
|
||||
if (c == '#')
|
||||
while ((c=fgetc(f)) != '\n') /* skip first line */;
|
||||
status = lua_doFILE(f, 0);
|
||||
}
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function to open a string to be input unit
|
||||
*/
|
||||
#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
|
||||
void lua_openstring (char *s)
|
||||
|
||||
|
||||
int lua_dobuffer (char *buff, int size)
|
||||
{
|
||||
char buff[SIZE_PREF+25];
|
||||
lua_setinput(stringinput);
|
||||
st = s;
|
||||
sprintf(buff, "(dostring) >> %.20s%s", s,
|
||||
(strlen(s) > SIZE_PREF) ? "..." : "");
|
||||
lua_parsedfile = luaI_createfixedstring(buff)->str;
|
||||
int status;
|
||||
ZIO z;
|
||||
luaI_setparsedfile("(buffer)");
|
||||
luaZ_mopen(&z, buff, size);
|
||||
status = luaI_undump(&z);
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to close an opened string
|
||||
*/
|
||||
void lua_closestring (void)
|
||||
|
||||
int lua_dostring (char *str)
|
||||
{
|
||||
int status;
|
||||
char buff[SIZE_PREF+25];
|
||||
char *temp;
|
||||
ZIO z;
|
||||
if (str == NULL) return 1;
|
||||
sprintf(buff, "(dostring) >> %.20s", str);
|
||||
temp = strchr(buff, '\n');
|
||||
if (temp) *temp = 0; /* end string after first line */
|
||||
luaI_setparsedfile(buff);
|
||||
luaZ_sopen(&z, str);
|
||||
lua_setinput(&z);
|
||||
status = lua_domain();
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int passresults (void)
|
||||
{
|
||||
int arg = 0;
|
||||
@@ -116,15 +125,45 @@ static int passresults (void)
|
||||
lua_pushobject(obj);
|
||||
return arg-1;
|
||||
}
|
||||
|
||||
|
||||
static void packresults (void)
|
||||
{
|
||||
int arg = 0;
|
||||
lua_Object obj;
|
||||
lua_Object table = lua_createtable();
|
||||
while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
|
||||
lua_pushobject(table);
|
||||
lua_pushnumber(arg);
|
||||
lua_pushobject(obj);
|
||||
lua_rawsettable();
|
||||
}
|
||||
lua_pushobject(table);
|
||||
lua_pushstring("n");
|
||||
lua_pushnumber(arg-1);
|
||||
lua_rawsettable();
|
||||
lua_pushobject(table); /* final result */
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: do a string
|
||||
*/
|
||||
static void lua_internaldostring (void)
|
||||
{
|
||||
lua_Object err = lua_getparam(2);
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
luaL_arg_check(lua_isnil(err) || lua_isfunction(err), 2,
|
||||
"must be a valid error handler");
|
||||
lua_pushobject(err);
|
||||
err = lua_seterrormethod();
|
||||
}
|
||||
if (lua_dostring(luaL_check_string(1)) == 0)
|
||||
if (passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(err);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -149,14 +188,8 @@ static char *tostring (lua_Object obj)
|
||||
case LUA_T_CFUNCTION: case LUA_T_NIL:
|
||||
return luaI_typenames[-ttype(o)];
|
||||
case LUA_T_USERDATA: {
|
||||
char *buff = luaI_buffer(100);
|
||||
int size = o->value.ts->size;
|
||||
int i;
|
||||
strcpy(buff, "userdata: ");
|
||||
if (size > 10) size = 10;
|
||||
for (i=0; i<size; i++)
|
||||
sprintf(buff+strlen(buff), "%.2X",
|
||||
(int)(unsigned char)o->value.ts->str[i]);
|
||||
char *buff = luaI_buffer(30);
|
||||
sprintf(buff, "userdata: %p", o->value.ts->u.v);
|
||||
return buff;
|
||||
}
|
||||
default: return "<unknown object>";
|
||||
@@ -242,37 +275,37 @@ static void luatag (void)
|
||||
lua_pushnumber(lua_tag(lua_getparam(1)));
|
||||
}
|
||||
|
||||
#define MAXPARAMS 256
|
||||
|
||||
static int getnarg (lua_Object table)
|
||||
{
|
||||
lua_Object temp;
|
||||
/* temp = table.n */
|
||||
lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
|
||||
return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
|
||||
}
|
||||
|
||||
static void luaI_call (void)
|
||||
{
|
||||
lua_Object f = lua_getparam(1);
|
||||
lua_Object arg = lua_getparam(2);
|
||||
lua_Object temp, params[MAXPARAMS];
|
||||
int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
|
||||
int narg, i;
|
||||
luaL_arg_check(lua_isfunction(f), 1, "function expected");
|
||||
luaL_arg_check(lua_istable(arg), 2, "table expected");
|
||||
/* narg = arg.n */
|
||||
lua_pushobject(arg);
|
||||
lua_pushstring("n");
|
||||
temp = lua_getsubscript();
|
||||
narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
|
||||
/* read arg[1...n] */
|
||||
narg = getnarg(arg);
|
||||
/* push arg[1...n] */
|
||||
for (i=0; i<narg; i++) {
|
||||
if (i>=MAXPARAMS)
|
||||
lua_error("argument list too long in function `call'");
|
||||
lua_pushobject(arg);
|
||||
lua_pushnumber(i+1);
|
||||
params[i] = lua_getsubscript();
|
||||
if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
|
||||
narg = i;
|
||||
lua_Object temp;
|
||||
/* temp = arg[i+1] */
|
||||
lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
|
||||
if (narg == MAX_WORD && lua_isnil(temp))
|
||||
break;
|
||||
}
|
||||
lua_pushobject(temp);
|
||||
}
|
||||
/* push parameters and do the call */
|
||||
for (i=0; i<narg; i++)
|
||||
lua_pushobject(params[i]);
|
||||
if (lua_callfunction(f))
|
||||
lua_error(NULL);
|
||||
else if (withtable)
|
||||
packresults();
|
||||
else
|
||||
passresults();
|
||||
}
|
||||
@@ -280,7 +313,7 @@ static void luaI_call (void)
|
||||
static void luaIl_settag (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
luaL_arg_check(o != LUA_NOOBJECT, 1, NULL);
|
||||
luaL_arg_check(lua_istable(o), 1, "table expected");
|
||||
lua_pushobject(o);
|
||||
lua_settag(luaL_check_number(2));
|
||||
}
|
||||
@@ -315,6 +348,11 @@ static void rawsettable (void)
|
||||
}
|
||||
|
||||
|
||||
static void luaI_collectgarbage (void)
|
||||
{
|
||||
lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal functions
|
||||
@@ -325,6 +363,7 @@ static struct {
|
||||
} int_funcs[] = {
|
||||
{"assert", luaI_assert},
|
||||
{"call", luaI_call},
|
||||
{"collectgarbage", luaI_collectgarbage},
|
||||
{"dofile", lua_internaldofile},
|
||||
{"dostring", lua_internaldostring},
|
||||
{"error", luaI_error},
|
||||
@@ -338,7 +377,9 @@ static struct {
|
||||
{"rawsetglobal", luaI_rawsetglobal},
|
||||
{"rawsettable", rawsettable},
|
||||
{"seterrormethod", luaI_seterrormethod},
|
||||
#if LUA_COMPAT2_5
|
||||
{"setfallback", luaI_setfallback},
|
||||
#endif
|
||||
{"setglobal", luaI_setglobal},
|
||||
{"settagmethod", luaI_settagmethod},
|
||||
{"gettagmethod", luaI_gettagmethod},
|
||||
@@ -360,7 +401,7 @@ void luaI_predefine (void)
|
||||
n = luaI_findsymbolbyname(int_funcs[i].name);
|
||||
s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
|
||||
}
|
||||
n = luaI_findsymbolbyname("_VERSION_");
|
||||
n = luaI_findsymbolbyname("_VERSION");
|
||||
s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
|
||||
}
|
||||
|
||||
|
||||
11
inout.h
11
inout.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: inout.h,v 1.16 1996/05/28 21:07:32 roberto Exp roberto $
|
||||
** $Id: inout.h,v 1.19 1997/06/18 20:35:49 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
@@ -14,11 +14,12 @@ extern Word lua_linenumber;
|
||||
extern Word lua_debugline;
|
||||
extern char *lua_parsedfile;
|
||||
|
||||
FILE *lua_openfile (char *fn);
|
||||
void lua_closefile (void);
|
||||
void lua_openstring (char *s);
|
||||
void lua_closestring (void);
|
||||
void luaI_setparsedfile (char *name);
|
||||
|
||||
void luaI_predefine (void);
|
||||
|
||||
int lua_dobuffer (char *buff, int size);
|
||||
int lua_doFILE (FILE *f, int bin);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
150
iolib.c
150
iolib.c
@@ -10,8 +10,6 @@
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
FILE *lua_infile, *lua_outfile;
|
||||
|
||||
int lua_tagio;
|
||||
|
||||
|
||||
@@ -39,59 +37,80 @@ static void pushresult (int i)
|
||||
}
|
||||
|
||||
|
||||
static void closefile (FILE *f)
|
||||
|
||||
static FILE *getfile (char *name)
|
||||
{
|
||||
if (f == stdin || f == stdout)
|
||||
return;
|
||||
if (f == lua_infile)
|
||||
lua_infile = stdin;
|
||||
if (f == lua_outfile)
|
||||
lua_outfile = stdout;
|
||||
lua_Object f = lua_getglobal(name);
|
||||
if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio)
|
||||
luaL_verror("global variable %s is not a file handle", name);
|
||||
return lua_getuserdata(f);
|
||||
}
|
||||
|
||||
|
||||
static void closefile (char *name)
|
||||
{
|
||||
FILE *f = getfile(name);
|
||||
if (f == stdin || f == stdout) return;
|
||||
if (pclose(f) == -1)
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
static void setfile (FILE *f, char *name)
|
||||
{
|
||||
lua_pushusertag(f, lua_tagio);
|
||||
lua_setglobal(name);
|
||||
}
|
||||
|
||||
|
||||
static void setreturn (FILE *f, char *name)
|
||||
{
|
||||
setfile(f, name);
|
||||
lua_pushusertag(f, lua_tagio);
|
||||
}
|
||||
|
||||
|
||||
static void io_readfrom (void)
|
||||
{
|
||||
FILE *current;
|
||||
lua_Object f = lua_getparam(1);
|
||||
if (f == LUA_NOOBJECT)
|
||||
closefile(lua_infile); /* restore standart input */
|
||||
if (f == LUA_NOOBJECT) {
|
||||
closefile("_INPUT");
|
||||
current = stdin;
|
||||
}
|
||||
else if (lua_tag(f) == lua_tagio)
|
||||
lua_infile = lua_getuserdata(f);
|
||||
current = lua_getuserdata(f);
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||
if (fp)
|
||||
lua_infile = fp;
|
||||
else {
|
||||
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||
if (current == NULL) {
|
||||
pushresult(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pushusertag(lua_infile, lua_tagio);
|
||||
setreturn(current, "_INPUT");
|
||||
}
|
||||
|
||||
|
||||
static void io_writeto (void)
|
||||
{
|
||||
FILE *current;
|
||||
lua_Object f = lua_getparam(1);
|
||||
if (f == LUA_NOOBJECT)
|
||||
closefile(lua_outfile); /* restore standart output */
|
||||
if (f == LUA_NOOBJECT) {
|
||||
closefile("_OUTPUT");
|
||||
current = stdout;
|
||||
}
|
||||
else if (lua_tag(f) == lua_tagio)
|
||||
lua_outfile = lua_getuserdata(f);
|
||||
current = lua_getuserdata(f);
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
||||
if (fp)
|
||||
lua_outfile = fp;
|
||||
else {
|
||||
current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
||||
if (current == NULL) {
|
||||
pushresult(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pushusertag(lua_outfile, lua_tagio);
|
||||
setreturn(current, "_OUTPUT");
|
||||
}
|
||||
|
||||
|
||||
@@ -99,10 +118,8 @@ static void io_appendto (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
FILE *fp = fopen (s, "a");
|
||||
if (fp != NULL) {
|
||||
lua_outfile = fp;
|
||||
lua_pushusertag(lua_outfile, lua_tagio);
|
||||
}
|
||||
if (fp != NULL)
|
||||
setreturn(fp, "_OUTPUT");
|
||||
else
|
||||
pushresult(0);
|
||||
}
|
||||
@@ -112,6 +129,7 @@ static void io_appendto (void)
|
||||
|
||||
static void io_read (void)
|
||||
{
|
||||
FILE *f = getfile("_INPUT");
|
||||
char *buff;
|
||||
char *p = luaL_opt_string(1, "[^\n]*{\n}");
|
||||
int inskip = 0; /* to control {skips} */
|
||||
@@ -131,7 +149,7 @@ static void io_read (void)
|
||||
else {
|
||||
char *ep = luaL_item_end(p); /* get what is next */
|
||||
int m; /* match result */
|
||||
if (c == NEED_OTHER) c = getc(lua_infile);
|
||||
if (c == NEED_OTHER) c = getc(f);
|
||||
m = (c == EOF) ? 0 : luaL_singlematch((char)c, p);
|
||||
if (m) {
|
||||
if (inskip == 0) luaI_addchar(c);
|
||||
@@ -152,7 +170,7 @@ static void io_read (void)
|
||||
}
|
||||
} break_while:
|
||||
if (c >= 0) /* not EOF nor NEED_OTHER? */
|
||||
ungetc(c, lua_infile);
|
||||
ungetc(c, f);
|
||||
buff = luaI_addchar(0);
|
||||
if (*buff != 0 || *p == 0) /* read something or did not fail? */
|
||||
lua_pushstring(buff);
|
||||
@@ -161,11 +179,12 @@ static void io_read (void)
|
||||
|
||||
static void io_write (void)
|
||||
{
|
||||
FILE *f = getfile("_OUTPUT");
|
||||
int arg = 1;
|
||||
int status = 1;
|
||||
char *s;
|
||||
while ((s = luaL_opt_string(arg++, NULL)) != NULL)
|
||||
status = status && (fputs(s, lua_outfile) != EOF);
|
||||
status = status && (fputs(s, f) != EOF);
|
||||
pushresult(status);
|
||||
}
|
||||
|
||||
@@ -242,28 +261,31 @@ static void lua_printstack (FILE *f)
|
||||
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
|
||||
char *name;
|
||||
int currentline;
|
||||
char *filename;
|
||||
int linedefined;
|
||||
lua_funcinfo(func, &filename, &linedefined);
|
||||
fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
|
||||
switch (*lua_getobjname(func, &name)) {
|
||||
case 'g':
|
||||
fprintf(f, "function %s", name);
|
||||
break;
|
||||
case 'f':
|
||||
fprintf(f, "`%s' iternal method", name);
|
||||
case 't':
|
||||
fprintf(f, "`%s' tag method", name);
|
||||
break;
|
||||
default: {
|
||||
char *filename;
|
||||
int linedefined;
|
||||
lua_funcinfo(func, &filename, &linedefined);
|
||||
if (linedefined == 0)
|
||||
fprintf(f, "main of %s", filename);
|
||||
else if (linedefined < 0)
|
||||
fprintf(f, "%s", filename);
|
||||
else
|
||||
fprintf(f, "function (%s:%d)", filename, linedefined);
|
||||
filename = NULL;
|
||||
}
|
||||
}
|
||||
if ((currentline = lua_currentline(func)) > 0)
|
||||
fprintf(f, " at line %d", currentline);
|
||||
if (filename)
|
||||
fprintf(f, " [in file %s]", filename);
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
}
|
||||
@@ -276,49 +298,6 @@ static void errorfb (void)
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------
|
||||
* functions to manipulate userdata
|
||||
*/
|
||||
static void getbyte (void)
|
||||
{
|
||||
lua_Object ud = lua_getparam(1);
|
||||
int i = luaL_opt_number(2, -1);
|
||||
luaL_arg_check(lua_isuserdata(ud), 1, "userdata expected");
|
||||
if (i == -1)
|
||||
lua_pushnumber(lua_getbindatasize(ud));
|
||||
else {
|
||||
i--;
|
||||
if (0 <= i && i < lua_getbindatasize(ud))
|
||||
lua_pushnumber(*(((char *)lua_getbindata(ud))+i));
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
}
|
||||
|
||||
static void newuserdata (void)
|
||||
{
|
||||
lua_Object t = lua_getparam(1);
|
||||
int tag = luaL_opt_number(2, 0);
|
||||
int i;
|
||||
luaI_emptybuff();
|
||||
luaL_arg_check(lua_istable(t), 1, "table expected");
|
||||
for (i=0; ; i++) {
|
||||
lua_Object o;
|
||||
lua_beginblock();
|
||||
lua_pushobject(t);
|
||||
lua_pushnumber(i+1);
|
||||
o = lua_rawgettable();
|
||||
if (lua_isnil(o)) {
|
||||
lua_endblock();
|
||||
break;
|
||||
}
|
||||
luaL_arg_check(lua_isnumber(o), 1, "table values must be numbers");
|
||||
luaI_addchar(lua_getnumber(o));
|
||||
lua_endblock();
|
||||
}
|
||||
lua_pushbindata(luaI_addchar(0), i, tag);
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg iolib[] = {
|
||||
{"readfrom", io_readfrom},
|
||||
@@ -334,15 +313,18 @@ static struct luaL_reg iolib[] = {
|
||||
{"date", io_date},
|
||||
{"exit", io_exit},
|
||||
{"debug", io_debug},
|
||||
{"getbyte", getbyte},
|
||||
{"userdata", newuserdata},
|
||||
{"print_stack", errorfb}
|
||||
};
|
||||
|
||||
void iolib_open (void)
|
||||
{
|
||||
lua_tagio = lua_newtag();
|
||||
lua_infile=stdin; lua_outfile=stdout;
|
||||
setfile(stdin, "_INPUT");
|
||||
setfile(stdout, "_OUTPUT");
|
||||
setfile(stdin, "_STDIN");
|
||||
setfile(stdout, "_STDOUT");
|
||||
setfile(stderr, "_STDERR");
|
||||
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
||||
lua_seterrormethod(errorfb);
|
||||
lua_pushcfunction(errorfb);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
|
||||
103
lex.c
103
lex.c
@@ -1,4 +1,4 @@
|
||||
char *rcs_lex = "$Id: lex.c,v 2.46 1997/04/07 14:48:53 roberto Exp roberto $";
|
||||
char *rcs_lex = "$Id: lex.c,v 3.4 1997/06/11 18:56:02 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
@@ -15,34 +15,36 @@ char *rcs_lex = "$Id: lex.c,v 2.46 1997/04/07 14:48:53 roberto Exp roberto $";
|
||||
|
||||
#define MINBUFF 250
|
||||
|
||||
#define next() (current = input())
|
||||
static int current; /* look ahead character */
|
||||
static ZIO *lex_z;
|
||||
|
||||
|
||||
#define next() (current = zgetc(lex_z))
|
||||
#define save(x) (yytext[tokensize++] = (x))
|
||||
#define save_and_next() (save(current), next())
|
||||
|
||||
|
||||
static int current; /* look ahead character */
|
||||
static Input input; /* input function */
|
||||
#define MAX_IFS 5
|
||||
|
||||
#define MAX_IFS 10
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
|
||||
|
||||
static struct {
|
||||
int elsepart; /* true if its in the $else part */
|
||||
int condition; /* true if $if condition is true */
|
||||
int skip; /* true if part must be skiped */
|
||||
} ifstate[MAX_IFS];
|
||||
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is
|
||||
** dealing with. The first bit indicates whether the $if condition
|
||||
** is false or true. The second bit indicates whether the lexical is
|
||||
** inside the "then" part (0) or the "else" part (2)
|
||||
*/
|
||||
static int ifstate[MAX_IFS]; /* 0 => then part - condition false */
|
||||
/* 1 => then part - condition true */
|
||||
/* 2 => else part - condition false */
|
||||
/* 3 => else part - condition true */
|
||||
static int iflevel; /* level of nested $if's */
|
||||
|
||||
|
||||
void lua_setinput (Input fn)
|
||||
void lua_setinput (ZIO *z)
|
||||
{
|
||||
current = '\n';
|
||||
lua_linenumber = 0;
|
||||
iflevel = 0;
|
||||
input = fn;
|
||||
ifstate[0].skip = 0;
|
||||
ifstate[0].elsepart = 1; /* to avoid a free $else */
|
||||
lex_z = z;
|
||||
}
|
||||
|
||||
|
||||
@@ -117,11 +119,10 @@ static void skipspace (void)
|
||||
|
||||
static int checkcond (char *buff)
|
||||
{
|
||||
if (strcmp(buff, "nil") == 0)
|
||||
return 0;
|
||||
else if (strcmp(buff, "1") == 0)
|
||||
return 1;
|
||||
else if (isalpha((unsigned char)buff[0]))
|
||||
static char *opts[] = {"nil", "1"};
|
||||
int i = luaI_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaI_globaldefined(buff);
|
||||
else {
|
||||
luaI_auxsynterrbf("invalid $if condition", buff);
|
||||
@@ -134,7 +135,7 @@ static void readname (char *buff)
|
||||
{
|
||||
int i = 0;
|
||||
skipspace();
|
||||
while (isalnum((unsigned char)current)) {
|
||||
while (isalnum((unsigned char)current) || current == '_') {
|
||||
if (i >= PRAGMASIZE) {
|
||||
buff[PRAGMASIZE] = 0;
|
||||
luaI_auxsynterrbf("pragma too long", buff);
|
||||
@@ -149,13 +150,12 @@ static void readname (char *buff)
|
||||
static void inclinenumber (void);
|
||||
|
||||
|
||||
static void ifskip (int thisiflevel)
|
||||
static void ifskip (void)
|
||||
{
|
||||
while (iflevel > thisiflevel &&
|
||||
(ifstate[thisiflevel] == 0 || ifstate[thisiflevel] == 3)) {
|
||||
while (ifstate[iflevel].skip) {
|
||||
if (current == '\n')
|
||||
inclinenumber();
|
||||
else if (current == 0)
|
||||
else if (current == EOZ)
|
||||
luaI_auxsyntaxerror("input ends inside a $if");
|
||||
else next();
|
||||
}
|
||||
@@ -165,49 +165,60 @@ static void ifskip (int thisiflevel)
|
||||
static void inclinenumber (void)
|
||||
{
|
||||
static char *pragmas [] =
|
||||
{"debug", "nodebug", "end", "ifnot", "if", "else", NULL};
|
||||
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
|
||||
next(); /* skip '\n' */
|
||||
++lua_linenumber;
|
||||
if (current == '$') { /* is a pragma? */
|
||||
char buff[PRAGMASIZE+1];
|
||||
int ifnot = 0;
|
||||
int skip = ifstate[iflevel].skip;
|
||||
next(); /* skip $ */
|
||||
readname(buff);
|
||||
switch (luaI_findstring(buff, pragmas)) {
|
||||
case 0: /* debug */
|
||||
lua_debug = 1;
|
||||
if (!skip) lua_debug = 1;
|
||||
break;
|
||||
case 1: /* nodebug */
|
||||
lua_debug = 0;
|
||||
if (!skip) lua_debug = 0;
|
||||
break;
|
||||
case 2: /* end */
|
||||
if (--iflevel < 0)
|
||||
case 2: /* endinput */
|
||||
if (!skip) {
|
||||
current = EOZ;
|
||||
iflevel = 0; /* to allow $endinput inside a $if */
|
||||
}
|
||||
break;
|
||||
case 3: /* end */
|
||||
if (iflevel-- == 0)
|
||||
luaI_auxsyntaxerror("unmatched $endif");
|
||||
break;
|
||||
case 3: /* ifnot */
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 4: /* if */
|
||||
if (iflevel == MAX_IFS)
|
||||
case 5: /* if */
|
||||
if (iflevel == MAX_IFS-1)
|
||||
luaI_auxsyntaxerror("too many nested `$ifs'");
|
||||
readname(buff);
|
||||
ifstate[iflevel++] = checkcond(buff) ? !ifnot : ifnot;
|
||||
iflevel++;
|
||||
ifstate[iflevel].elsepart = 0;
|
||||
ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
|
||||
ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
|
||||
break;
|
||||
case 5: /* else */
|
||||
if (iflevel <= 0 || (ifstate[iflevel-1] & 2))
|
||||
case 6: /* else */
|
||||
if (ifstate[iflevel].elsepart)
|
||||
luaI_auxsyntaxerror("unmatched $else");
|
||||
ifstate[iflevel-1] = ifstate[iflevel-1] | 2;
|
||||
ifstate[iflevel].elsepart = 1;
|
||||
ifstate[iflevel].skip =
|
||||
ifstate[iflevel-1].skip || ifstate[iflevel].condition;
|
||||
break;
|
||||
default:
|
||||
luaI_auxsynterrbf("invalid pragma", buff);
|
||||
}
|
||||
skipspace();
|
||||
if (current == '\n') /* pragma must end with a '\n' */
|
||||
if (current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber();
|
||||
else if (current != 0) /* or eof */
|
||||
else if (current != EOZ) /* or eof */
|
||||
luaI_auxsyntaxerror("invalid pragma format");
|
||||
if (iflevel > 0)
|
||||
ifskip(iflevel-1);
|
||||
ifskip();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,7 +232,7 @@ static int read_long_string (char *yytext, int buffsize)
|
||||
yytext = luaI_buffer(buffsize *= 2);
|
||||
switch (current)
|
||||
{
|
||||
case 0:
|
||||
case EOZ:
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '[':
|
||||
@@ -284,7 +295,7 @@ int luaY_lex (void)
|
||||
case '-':
|
||||
save_and_next();
|
||||
if (current != '-') return '-';
|
||||
do { next(); } while (current != '\n' && current != 0);
|
||||
do { next(); } while (current != '\n' && current != EOZ);
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
@@ -327,7 +338,7 @@ int luaY_lex (void)
|
||||
yytext = luaI_buffer(buffsize *= 2);
|
||||
switch (current)
|
||||
{
|
||||
case 0:
|
||||
case EOZ:
|
||||
case '\n':
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
@@ -444,7 +455,7 @@ int luaY_lex (void)
|
||||
return NUMBER;
|
||||
}
|
||||
|
||||
case 0:
|
||||
case EOZ:
|
||||
save(0);
|
||||
if (iflevel > 0)
|
||||
luaI_syntaxerror("missing $endif");
|
||||
|
||||
7
lex.h
7
lex.h
@@ -1,16 +1,15 @@
|
||||
/*
|
||||
** lex.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: lex.h,v 1.2 1996/02/14 13:35:51 roberto Exp roberto $
|
||||
** $Id: lex.h,v 1.3 1996/11/08 12:49:35 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef lex_h
|
||||
#define lex_h
|
||||
|
||||
#include "zio.h"
|
||||
|
||||
typedef int (*Input) (void);
|
||||
|
||||
void lua_setinput (Input fn);
|
||||
void lua_setinput (ZIO *z);
|
||||
void luaI_syntaxerror (char *s);
|
||||
int luaY_lex (void);
|
||||
void luaI_addReserved (void);
|
||||
|
||||
72
lua.c
72
lua.c
@@ -3,12 +3,13 @@
|
||||
** Linguagem para Usuarios de Aplicacao
|
||||
*/
|
||||
|
||||
char *rcs_lua="$Id: lua.c,v 1.14 1996/09/24 17:30:28 roberto Exp roberto $";
|
||||
char *rcs_lua="$Id: lua.c,v 1.17 1997/06/18 21:20:45 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "auxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
@@ -19,6 +20,74 @@ char *rcs_lua="$Id: lua.c,v 1.14 1996/09/24 17:30:28 roberto Exp roberto $";
|
||||
#endif
|
||||
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
static void testC (void)
|
||||
{
|
||||
#if DEBUG
|
||||
#define getnum(s) ((*s++) - '0')
|
||||
#define getname(s) (nome[0] = *s++, nome)
|
||||
|
||||
static int locks[10];
|
||||
lua_Object reg[10];
|
||||
char nome[2];
|
||||
char *s = luaL_check_string(1);
|
||||
nome[1] = 0;
|
||||
while (1) {
|
||||
switch (*s++) {
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
lua_pushnumber(*(s-1) - '0');
|
||||
break;
|
||||
|
||||
case 'c': reg[getnum(s)] = lua_createtable(); break;
|
||||
|
||||
case 'P': reg[getnum(s)] = lua_pop(); break;
|
||||
|
||||
case 'g': { int n = getnum(s); reg[n] = lua_getglobal(getname(s)); break; }
|
||||
|
||||
case 'G': { int n = getnum(s);
|
||||
reg[n] = lua_rawgetglobal(getname(s));
|
||||
break;
|
||||
}
|
||||
|
||||
case 'l': locks[getnum(s)] = lua_ref(1); break;
|
||||
case 'L': locks[getnum(s)] = lua_ref(0); break;
|
||||
|
||||
case 'r': { int n = getnum(s); reg[n] = lua_getref(locks[getnum(s)]); break; }
|
||||
|
||||
case 'u': lua_unref(locks[getnum(s)]); break;
|
||||
|
||||
case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
|
||||
|
||||
case '=': lua_setglobal(getname(s)); break;
|
||||
|
||||
case 's': lua_pushstring(getname(s)); break;
|
||||
|
||||
case 'o': lua_pushobject(reg[getnum(s)]); break;
|
||||
|
||||
case 'f': lua_call(getname(s)); break;
|
||||
|
||||
case 'i': reg[getnum(s)] = lua_gettable(); break;
|
||||
|
||||
case 'I': reg[getnum(s)] = lua_rawgettable(); break;
|
||||
|
||||
case 't': lua_settable(); break;
|
||||
|
||||
case 'T': lua_rawsettable(); break;
|
||||
|
||||
default: luaL_verror("unknown command in `testC': %c", *(s-1));
|
||||
|
||||
}
|
||||
if (*s == 0) return;
|
||||
if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
|
||||
}
|
||||
#else
|
||||
lua_error("`testC' not active");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (void)
|
||||
{
|
||||
if (isatty(0)) {
|
||||
@@ -41,6 +110,7 @@ int main (int argc, char *argv[])
|
||||
iolib_open ();
|
||||
strlib_open ();
|
||||
mathlib_open ();
|
||||
lua_register("testC", testC);
|
||||
if (argc < 2)
|
||||
manual_input();
|
||||
else for (i=1; i<argc; i++) {
|
||||
|
||||
58
lua.h
58
lua.h
@@ -2,26 +2,28 @@
|
||||
** LUA - An Extensible Extension Language
|
||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||
** e-mail: lua@tecgraf.puc-rio.br
|
||||
** $Id: lua.h,v 4.1 1997/04/03 18:26:08 roberto Exp roberto $
|
||||
** $Id: lua.h,v 4.10 1997/06/19 18:03:04 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 3.0 (alpha)"
|
||||
#define LUA_VERSION "Lua 3.0"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-1997 TeCGraf"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
#define LUA_NOOBJECT 0
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
|
||||
typedef void (*lua_CFunction) (void);
|
||||
typedef unsigned int lua_Object;
|
||||
|
||||
void lua_settagmethod (int tag, char *event, lua_CFunction method);
|
||||
void lua_gettagmethod (int tag, char *event); /* out: method */
|
||||
void lua_seterrormethod (lua_CFunction method);
|
||||
lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
|
||||
lua_Object lua_gettagmethod (int tag, char *event);
|
||||
lua_Object lua_seterrormethod (void); /* In: new method */
|
||||
|
||||
int lua_newtag (void);
|
||||
void lua_settag (int tag); /* In: object */
|
||||
@@ -31,8 +33,6 @@ int lua_dofile (char *filename); /* Out: returns */
|
||||
int lua_dostring (char *string); /* Out: returns */
|
||||
int lua_callfunction (lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
int lua_call (char *funcname);
|
||||
/* In: parameters; Out: returns */
|
||||
|
||||
void lua_beginblock (void);
|
||||
void lua_endblock (void);
|
||||
@@ -52,17 +52,18 @@ int lua_isfunction (lua_Object object);
|
||||
float lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
void *lua_getbindata (lua_Object object);
|
||||
int lua_getbindatasize (lua_Object object);
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
|
||||
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (float n);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushcfunction (lua_CFunction fn);
|
||||
void lua_pushbindata (void *buff, int size, int tag);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
|
||||
lua_Object lua_pop (void);
|
||||
|
||||
lua_Object lua_getglobal (char *name);
|
||||
lua_Object lua_rawgetglobal (char *name);
|
||||
void lua_setglobal (char *name); /* In: value */
|
||||
@@ -78,15 +79,20 @@ int lua_tag (lua_Object object);
|
||||
|
||||
int lua_ref (int lock); /* In: value */
|
||||
lua_Object lua_getref (int ref);
|
||||
void lua_pushref (int ref);
|
||||
void lua_unref (int ref);
|
||||
|
||||
lua_Object lua_createtable (void);
|
||||
|
||||
long lua_collectgarbage (long limit);
|
||||
|
||||
|
||||
/* =============================================================== */
|
||||
/* some useful macros */
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
|
||||
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
|
||||
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
|
||||
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
|
||||
@@ -94,15 +100,25 @@ lua_Object lua_createtable (void);
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, 0)
|
||||
|
||||
|
||||
/* =============================================================== */
|
||||
/* for compatibility with old versions. Avoid using these macros/functions */
|
||||
|
||||
|
||||
/* ==========================================================================
|
||||
** for compatibility with old versions. Avoid using these macros/functions
|
||||
** If your program does not use any of these, define LUA_COMPAT2_5 to 0
|
||||
*/
|
||||
|
||||
#ifndef LUA_COMPAT2_5
|
||||
#define LUA_COMPAT2_5 1
|
||||
#endif
|
||||
|
||||
|
||||
#if LUA_COMPAT2_5
|
||||
|
||||
|
||||
lua_Object lua_setfallback (char *event, lua_CFunction fallback);
|
||||
|
||||
#define lua_storeglobal(n) lua_setglobal(n)
|
||||
#define lua_type(o) (lua_tag(o))
|
||||
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
#define lua_storeglobal lua_setglobal
|
||||
#define lua_type lua_tag
|
||||
|
||||
#define lua_lockobject(o) lua_refobject(o,1)
|
||||
#define lua_lock() lua_ref(1)
|
||||
@@ -113,9 +129,13 @@ void *lua_getuserdata (lua_Object object);
|
||||
#define lua_pushliteral(o) lua_pushstring(o)
|
||||
|
||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_gettable())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushliteral(f), lua_gettable())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_gettable())
|
||||
|
||||
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
||||
|
||||
#define lua_getsubscript lua_gettable
|
||||
#define lua_storesubscript lua_settable
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
3
lua.stx
3
lua.stx
@@ -1,6 +1,6 @@
|
||||
%{
|
||||
|
||||
char *rcs_luastx = "$Id: lua.stx,v 3.45 1997/03/06 17:30:55 roberto Exp roberto $";
|
||||
char *rcs_luastx = "$Id: lua.stx,v 3.46 1997/03/31 14:19:01 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -489,7 +489,6 @@ body : '(' parlist ')' block END
|
||||
luaI_initTFunc($$);
|
||||
$$->size = pc;
|
||||
$$->code = newvector(pc, Byte);
|
||||
$$->fileName = lua_parsedfile;
|
||||
$$->lineDefined = $2;
|
||||
memcpy($$->code, basepc, pc*sizeof(Byte));
|
||||
if (lua_debug)
|
||||
|
||||
38
makefile
38
makefile
@@ -1,4 +1,4 @@
|
||||
# $Id: makefile,v 1.32 1997/03/31 20:58:42 roberto Exp roberto $
|
||||
# $Id: makefile,v 1.35 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
|
||||
#configuration
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
|
||||
#define (undefine) NOSTRERROR if your system does NOT have function "strerror"
|
||||
# (although this is ANSI, SunOS does not comply; so, add "-DNOSTRERROR" on SunOS)
|
||||
# define LUA_COMPAT2_5=0 if yous system does not need to be compatible with
|
||||
# version 2.5 (or older)
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
@@ -32,7 +34,8 @@ LUAOBJS = \
|
||||
luamem.o \
|
||||
func.o \
|
||||
undump.o \
|
||||
auxlib.o
|
||||
auxlib.o \
|
||||
zio.o
|
||||
|
||||
LIBOBJS = \
|
||||
iolib.o \
|
||||
@@ -77,29 +80,32 @@ clear :
|
||||
co $@
|
||||
|
||||
|
||||
auxlib.o: auxlib.c lua.h auxlib.h
|
||||
auxlib.o: auxlib.c lua.h auxlib.h luadebug.h
|
||||
fallback.o: fallback.c auxlib.h lua.h luamem.h fallback.h opcode.h \
|
||||
types.h tree.h func.h table.h hash.h
|
||||
func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \
|
||||
luamem.h
|
||||
hash.o: hash.c luamem.h opcode.h lua.h types.h tree.h func.h hash.h \
|
||||
table.h auxlib.h
|
||||
inout.o: inout.c auxlib.h lua.h lex.h opcode.h types.h tree.h func.h \
|
||||
inout.h table.h hash.h luamem.h fallback.h
|
||||
inout.o: inout.c auxlib.h lua.h fallback.h opcode.h types.h tree.h \
|
||||
func.h hash.h inout.h lex.h zio.h luamem.h table.h undump.h
|
||||
iolib.o: iolib.c lua.h auxlib.h luadebug.h lualib.h
|
||||
lex.o: lex.c auxlib.h lua.h luamem.h tree.h types.h table.h opcode.h \
|
||||
func.h lex.h inout.h luadebug.h parser.h
|
||||
lua.o: lua.c lua.h lualib.h
|
||||
func.h lex.h zio.h inout.h luadebug.h parser.h
|
||||
lua.o: lua.c lua.h auxlib.h lualib.h
|
||||
luamem.o: luamem.c luamem.h lua.h
|
||||
mathlib.o: mathlib.c lualib.h lua.h auxlib.h
|
||||
opcode.o: opcode.c luadebug.h lua.h luamem.h opcode.h types.h tree.h \
|
||||
func.h hash.h inout.h table.h fallback.h undump.h auxlib.h
|
||||
parser.o: parser.c luadebug.h lua.h luamem.h lex.h opcode.h types.h \
|
||||
tree.h func.h hash.h inout.h table.h
|
||||
func.h hash.h inout.h table.h fallback.h auxlib.h lex.h zio.h
|
||||
parser.o: parser.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
|
||||
types.h tree.h func.h hash.h inout.h table.h
|
||||
strlib.o: strlib.c lua.h auxlib.h lualib.h
|
||||
table.o: table.c luamem.h auxlib.h lua.h opcode.h types.h tree.h \
|
||||
func.h hash.h table.h inout.h fallback.h luadebug.h
|
||||
tree.o: tree.c luamem.h lua.h tree.h types.h lex.h hash.h opcode.h \
|
||||
func.h table.h fallback.h
|
||||
undump.o: undump.c opcode.h lua.h types.h tree.h func.h luamem.h \
|
||||
table.h undump.h
|
||||
table.o: table.c luamem.h auxlib.h lua.h func.h types.h tree.h \
|
||||
opcode.h hash.h table.h inout.h fallback.h luadebug.h
|
||||
tree.o: tree.c luamem.h lua.h tree.h types.h lex.h zio.h hash.h \
|
||||
opcode.h func.h table.h fallback.h
|
||||
undump.o: undump.c auxlib.h lua.h opcode.h types.h tree.h func.h \
|
||||
luamem.h table.h undump.h zio.h
|
||||
y.tab.o: y.tab.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
|
||||
types.h tree.h func.h hash.h inout.h table.h
|
||||
zio.o: zio.c zio.h
|
||||
|
||||
1793
manual.tex
1793
manual.tex
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@
|
||||
** Mathematics library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_mathlib="$Id: mathlib.c,v 1.22 1997/04/04 22:24:51 roberto Exp roberto $";
|
||||
char *rcs_mathlib="$Id: mathlib.c,v 1.24 1997/06/09 17:30:10 roberto Exp roberto $";
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
@@ -210,6 +210,8 @@ static struct luaL_reg mathlib[] = {
|
||||
void mathlib_open (void)
|
||||
{
|
||||
luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
|
||||
lua_settagmethod(0, "pow", math_pow);
|
||||
lua_pushcfunction(math_pow);
|
||||
lua_pushnumber(0); /* to get its tag */
|
||||
lua_settagmethod(lua_tag(lua_pop()), "pow");
|
||||
}
|
||||
|
||||
|
||||
409
opcode.c
409
opcode.c
@@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_opcode="$Id: opcode.c,v 4.1 1997/04/03 18:27:06 roberto Exp roberto $";
|
||||
char *rcs_opcode="$Id: opcode.c,v 4.14 1997/06/23 18:27:53 roberto Exp roberto $";
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
@@ -18,8 +18,8 @@ char *rcs_opcode="$Id: opcode.c,v 4.1 1997/04/03 18:27:06 roberto Exp roberto $"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
#include "fallback.h"
|
||||
#include "undump.h"
|
||||
#include "auxlib.h"
|
||||
#include "lex.h"
|
||||
|
||||
#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
|
||||
#define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
|
||||
@@ -52,13 +52,13 @@ static TObject *top = &initial_stack;
|
||||
#define incr_top if (++top >= stackLimit) growstack()
|
||||
|
||||
struct C_Lua_Stack {
|
||||
StkId base; /* when Lua calls C or C calls Lua, points to */
|
||||
/* the first slot after the last parameter. */
|
||||
int num; /* when Lua calls C, has the number of parameters; */
|
||||
/* when C calls Lua, has the number of results. */
|
||||
StkId base; /* when Lua calls C or C calls Lua, points to */
|
||||
/* the first slot after the last parameter. */
|
||||
StkId lua2C; /* points to first element of "array" lua2C */
|
||||
int num; /* size of "array" lua2C */
|
||||
};
|
||||
|
||||
static struct C_Lua_Stack CLS_current = {0, 0};
|
||||
static struct C_Lua_Stack CLS_current = {0, 0, 0};
|
||||
|
||||
static jmp_buf *errorJmp = NULL; /* current error recover point */
|
||||
|
||||
@@ -178,18 +178,29 @@ static int lua_tostring (TObject *obj)
|
||||
/*
|
||||
** Adjust stack. Set top to the given value, pushing NILs if needed.
|
||||
*/
|
||||
static void adjust_top (StkId newtop)
|
||||
static void adjust_top_aux (StkId newtop)
|
||||
{
|
||||
TObject *nt;
|
||||
lua_checkstack(stack+newtop);
|
||||
nt = stack+newtop; /* warning: previous call may change stack */
|
||||
while (top < nt) ttype(top++) = LUA_T_NIL;
|
||||
top = nt; /* top could be bigger than newtop */
|
||||
}
|
||||
|
||||
|
||||
#define adjust_top(newtop) { if (newtop <= top-stack) \
|
||||
top = stack+newtop; \
|
||||
else adjust_top_aux(newtop); }
|
||||
|
||||
#define adjustC(nParams) adjust_top(CLS_current.base+nParams)
|
||||
|
||||
|
||||
static void checkCparams (int nParams)
|
||||
{
|
||||
if (top-stack < CLS_current.base+nParams)
|
||||
lua_error("API error - wrong number of arguments in C2lua stack");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a hole below "nelems" from the top.
|
||||
*/
|
||||
@@ -202,13 +213,37 @@ static void open_stack (int nelems)
|
||||
}
|
||||
|
||||
|
||||
static lua_Object put_luaObject (TObject *o)
|
||||
{
|
||||
open_stack((top-stack)-CLS_current.base);
|
||||
stack[CLS_current.base++] = *o;
|
||||
return CLS_current.base; /* this is +1 real position (see Ref) */
|
||||
}
|
||||
|
||||
|
||||
static lua_Object put_luaObjectonTop (void)
|
||||
{
|
||||
open_stack((top-stack)-CLS_current.base);
|
||||
stack[CLS_current.base++] = *(--top);
|
||||
return CLS_current.base; /* this is +1 real position (see Ref) */
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_pop (void)
|
||||
{
|
||||
checkCparams(1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** call Line hook
|
||||
*/
|
||||
static void lineHook (int line)
|
||||
{
|
||||
struct C_Lua_Stack oldCLS = CLS_current;
|
||||
StkId old_top = CLS_current.base = top-stack;
|
||||
StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
|
||||
CLS_current.num = 0;
|
||||
(*lua_linehook)(line);
|
||||
top = stack+old_top;
|
||||
@@ -223,7 +258,7 @@ static void lineHook (int line)
|
||||
static void callHook (StkId base, lua_Type type, int isreturn)
|
||||
{
|
||||
struct C_Lua_Stack oldCLS = CLS_current;
|
||||
StkId old_top = CLS_current.base = top-stack;
|
||||
StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
|
||||
CLS_current.num = 0;
|
||||
if (isreturn)
|
||||
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
|
||||
@@ -251,6 +286,7 @@ static StkId callC (lua_CFunction func, StkId base)
|
||||
StkId firstResult;
|
||||
CLS_current.num = (top-stack) - base;
|
||||
/* incorporate parameters on the stack */
|
||||
CLS_current.lua2C = base;
|
||||
CLS_current.base = base+CLS_current.num; /* == top-stack */
|
||||
if (lua_callhook)
|
||||
callHook(base, LUA_T_CMARK, 0);
|
||||
@@ -290,7 +326,7 @@ static void do_call (StkId base, int nResults)
|
||||
firstResult = lua_execute(func->value.tf->code, base);
|
||||
}
|
||||
else { /* func is not a function */
|
||||
/* Check the fallback for invalid functions */
|
||||
/* Check the tag method for invalid functions */
|
||||
TObject *im = luaI_getimbyObj(func, IM_FUNCTION);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("call expression not a function");
|
||||
@@ -300,7 +336,7 @@ static void do_call (StkId base, int nResults)
|
||||
return;
|
||||
}
|
||||
/* adjust the number of results */
|
||||
if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
|
||||
if (nResults != MULT_RET)
|
||||
adjust_top(firstResult+nResults);
|
||||
/* move results to base-1 (to erase parameters and function) */
|
||||
base--;
|
||||
@@ -317,9 +353,13 @@ static void do_call (StkId base, int nResults)
|
||||
*/
|
||||
static void pushsubscript (void)
|
||||
{
|
||||
int tg = luaI_tag(top-2);
|
||||
TObject *im = luaI_getim(tg, IM_GETTABLE);
|
||||
if (ttype(top-2) == LUA_T_ARRAY && ttype(im) == LUA_T_NIL) {
|
||||
TObject *im;
|
||||
if (ttype(top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
|
||||
im = luaI_getimbyObj(top-2, IM_GETTABLE);
|
||||
else { /* object is a table... */
|
||||
int tg = (top-2)->value.a->htag;
|
||||
im = luaI_getim(tg, IM_GETTABLE);
|
||||
if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
|
||||
TObject *h = lua_hashget(avalue(top-2), top-1);
|
||||
if (h != NULL && ttype(h) != LUA_T_NIL) {
|
||||
--top;
|
||||
@@ -331,19 +371,21 @@ static void pushsubscript (void)
|
||||
--top;
|
||||
ttype(top-1) = LUA_T_NIL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* else it has a "gettable" method, go through to next command */
|
||||
}
|
||||
else { /* object is not a table, and/or has a specific "gettable" method */
|
||||
if (ttype(im) != LUA_T_NIL)
|
||||
callIM(im, 2, 1);
|
||||
else
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
/* object is not a table, or it has a "gettable" method */
|
||||
if (ttype(im) != LUA_T_NIL)
|
||||
callIM(im, 2, 1);
|
||||
else
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawgettable (void)
|
||||
{
|
||||
adjustC(2);
|
||||
checkCparams(2);
|
||||
if (ttype(top-2) != LUA_T_ARRAY)
|
||||
lua_error("indexed expression not a table in raw gettable");
|
||||
else {
|
||||
@@ -354,8 +396,7 @@ lua_Object lua_rawgettable (void)
|
||||
else
|
||||
ttype(top-1) = LUA_T_NIL;
|
||||
}
|
||||
CLS_current.base++; /* incorporate object in the stack */
|
||||
return (Ref(top-1));
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
@@ -426,9 +467,7 @@ void lua_travstack (int (*fn)(TObject *))
|
||||
static void lua_message (char *s)
|
||||
{
|
||||
TObject *im = luaI_geterrorim();
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
fprintf(stderr, "lua: %s\n", s);
|
||||
else {
|
||||
if (ttype(im) != LUA_T_NIL) {
|
||||
lua_pushstring(s);
|
||||
callIM(im, 1, 0);
|
||||
}
|
||||
@@ -486,7 +525,7 @@ int lua_setlocal (lua_Function func, int local_number)
|
||||
{
|
||||
TObject *f = Address(func);
|
||||
char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
|
||||
adjustC(1);
|
||||
checkCparams(1);
|
||||
--top;
|
||||
if (name)
|
||||
{
|
||||
@@ -505,18 +544,21 @@ int lua_setlocal (lua_Function func, int local_number)
|
||||
*/
|
||||
static void do_callinc (int nResults)
|
||||
{
|
||||
do_call(CLS_current.base+1, nResults);
|
||||
CLS_current.num = (top-stack) - CLS_current.base; /* number of results */
|
||||
CLS_current.base += CLS_current.num; /* incorporate results on the stack */
|
||||
StkId base = CLS_current.base;
|
||||
do_call(base+1, nResults);
|
||||
CLS_current.lua2C = base; /* position of the new results */
|
||||
CLS_current.num = (top-stack) - base; /* number of results */
|
||||
CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
|
||||
}
|
||||
|
||||
|
||||
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
|
||||
{
|
||||
adjustC(nParams);
|
||||
open_stack((top-stack)-CLS_current.base);
|
||||
stack[CLS_current.base].ttype = LUA_T_CFUNCTION;
|
||||
stack[CLS_current.base].value.f = f;
|
||||
do_callinc(nResults);
|
||||
StkId base = (top-stack)-nParams;
|
||||
open_stack(nParams);
|
||||
stack[base].ttype = LUA_T_CFUNCTION;
|
||||
stack[base].value.f = f;
|
||||
do_call(base+1, nResults);
|
||||
}
|
||||
|
||||
|
||||
@@ -554,7 +596,8 @@ int luaI_dorun (TFunc *tf)
|
||||
return status;
|
||||
}
|
||||
|
||||
static int do_protectedmain (void)
|
||||
|
||||
int lua_domain (void)
|
||||
{
|
||||
TFunc tf;
|
||||
int status;
|
||||
@@ -562,23 +605,21 @@ static int do_protectedmain (void)
|
||||
jmp_buf *oldErr = errorJmp;
|
||||
errorJmp = &myErrorJmp;
|
||||
luaI_initTFunc(&tf);
|
||||
tf.fileName = lua_parsedfile;
|
||||
if (setjmp(myErrorJmp) == 0)
|
||||
{
|
||||
if (setjmp(myErrorJmp) == 0) {
|
||||
lua_parse(&tf);
|
||||
status = luaI_dorun(&tf);
|
||||
status = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
status = 1;
|
||||
else {
|
||||
adjustC(0); /* erase extra slot */
|
||||
status = 1;
|
||||
}
|
||||
if (status == 0)
|
||||
status = luaI_dorun(&tf);
|
||||
errorJmp = oldErr;
|
||||
luaI_free(tf.code);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute the given lua function. Return 0 on success or 1 on error.
|
||||
*/
|
||||
@@ -595,90 +636,31 @@ int lua_callfunction (lua_Object function)
|
||||
}
|
||||
|
||||
|
||||
int lua_call (char *funcname)
|
||||
{
|
||||
Word n = luaI_findsymbolbyname(funcname);
|
||||
open_stack((top-stack)-CLS_current.base);
|
||||
stack[CLS_current.base] = s_object(n);
|
||||
return do_protectedrun(MULT_RET);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open file, generate opcode and execute global statement. Return 0 on
|
||||
** success or non 0 on error.
|
||||
*/
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
int status;
|
||||
int c;
|
||||
FILE *f = lua_openfile(filename);
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
if (c == ID_CHUNK) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
status = luaI_undump(f);
|
||||
}
|
||||
else {
|
||||
if (c == '#')
|
||||
while ((c=fgetc(f)) != '\n') /* skip first line */;
|
||||
status = do_protectedmain();
|
||||
}
|
||||
lua_closefile();
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
** Generate opcode stored on string and execute global statement. Return 0 on
|
||||
** success or non 0 on error.
|
||||
*/
|
||||
int lua_dostring (char *str)
|
||||
{
|
||||
int status;
|
||||
if (str == NULL)
|
||||
return 1;
|
||||
lua_openstring(str);
|
||||
status = do_protectedmain();
|
||||
lua_closestring();
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** API: set a function as a fallback
|
||||
*/
|
||||
lua_Object lua_setfallback (char *name, lua_CFunction fallback)
|
||||
{
|
||||
lua_pushstring(name);
|
||||
lua_pushcfunction(fallback);
|
||||
do_unprotectedrun(luaI_setfallback, 2, 1);
|
||||
return (Ref(top-1));
|
||||
}
|
||||
|
||||
void lua_gettagmethod (int tag, char *event)
|
||||
lua_Object lua_gettagmethod (int tag, char *event)
|
||||
{
|
||||
lua_pushnumber(tag);
|
||||
lua_pushstring(event);
|
||||
do_unprotectedrun(luaI_gettagmethod, 2, 1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
void lua_settagmethod (int tag, char *event, lua_CFunction method)
|
||||
lua_Object lua_settagmethod (int tag, char *event)
|
||||
{
|
||||
TObject newmethod;
|
||||
checkCparams(1);
|
||||
newmethod = *(--top);
|
||||
lua_pushnumber(tag);
|
||||
lua_pushstring(event);
|
||||
if (method)
|
||||
lua_pushcfunction (method);
|
||||
else
|
||||
lua_pushnil();
|
||||
*top = newmethod; incr_top;
|
||||
do_unprotectedrun(luaI_settagmethod, 3, 1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
void lua_seterrormethod (lua_CFunction method)
|
||||
lua_Object lua_seterrormethod (void)
|
||||
{
|
||||
lua_pushcfunction (method);
|
||||
do_unprotectedrun(luaI_seterrormethod, 1, 0);
|
||||
checkCparams(1);
|
||||
do_unprotectedrun(luaI_seterrormethod, 1, 1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
@@ -688,10 +670,9 @@ void lua_seterrormethod (lua_CFunction method)
|
||||
*/
|
||||
lua_Object lua_gettable (void)
|
||||
{
|
||||
adjustC(2);
|
||||
checkCparams(2);
|
||||
pushsubscript();
|
||||
CLS_current.base++; /* incorporate object in the stack */
|
||||
return (Ref(top-1));
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
@@ -723,7 +704,7 @@ void lua_endblock (void)
|
||||
|
||||
void lua_settag (int tag)
|
||||
{
|
||||
adjustC(1);
|
||||
checkCparams(1);
|
||||
luaI_settag(tag, --top);
|
||||
}
|
||||
|
||||
@@ -732,13 +713,13 @@ void lua_settag (int tag)
|
||||
*/
|
||||
void lua_settable (void)
|
||||
{
|
||||
adjustC(3);
|
||||
checkCparams(3);
|
||||
storesubscript(top-3, 1);
|
||||
}
|
||||
|
||||
void lua_rawsettable (void)
|
||||
{
|
||||
adjustC(3);
|
||||
checkCparams(3);
|
||||
storesubscript(top-3, 0);
|
||||
}
|
||||
|
||||
@@ -747,12 +728,10 @@ void lua_rawsettable (void)
|
||||
*/
|
||||
lua_Object lua_createtable (void)
|
||||
{
|
||||
adjustC(0);
|
||||
avalue(top) = lua_createarray(0);
|
||||
ttype(top) = LUA_T_ARRAY;
|
||||
incr_top;
|
||||
CLS_current.base++; /* incorporate object in the stack */
|
||||
return Ref(top-1);
|
||||
TObject o;
|
||||
avalue(&o) = lua_createarray(0);
|
||||
ttype(&o) = LUA_T_ARRAY;
|
||||
return put_luaObject(&o);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -761,10 +740,10 @@ lua_Object lua_createtable (void)
|
||||
*/
|
||||
lua_Object lua_lua2C (int number)
|
||||
{
|
||||
if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
|
||||
/* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
|
||||
stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
|
||||
return CLS_current.base-CLS_current.num+number;
|
||||
if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
|
||||
/* Ref(stack+(CLS_current.lua2C+number-1)) ==
|
||||
stack+(CLS_current.lua2C+number-1)-stack+1 == */
|
||||
return CLS_current.lua2C+number;
|
||||
}
|
||||
|
||||
int lua_isnil (lua_Object o)
|
||||
@@ -821,33 +800,20 @@ real lua_getnumber (lua_Object object)
|
||||
*/
|
||||
char *lua_getstring (lua_Object object)
|
||||
{
|
||||
if (object == LUA_NOOBJECT) return NULL;
|
||||
if (tostring (Address(object))) return NULL;
|
||||
else return (svalue(Address(object)));
|
||||
if (object == LUA_NOOBJECT || tostring (Address(object)))
|
||||
return NULL;
|
||||
else return (svalue(Address(object)));
|
||||
}
|
||||
|
||||
void *lua_getbindata (lua_Object object)
|
||||
{
|
||||
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
|
||||
return NULL;
|
||||
else return svalue(Address(object));
|
||||
}
|
||||
|
||||
void *lua_getuserdata (lua_Object object)
|
||||
{
|
||||
void *add = lua_getbindata(object);
|
||||
if (add == NULL) return NULL;
|
||||
else return *(void **)add;
|
||||
}
|
||||
|
||||
|
||||
int lua_getbindatasize (lua_Object object)
|
||||
{
|
||||
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
|
||||
return 0;
|
||||
else return (Address(object))->value.ts->size;
|
||||
return NULL;
|
||||
else return tsvalue(Address(object))->u.v;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given an object handle, return its cfuntion pointer. On error, return NULL.
|
||||
*/
|
||||
@@ -865,25 +831,13 @@ lua_Object lua_getref (int ref)
|
||||
TObject *o = luaI_getref(ref);
|
||||
if (o == NULL)
|
||||
return LUA_NOOBJECT;
|
||||
adjustC(0);
|
||||
luaI_pushobject(o);
|
||||
CLS_current.base++; /* incorporate object in the stack */
|
||||
return Ref(top-1);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushref (int ref)
|
||||
{
|
||||
TObject *o = luaI_getref(ref);
|
||||
if (o == NULL)
|
||||
lua_error("access to invalid reference (possibly garbage collected)");
|
||||
luaI_pushobject(o);
|
||||
return put_luaObject(o);
|
||||
}
|
||||
|
||||
|
||||
int lua_ref (int lock)
|
||||
{
|
||||
adjustC(1);
|
||||
checkCparams(1);
|
||||
return luaI_ref(--top, lock);
|
||||
}
|
||||
|
||||
@@ -894,20 +848,14 @@ int lua_ref (int lock)
|
||||
*/
|
||||
lua_Object lua_getglobal (char *name)
|
||||
{
|
||||
adjustC(0);
|
||||
getglobal(luaI_findsymbolbyname(name));
|
||||
CLS_current.base++; /* incorporate object in the stack */
|
||||
return Ref(top-1);
|
||||
getglobal(luaI_findsymbolbyname(name));
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawgetglobal (char *name)
|
||||
{
|
||||
adjustC(0);
|
||||
*top = lua_table[luaI_findsymbolbyname(name)].object;
|
||||
incr_top;
|
||||
CLS_current.base++; /* incorporate object in the stack */
|
||||
return Ref(top-1);
|
||||
return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object);
|
||||
}
|
||||
|
||||
|
||||
@@ -935,15 +883,15 @@ static void setglobal (Word n)
|
||||
|
||||
void lua_setglobal (char *name)
|
||||
{
|
||||
adjustC(1);
|
||||
checkCparams(1);
|
||||
setglobal(luaI_findsymbolbyname(name));
|
||||
}
|
||||
|
||||
void lua_rawsetglobal (char *name)
|
||||
{
|
||||
Word n = luaI_findsymbolbyname(name);
|
||||
adjustC(1);
|
||||
s_object(n) = *(--top);
|
||||
Word n = luaI_findsymbolbyname(name);
|
||||
checkCparams(1);
|
||||
s_object(n) = *(--top);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -951,8 +899,8 @@ void lua_rawsetglobal (char *name)
|
||||
*/
|
||||
void lua_pushnil (void)
|
||||
{
|
||||
ttype(top) = LUA_T_NIL;
|
||||
incr_top;
|
||||
ttype(top) = LUA_T_NIL;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -978,8 +926,7 @@ void lua_pushstring (char *s)
|
||||
}
|
||||
incr_top;
|
||||
}
|
||||
/*>>>>>>>>>#undef lua_pushliteral
|
||||
void lua_pushliteral(char *s) { lua_pushstring(s); }*/
|
||||
|
||||
|
||||
/*
|
||||
** Push an object (ttype=cfunction) to stack.
|
||||
@@ -990,25 +937,15 @@ void lua_pushcfunction (lua_CFunction fn)
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushbindata (void *buff, int size, int tag)
|
||||
{
|
||||
if (buff == NULL)
|
||||
ttype(top) = LUA_T_NIL;
|
||||
else {
|
||||
if (tag < 0)
|
||||
luaI_realtag(tag);
|
||||
tsvalue(top) = luaI_createuserdata(buff, size, tag);
|
||||
ttype(top) = LUA_T_USERDATA;
|
||||
}
|
||||
incr_top;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object (ttype=userdata) to stack.
|
||||
*/
|
||||
|
||||
void lua_pushusertag (void *u, int tag)
|
||||
{
|
||||
lua_pushbindata(&u, sizeof(void *), tag);
|
||||
if (tag < 0 && tag != LUA_ANYTAG)
|
||||
luaI_realtag(tag); /* error if tag is not valid */
|
||||
tsvalue(top) = luaI_createudata(u, tag);
|
||||
ttype(top) = LUA_T_USERDATA;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1026,16 +963,25 @@ void luaI_pushobject (TObject *o)
|
||||
void lua_pushobject (lua_Object o)
|
||||
{
|
||||
if (o == LUA_NOOBJECT)
|
||||
lua_error("attempt to push a NOOBJECT");
|
||||
lua_error("API error - attempt to push a NOOBJECT");
|
||||
*top = *Address(o);
|
||||
if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
|
||||
else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
int lua_tag (lua_Object o)
|
||||
int lua_tag (lua_Object lo)
|
||||
{
|
||||
return (o == LUA_NOOBJECT) ? LUA_T_NIL : luaI_tag(Address(o));
|
||||
if (lo == LUA_NOOBJECT) return LUA_T_NIL;
|
||||
else {
|
||||
TObject *o = Address(lo);
|
||||
lua_Type t = ttype(o);
|
||||
if (t == LUA_T_USERDATA)
|
||||
return o->value.ts->tag;
|
||||
else if (t == LUA_T_ARRAY)
|
||||
return o->value.a->htag;
|
||||
else return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1050,7 +996,7 @@ void luaI_gcIM (TObject *o)
|
||||
}
|
||||
|
||||
|
||||
static void call_arith (IMS event)
|
||||
static void call_binTM (IMS event, char *msg)
|
||||
{
|
||||
TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
|
||||
if (ttype(im) == LUA_T_NIL) {
|
||||
@@ -1058,29 +1004,19 @@ static void call_arith (IMS event)
|
||||
if (ttype(im) == LUA_T_NIL) {
|
||||
im = luaI_getim(0, event); /* try a 'global' i.m. */
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("unexpected type at conversion to number");
|
||||
lua_error(msg);
|
||||
}
|
||||
}
|
||||
lua_pushstring(luaI_eventname[event]);
|
||||
callIM(im, 3, 1);
|
||||
}
|
||||
|
||||
static void concim (TObject *o)
|
||||
|
||||
static void call_arith (IMS event)
|
||||
{
|
||||
TObject *im = luaI_getimbyObj(o, IM_CONCAT);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("unexpected type at conversion to string");
|
||||
callIM(im, 2, 1);
|
||||
call_binTM(event, "unexpected type at arithmetic operation");
|
||||
}
|
||||
|
||||
static void ordim (TObject *o, IMS event)
|
||||
{
|
||||
TObject *im = luaI_getimbyObj(o, event);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("unexpected type at comparison");
|
||||
lua_pushstring(luaI_eventname[event]);
|
||||
callIM(im, 3, 1);
|
||||
}
|
||||
|
||||
static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
||||
lua_Type ttype_great, IMS op)
|
||||
@@ -1090,16 +1026,12 @@ static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
||||
int result;
|
||||
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
|
||||
result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
|
||||
else if (tostring(l)) {
|
||||
ordim(l, op);
|
||||
return;
|
||||
}
|
||||
else if (tostring(r)) {
|
||||
ordim(r, op);
|
||||
return;
|
||||
}
|
||||
else
|
||||
else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
|
||||
result = strcmp(svalue(l), svalue(r));
|
||||
else {
|
||||
call_binTM(op, "unexpected type at comparison");
|
||||
return;
|
||||
}
|
||||
top--;
|
||||
nvalue(top-1) = 1;
|
||||
ttype(top-1) = (result < 0) ? ttype_less :
|
||||
@@ -1308,9 +1240,11 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
adjust_top(base);
|
||||
break;
|
||||
|
||||
case ADJUST:
|
||||
adjust_top(base + *(pc++));
|
||||
case ADJUST: {
|
||||
StkId newtop = base + *(pc++);
|
||||
adjust_top(newtop);
|
||||
break;
|
||||
}
|
||||
|
||||
case VARARGS:
|
||||
adjust_varargs(base + *(pc++));
|
||||
@@ -1414,10 +1348,8 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
case CONCOP: {
|
||||
TObject *l = top-2;
|
||||
TObject *r = top-1;
|
||||
if (tostring(l)) /* first argument is not a string */
|
||||
concim(l);
|
||||
else if (tostring(r)) /* second argument is not a string */
|
||||
concim(r);
|
||||
if (tostring(l) || tostring(r))
|
||||
call_binTM(IM_CONCAT, "unexpected type for concatenation");
|
||||
else {
|
||||
tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
|
||||
--top;
|
||||
@@ -1531,3 +1463,16 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if COMPAT2_5
|
||||
/*
|
||||
** API: set a function as a fallback
|
||||
*/
|
||||
lua_Object lua_setfallback (char *name, lua_CFunction fallback)
|
||||
{
|
||||
lua_pushstring(name);
|
||||
lua_pushcfunction(fallback);
|
||||
do_unprotectedrun(luaI_setfallback, 2, 1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
#endif
|
||||
|
||||
3
opcode.h
3
opcode.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: opcode.h,v 3.32 1997/04/04 22:24:51 roberto Exp roberto $
|
||||
** $Id: opcode.h,v 3.33 1997/04/11 21:34:53 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef opcode_h
|
||||
@@ -166,5 +166,6 @@ TObject *luaI_Address (lua_Object o);
|
||||
void luaI_pushobject (TObject *o);
|
||||
void luaI_gcIM (TObject *o);
|
||||
int luaI_dorun (TFunc *tf);
|
||||
int lua_domain (void);
|
||||
|
||||
#endif
|
||||
|
||||
52
strlib.c
52
strlib.c
@@ -3,7 +3,7 @@
|
||||
** String library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_strlib="$Id: strlib.c,v 1.40 1997/04/06 14:08:08 roberto Exp roberto $";
|
||||
char *rcs_strlib="$Id: strlib.c,v 1.45 1997/06/19 17:45:28 roberto Exp roberto $";
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
@@ -69,28 +69,6 @@ static void addstr (char *s)
|
||||
addnchar(s, strlen(s));
|
||||
}
|
||||
|
||||
/*
|
||||
** Interface to strtok
|
||||
*/
|
||||
static void str_tok (void)
|
||||
{
|
||||
char *s1 = luaL_check_string(1);
|
||||
char *del = luaL_check_string(2);
|
||||
lua_Object t = lua_createtable();
|
||||
int i = 1;
|
||||
/* As strtok changes s1, and s1 is "constant", make a copy of it */
|
||||
s1 = strcpy(strbuffer(strlen(s1+1)), s1);
|
||||
while ((s1 = strtok(s1, del)) != NULL) {
|
||||
lua_pushobject(t);
|
||||
lua_pushnumber(i++);
|
||||
lua_pushstring(s1);
|
||||
lua_settable();
|
||||
s1 = NULL; /* prepare for next strtok */
|
||||
}
|
||||
lua_pushobject(t);
|
||||
lua_pushnumber(i-1); /* total number of tokens */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the string length
|
||||
@@ -106,14 +84,17 @@ static void str_len (void)
|
||||
static void str_sub (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
long l = strlen(s);
|
||||
long start = (long)luaL_check_number(2);
|
||||
long end = (long)luaL_opt_number(3, strlen(s));
|
||||
if (1 <= start && start <= end && end <= strlen(s)) {
|
||||
long end = (long)luaL_opt_number(3, -1);
|
||||
if (start < 0) start = l+start+1;
|
||||
if (end < 0) end = l+end+1;
|
||||
if (1 <= start && start <= end && end <= l) {
|
||||
luaI_emptybuff();
|
||||
addnchar(s+start-1, end-start+1);
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
else lua_pushliteral("");
|
||||
else lua_pushstring("");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -303,7 +284,7 @@ static char *match (char *s, char *p, int level)
|
||||
return res;
|
||||
}
|
||||
case ESC:
|
||||
if (isdigit((unsigned char)*(p+1))) { /* capture */
|
||||
if (isdigit((unsigned char)(*(p+1)))) { /* capture */
|
||||
int l = check_cap(*(p+1), level);
|
||||
if (strncmp(capture[l].init, s, capture[l].len) == 0) {
|
||||
/* return match(p+2, s+capture[l].len, level); */
|
||||
@@ -391,7 +372,7 @@ static void str_find (void)
|
||||
}
|
||||
}
|
||||
|
||||
static void add_s (lua_Object newp)
|
||||
static void add_s (lua_Object newp, lua_Object table, int n)
|
||||
{
|
||||
if (lua_isstring(newp)) {
|
||||
char *news = lua_getstring(newp);
|
||||
@@ -407,15 +388,22 @@ static void add_s (lua_Object newp)
|
||||
else if (lua_isfunction(newp)) {
|
||||
lua_Object res;
|
||||
struct lbuff oldbuff;
|
||||
int status;
|
||||
lua_beginblock();
|
||||
if (lua_istable(table)) {
|
||||
lua_pushobject(table);
|
||||
lua_pushnumber(n);
|
||||
}
|
||||
push_captures();
|
||||
/* function may use lbuffer, so save it and create a new one */
|
||||
oldbuff = lbuffer;
|
||||
lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0;
|
||||
lua_callfunction(newp);
|
||||
status = lua_callfunction(newp);
|
||||
/* restore old buffer */
|
||||
free(lbuffer.b);
|
||||
lbuffer = oldbuff;
|
||||
if (status != 0)
|
||||
lua_error(NULL);
|
||||
res = lua_getresult(1);
|
||||
addstr(lua_isstring(res) ? lua_getstring(res) : "");
|
||||
lua_endblock();
|
||||
@@ -428,15 +416,16 @@ static void str_gsub (void)
|
||||
char *src = luaL_check_string(1);
|
||||
char *p = luaL_check_string(2);
|
||||
lua_Object newp = lua_getparam(3);
|
||||
int max_s = (int)luaL_opt_number(4, strlen(src)+1);
|
||||
lua_Object table = lua_getparam(4);
|
||||
int max_s = (int)luaL_opt_number(lua_istable(table)?5:4, strlen(src)+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
luaI_emptybuff();
|
||||
while (n < max_s) {
|
||||
char *e = match(src, p, 0);
|
||||
if (e) {
|
||||
add_s(newp);
|
||||
n++;
|
||||
add_s(newp, table, n);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
@@ -524,7 +513,6 @@ static void str_format (void)
|
||||
|
||||
|
||||
static struct luaL_reg strlib[] = {
|
||||
{"strtok", str_tok},
|
||||
{"strlen", str_len},
|
||||
{"strsub", str_sub},
|
||||
{"strset", str_set},
|
||||
|
||||
64
table.c
64
table.c
@@ -3,10 +3,11 @@
|
||||
** Module to control static tables
|
||||
*/
|
||||
|
||||
char *rcs_table="$Id: table.c,v 2.67 1997/04/06 14:08:08 roberto Exp roberto $";
|
||||
char *rcs_table="$Id: table.c,v 2.71 1997/06/09 17:28:14 roberto Exp roberto $";
|
||||
|
||||
#include "luamem.h"
|
||||
#include "auxlib.h"
|
||||
#include "func.h"
|
||||
#include "opcode.h"
|
||||
#include "tree.h"
|
||||
#include "hash.h"
|
||||
@@ -28,7 +29,7 @@ Word lua_nconstant = 0;
|
||||
static Long lua_maxconstant = 0;
|
||||
|
||||
|
||||
#define GARBAGE_BLOCK 50
|
||||
#define GARBAGE_BLOCK 100
|
||||
|
||||
|
||||
void luaI_initsymbol (void)
|
||||
@@ -58,17 +59,17 @@ void luaI_initconstant (void)
|
||||
*/
|
||||
Word luaI_findsymbol (TaggedString *t)
|
||||
{
|
||||
if (t->varindex == NOT_USED)
|
||||
if (t->u.s.varindex == NOT_USED)
|
||||
{
|
||||
if (lua_ntable == lua_maxsymbol)
|
||||
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
|
||||
symbolEM, MAX_WORD);
|
||||
t->varindex = lua_ntable;
|
||||
t->u.s.varindex = lua_ntable;
|
||||
lua_table[lua_ntable].varname = t;
|
||||
s_ttype(lua_ntable) = LUA_T_NIL;
|
||||
lua_ntable++;
|
||||
}
|
||||
return t->varindex;
|
||||
return t->u.s.varindex;
|
||||
}
|
||||
|
||||
|
||||
@@ -84,16 +85,16 @@ Word luaI_findsymbolbyname (char *name)
|
||||
*/
|
||||
Word luaI_findconstant (TaggedString *t)
|
||||
{
|
||||
if (t->constindex == NOT_USED)
|
||||
if (t->u.s.constindex == NOT_USED)
|
||||
{
|
||||
if (lua_nconstant == lua_maxconstant)
|
||||
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
|
||||
constantEM, MAX_WORD);
|
||||
t->constindex = lua_nconstant;
|
||||
t->u.s.constindex = lua_nconstant;
|
||||
lua_constant[lua_nconstant] = t;
|
||||
lua_nconstant++;
|
||||
}
|
||||
return t->constindex;
|
||||
return t->u.s.constindex;
|
||||
}
|
||||
|
||||
|
||||
@@ -153,7 +154,7 @@ int luaI_ismarked (TObject *o)
|
||||
{
|
||||
switch (o->ttype)
|
||||
{
|
||||
case LUA_T_STRING:
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
return o->value.ts->marked;
|
||||
case LUA_T_FUNCTION:
|
||||
return o->value.tf->marked;
|
||||
@@ -176,32 +177,45 @@ static void call_nilIM (void)
|
||||
** Garbage collection.
|
||||
** Delete all unused strings and arrays.
|
||||
*/
|
||||
Long luaI_collectgarbage (void)
|
||||
static long gc_block = GARBAGE_BLOCK;
|
||||
static long gc_nentity = 0; /* total of strings, arrays, etc */
|
||||
|
||||
static void markall (void)
|
||||
{
|
||||
Long recovered = 0;
|
||||
lua_travstack(lua_markobject); /* mark stack objects */
|
||||
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
||||
luaI_travlock(lua_markobject); /* mark locked objects */
|
||||
luaI_travfallbacks(lua_markobject); /* mark fallbacks */
|
||||
luaI_hashcallIM();
|
||||
luaI_strcallIM();
|
||||
call_nilIM();
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (long limit)
|
||||
{
|
||||
long recovered = 0;
|
||||
Hash *freetable;
|
||||
TaggedString *freestr;
|
||||
TFunc *freefunc;
|
||||
markall();
|
||||
luaI_invalidaterefs();
|
||||
recovered += lua_strcollector();
|
||||
recovered += lua_hashcollector();
|
||||
recovered += luaI_funccollector();
|
||||
freetable = luaI_hashcollector(&recovered);
|
||||
freestr = luaI_strcollector(&recovered);
|
||||
freefunc = luaI_funccollector(&recovered);
|
||||
gc_nentity -= recovered;
|
||||
gc_block = (limit == 0) ? 2*(gc_block-recovered) : gc_nentity+limit;
|
||||
luaI_hashcallIM(freetable);
|
||||
luaI_strcallIM(freestr);
|
||||
call_nilIM();
|
||||
luaI_hashfree(freetable);
|
||||
luaI_strfree(freestr);
|
||||
luaI_funcfree(freefunc);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
|
||||
void lua_pack (void)
|
||||
{
|
||||
static unsigned long block = GARBAGE_BLOCK;
|
||||
static unsigned long nentity = 0; /* total of strings, arrays, etc */
|
||||
unsigned long recovered = 0;
|
||||
if (nentity++ < block) return;
|
||||
recovered = luaI_collectgarbage();
|
||||
block = 2*(block-recovered);
|
||||
nentity -= recovered;
|
||||
if (++gc_nentity >= gc_block)
|
||||
lua_collectgarbage(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -244,7 +258,7 @@ char *lua_getobjname (lua_Object o, char **name)
|
||||
{ /* try to find a name for given function */
|
||||
functofind = luaI_Address(o);
|
||||
if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
|
||||
return "fallback";
|
||||
return "tag-method";
|
||||
else if ((*name = lua_travsymbol(checkfunc)) != NULL)
|
||||
return "global";
|
||||
else return "";
|
||||
|
||||
3
table.h
3
table.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: table.h,v 2.23 1997/03/31 14:02:58 roberto Exp roberto $
|
||||
** $Id: table.h,v 2.24 1997/04/07 14:48:53 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef table_h
|
||||
@@ -33,7 +33,6 @@ void luaI_nextvar (void);
|
||||
TaggedString *luaI_createfixedstring (char *str);
|
||||
int lua_markobject (TObject *o);
|
||||
int luaI_ismarked (TObject *o);
|
||||
Long luaI_collectgarbage (void);
|
||||
void lua_pack (void);
|
||||
|
||||
|
||||
|
||||
113
tree.c
113
tree.c
@@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_tree="$Id: tree.c,v 1.23 1997/03/31 14:02:58 roberto Exp roberto $";
|
||||
char *rcs_tree="$Id: tree.c,v 1.27 1997/06/09 17:28:14 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include <string.h>
|
||||
@@ -29,20 +29,39 @@ static int initialized = 0;
|
||||
|
||||
static stringtable string_root[NUM_HASHS];
|
||||
|
||||
static TaggedString EMPTY = {LUA_T_STRING, 0, NOT_USED, NOT_USED, 0, 2, {0}};
|
||||
static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
|
||||
0, 2, {0}};
|
||||
|
||||
|
||||
static unsigned long hash (char *buff, long size)
|
||||
static unsigned long hash (char *s, int tag)
|
||||
{
|
||||
unsigned long h = 0;
|
||||
while (size--)
|
||||
h = ((h<<5)-h)^(unsigned char)*(buff++);
|
||||
unsigned long h;
|
||||
if (tag != LUA_T_STRING)
|
||||
h = (unsigned long)s;
|
||||
else {
|
||||
h = 0;
|
||||
while (*s)
|
||||
h = ((h<<5)-h)^(unsigned char)*(s++);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
static void luaI_inittree (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
string_root[i].size = 0;
|
||||
string_root[i].nuse = 0;
|
||||
string_root[i].hash = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void initialize (void)
|
||||
{
|
||||
initialized = 1;
|
||||
luaI_inittree();
|
||||
luaI_addReserved();
|
||||
luaI_initsymbol();
|
||||
luaI_initconstant();
|
||||
@@ -60,8 +79,7 @@ static void grow (stringtable *tb)
|
||||
/* rehash */
|
||||
tb->nuse = 0;
|
||||
for (i=0; i<tb->size; i++)
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
||||
{
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
|
||||
int h = tb->hash[i]->hash%newsize;
|
||||
while (newhash[h])
|
||||
h = (h+1)%newsize;
|
||||
@@ -73,10 +91,30 @@ static void grow (stringtable *tb)
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
|
||||
|
||||
static TaggedString *newone(char *buff, int tag, unsigned long h)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(buff, size);
|
||||
if (tag == LUA_T_STRING) {
|
||||
ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
|
||||
strcpy(ts->str, buff);
|
||||
ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
|
||||
ts->tag = LUA_T_STRING;
|
||||
}
|
||||
else {
|
||||
ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
|
||||
ts->u.v = buff;
|
||||
ts->tag = tag == LUA_ANYTAG ? 0 : tag;
|
||||
}
|
||||
ts->marked = 0;
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *buff, int tag, stringtable *tb)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(buff, tag);
|
||||
int i;
|
||||
int j = -1;
|
||||
if ((Long)tb->nuse*3 >= (Long)tb->size*2)
|
||||
@@ -90,8 +128,9 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
|
||||
{
|
||||
if (ts == &EMPTY)
|
||||
j = i;
|
||||
else if (ts->size == size && ts->tag == tag &&
|
||||
memcmp(buff, ts->str, size) == 0)
|
||||
else if ((ts->tag == LUA_T_STRING) ?
|
||||
(tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
|
||||
((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
|
||||
return ts;
|
||||
i = (i+1)%tb->size;
|
||||
}
|
||||
@@ -101,53 +140,49 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
|
||||
i = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1);
|
||||
memcpy(ts->str, buff, size);
|
||||
ts->tag = tag;
|
||||
ts->size = size;
|
||||
ts->marked = 0;
|
||||
ts->hash = h;
|
||||
ts->varindex = ts->constindex = NOT_USED;
|
||||
ts = tb->hash[i] = newone(buff, tag, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
TaggedString *luaI_createuserdata (char *buff, long size, int tag)
|
||||
TaggedString *luaI_createudata (void *udata, int tag)
|
||||
{
|
||||
return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]);
|
||||
return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
|
||||
}
|
||||
|
||||
TaggedString *lua_createstring (char *str)
|
||||
{
|
||||
return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING);
|
||||
return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||
}
|
||||
|
||||
|
||||
void luaI_strcallIM (void)
|
||||
void luaI_strcallIM (TaggedString *l)
|
||||
{
|
||||
int i;
|
||||
TObject o;
|
||||
ttype(&o) = LUA_T_USERDATA;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t != NULL && t->tag != LUA_T_STRING && t->marked == 0) {
|
||||
tsvalue(&o) = t;
|
||||
luaI_gcIM(&o);
|
||||
}
|
||||
}
|
||||
for (; l; l=l->next) {
|
||||
tsvalue(&o) = l;
|
||||
luaI_gcIM(&o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaI_strfree (TaggedString *l)
|
||||
{
|
||||
while (l) {
|
||||
TaggedString *next = l->next;
|
||||
luaI_free(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection function.
|
||||
** This function traverse the string list freeing unindexed strings
|
||||
*/
|
||||
Long lua_strcollector (void)
|
||||
TaggedString *luaI_strcollector (long *acum)
|
||||
{
|
||||
Long counter = 0;
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++)
|
||||
{
|
||||
@@ -162,13 +197,15 @@ Long lua_strcollector (void)
|
||||
t->marked = 0;
|
||||
else
|
||||
{
|
||||
luaI_free(t);
|
||||
t->next = frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
*acum += counter;
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
22
tree.h
22
tree.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
** tree.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: tree.h,v 1.15 1997/02/11 11:35:05 roberto Exp roberto $
|
||||
** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef tree_h
|
||||
@@ -15,18 +15,24 @@
|
||||
typedef struct TaggedString
|
||||
{
|
||||
int tag; /* if != LUA_T_STRING, this is a userdata */
|
||||
long size;
|
||||
Word varindex; /* != NOT_USED if this is a symbol */
|
||||
Word constindex; /* != NOT_USED if this is a constant */
|
||||
struct TaggedString *next;
|
||||
union {
|
||||
struct {
|
||||
Word varindex; /* != NOT_USED if this is a symbol */
|
||||
Word constindex; /* != NOT_USED if this is a constant */
|
||||
} s;
|
||||
void *v; /* if this is a userdata, here is its value */
|
||||
} u;
|
||||
unsigned long hash; /* 0 if not initialized */
|
||||
int marked; /* for garbage collection; never collect (nor change) if > 1 */
|
||||
char str[1]; /* \0 byte already reserved; MAY BE NOT 0 TERMINATED!! */
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
|
||||
TaggedString *lua_createstring (char *str);
|
||||
TaggedString *luaI_createuserdata (char *buff, long size, int tag);
|
||||
Long lua_strcollector (void);
|
||||
void luaI_strcallIM (void);
|
||||
TaggedString *luaI_createudata (void *udata, int tag);
|
||||
TaggedString *luaI_strcollector (long *cont);
|
||||
void luaI_strfree (TaggedString *l);
|
||||
void luaI_strcallIM (TaggedString *l);
|
||||
|
||||
#endif
|
||||
|
||||
105
undump.c
105
undump.c
@@ -3,7 +3,7 @@
|
||||
** load bytecodes from files
|
||||
*/
|
||||
|
||||
char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp lhf $";
|
||||
char* rcs_undump="$Id: undump.c,v 1.23 1997/06/16 16:50:22 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -12,6 +12,7 @@ char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp lhf $";
|
||||
#include "luamem.h"
|
||||
#include "table.h"
|
||||
#include "undump.h"
|
||||
#include "zio.h"
|
||||
|
||||
static int swapword=0;
|
||||
static int swapfloat=0;
|
||||
@@ -147,10 +148,10 @@ static void Unthread(Byte* code, int i, int v)
|
||||
}
|
||||
}
|
||||
|
||||
static int LoadWord(FILE* D)
|
||||
static int LoadWord(ZIO* Z)
|
||||
{
|
||||
Word w;
|
||||
fread(&w,sizeof(w),1,D);
|
||||
zread(Z,&w,sizeof(w));
|
||||
if (swapword)
|
||||
{
|
||||
Byte* p=(Byte*)&w;
|
||||
@@ -160,101 +161,101 @@ static int LoadWord(FILE* D)
|
||||
return w;
|
||||
}
|
||||
|
||||
static int LoadSize(FILE* D)
|
||||
static int LoadSize(ZIO* Z)
|
||||
{
|
||||
Word hi=LoadWord(D);
|
||||
Word lo=LoadWord(D);
|
||||
Word hi=LoadWord(Z);
|
||||
Word lo=LoadWord(Z);
|
||||
int s=(hi<<16)|lo;
|
||||
if ((Word)s != s) lua_error("code too long");
|
||||
return s;
|
||||
}
|
||||
|
||||
static void* LoadBlock(int size, FILE* D)
|
||||
static void* LoadBlock(int size, ZIO* Z)
|
||||
{
|
||||
void* b=luaI_malloc(size);
|
||||
fread(b,size,1,D);
|
||||
zread(Z,b,size);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadString(FILE* D)
|
||||
static char* LoadString(ZIO* Z)
|
||||
{
|
||||
int size=LoadWord(D);
|
||||
int size=LoadWord(Z);
|
||||
char *b=luaI_buffer(size);
|
||||
fread(b,size,1,D);
|
||||
zread(Z,b,size);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadNewString(FILE* D)
|
||||
static char* LoadNewString(ZIO* Z)
|
||||
{
|
||||
return LoadBlock(LoadWord(D),D);
|
||||
return LoadBlock(LoadWord(Z),Z);
|
||||
}
|
||||
|
||||
static void LoadFunction(FILE* D)
|
||||
static void LoadFunction(ZIO* Z)
|
||||
{
|
||||
TFunc* tf=new(TFunc);
|
||||
tf->next=NULL;
|
||||
tf->locvars=NULL;
|
||||
tf->size=LoadSize(D);
|
||||
tf->lineDefined=LoadWord(D);
|
||||
tf->size=LoadSize(Z);
|
||||
tf->lineDefined=LoadWord(Z);
|
||||
if (IsMain(tf)) /* new main */
|
||||
{
|
||||
tf->fileName=LoadNewString(D);
|
||||
tf->fileName=LoadNewString(Z);
|
||||
Main=lastF=tf;
|
||||
}
|
||||
else /* fix PUSHFUNCTION */
|
||||
{
|
||||
tf->marked=LoadWord(D);
|
||||
tf->marked=LoadWord(Z);
|
||||
tf->fileName=Main->fileName;
|
||||
memcpy(Main->code+tf->marked,&tf,sizeof(tf));
|
||||
lastF=lastF->next=tf;
|
||||
}
|
||||
tf->code=LoadBlock(tf->size,D);
|
||||
tf->code=LoadBlock(tf->size,Z);
|
||||
if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
|
||||
while (1) /* unthread */
|
||||
{
|
||||
int c=getc(D);
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_VAR) /* global var */
|
||||
{
|
||||
int i=LoadWord(D);
|
||||
char* s=LoadString(D);
|
||||
int i=LoadWord(Z);
|
||||
char* s=LoadString(Z);
|
||||
int v=luaI_findsymbolbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else if (c==ID_STR) /* constant string */
|
||||
{
|
||||
int i=LoadWord(D);
|
||||
char* s=LoadString(D);
|
||||
int i=LoadWord(Z);
|
||||
char* s=LoadString(Z);
|
||||
int v=luaI_findconstantbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else
|
||||
{
|
||||
ungetc(c,D);
|
||||
zungetc(Z);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadSignature(FILE* D)
|
||||
static void LoadSignature(ZIO* Z)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && getc(D)==*s)
|
||||
while (*s!=0 && zgetc(Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) lua_error("cannot load binary file: bad signature");
|
||||
}
|
||||
|
||||
static void LoadHeader(FILE* D)
|
||||
static void LoadHeader(ZIO* Z)
|
||||
{
|
||||
Word w,tw=TEST_WORD;
|
||||
float f,tf=TEST_FLOAT;
|
||||
int version;
|
||||
LoadSignature(D);
|
||||
version=getc(D);
|
||||
LoadSignature(Z);
|
||||
version=zgetc(Z);
|
||||
if (version>0x23) /* after 2.5 */
|
||||
{
|
||||
int oldsizeofW=getc(D);
|
||||
int oldsizeofF=getc(D);
|
||||
int oldsizeofP=getc(D);
|
||||
int oldsizeofW=zgetc(Z);
|
||||
int oldsizeofF=zgetc(Z);
|
||||
int oldsizeofP=zgetc(Z);
|
||||
if (oldsizeofW!=2)
|
||||
luaL_verror(
|
||||
"cannot load binary file created on machine with sizeof(Word)=%d; "
|
||||
@@ -266,14 +267,14 @@ static void LoadHeader(FILE* D)
|
||||
if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
|
||||
luaL_verror(
|
||||
"cannot load binary file created on machine with sizeof(TFunc*)=%d; "
|
||||
"expected %d",oldsizeofP,sizeof(TFunc*));
|
||||
"expected %d",oldsizeofP,(int)sizeof(TFunc*));
|
||||
}
|
||||
fread(&w,sizeof(w),1,D); /* test word */
|
||||
zread(Z,&w,sizeof(w)); /* test word */
|
||||
if (w!=tw)
|
||||
{
|
||||
swapword=1;
|
||||
}
|
||||
fread(&f,sizeof(f),1,D); /* test float */
|
||||
zread(Z,&f,sizeof(f)); /* test float */
|
||||
if (f!=tf)
|
||||
{
|
||||
Byte* p=(Byte*)&f;
|
||||
@@ -286,13 +287,13 @@ static void LoadHeader(FILE* D)
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadChunk(FILE* D)
|
||||
static void LoadChunk(ZIO* Z)
|
||||
{
|
||||
LoadHeader(D);
|
||||
LoadHeader(Z);
|
||||
while (1)
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,30 +301,26 @@ static void LoadChunk(FILE* D)
|
||||
** load one chunk from a file.
|
||||
** return list of functions found, headed by main, or NULL at EOF.
|
||||
*/
|
||||
TFunc* luaI_undump1(FILE* D)
|
||||
TFunc* luaI_undump1(ZIO* Z)
|
||||
{
|
||||
while (1)
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_CHUNK)
|
||||
{
|
||||
LoadChunk(D);
|
||||
return Main;
|
||||
}
|
||||
else if (c==EOF)
|
||||
return NULL;
|
||||
else
|
||||
lua_error("not a lua binary file");
|
||||
LoadChunk(Z);
|
||||
return Main;
|
||||
}
|
||||
else if (c!=EOZ)
|
||||
lua_error("not a lua binary file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
** load and run all chunks in a file
|
||||
*/
|
||||
int luaI_undump(FILE* D)
|
||||
int luaI_undump(ZIO* Z)
|
||||
{
|
||||
TFunc* m;
|
||||
while ((m=luaI_undump1(D)))
|
||||
while ((m=luaI_undump1(Z)))
|
||||
{
|
||||
int status=luaI_dorun(m);
|
||||
luaI_freefunc(m);
|
||||
|
||||
13
undump.h
13
undump.h
@@ -1,10 +1,14 @@
|
||||
/*
|
||||
** undump.h
|
||||
** definitions for lua decompiler
|
||||
** $Id: undump.h,v 1.3 1996/11/14 11:44:34 lhf Exp lhf $
|
||||
** $Id: undump.h,v 1.5 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef undump_h
|
||||
#define undump_h
|
||||
|
||||
#include "func.h"
|
||||
#include "zio.h"
|
||||
|
||||
#define IsMain(f) (f->lineDefined==0)
|
||||
|
||||
@@ -19,5 +23,8 @@
|
||||
#define TEST_WORD 0x1234 /* a word for testing byte ordering */
|
||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||
|
||||
TFunc* luaI_undump1(FILE* D); /* load one chunk */
|
||||
int luaI_undump(FILE* D); /* load all chunks */
|
||||
|
||||
TFunc* luaI_undump1(ZIO* Z);
|
||||
int luaI_undump(ZIO* Z); /* load all chunks */
|
||||
|
||||
#endif
|
||||
|
||||
79
zio.c
Normal file
79
zio.c
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* zio.c
|
||||
* a generic input stream interface
|
||||
* $Id: zio.c,v 1.1 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "zio.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
static int zmfilbuf(ZIO* z)
|
||||
{
|
||||
return EOZ;
|
||||
}
|
||||
|
||||
ZIO* zmopen(ZIO* z, char* b, int size)
|
||||
{
|
||||
if (b==NULL) return NULL;
|
||||
z->n=size;
|
||||
z->p= (unsigned char *)b;
|
||||
z->filbuf=zmfilbuf;
|
||||
z->u=NULL;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ strings --- */
|
||||
|
||||
ZIO* zsopen(ZIO* z, char* s)
|
||||
{
|
||||
if (s==NULL) return NULL;
|
||||
return zmopen(z,s,strlen(s));
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- FILEs --- */
|
||||
|
||||
static int zffilbuf(ZIO* z)
|
||||
{
|
||||
int n=fread(z->buffer,1,ZBSIZE,z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n=n-1;
|
||||
z->p=z->buffer;
|
||||
return *(z->p++);
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen(ZIO* z, FILE* f)
|
||||
{
|
||||
if (f==NULL) return NULL;
|
||||
z->n=0;
|
||||
z->p=z->buffer;
|
||||
z->filbuf=zffilbuf;
|
||||
z->u=f;
|
||||
return z;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
int zread(ZIO *z, void *b, int n)
|
||||
{
|
||||
while (n) {
|
||||
int m;
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
return n; /* retorna quantos faltaram ler */
|
||||
zungetc(z); /* poe o resultado de filbuf no buffer */
|
||||
}
|
||||
m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
z->p += m;
|
||||
b = (char *)b + m;
|
||||
n -= m;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
48
zio.h
Normal file
48
zio.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* zio.h
|
||||
* a generic input stream interface
|
||||
* $Id: zio.h,v 1.4 1997/06/19 18:55:28 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef zio_h
|
||||
#define zio_h
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
/* For Lua only */
|
||||
#define zFopen luaZ_Fopen
|
||||
#define zsopen luaZ_sopen
|
||||
#define zmopen luaZ_mopen
|
||||
#define zread luaZ_read
|
||||
|
||||
#define EOZ (-1) /* end of stream */
|
||||
|
||||
typedef struct zio ZIO;
|
||||
|
||||
ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */
|
||||
ZIO* zsopen(ZIO* z, char* s); /* string */
|
||||
ZIO* zmopen(ZIO* z, char* b, int size); /* memory */
|
||||
|
||||
int zread(ZIO* z, void* b, int n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
|
||||
struct zio {
|
||||
int n; /* bytes still unread */
|
||||
unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user