1
0
mirror of https://github.com/lua/lua synced 2026-06-08 15:34:49 +00:00

Compare commits

...

63 Commits

Author SHA1 Message Date
Roberto Ierusalimschy ef3c45d523 for security reasons, one cannot change the tag of a udata
from Lua.
1997-06-27 19:38:49 -03:00
Roberto Ierusalimschy 20a48a818f a file handle must be a userdata. 1997-06-27 19:38:49 -03:00
Roberto Ierusalimschy c9e6ed1d9f small corrections. 1997-06-27 15:39:34 -03:00
Roberto Ierusalimschy 56fdab67d8 believe it or not, this makes a difference... 1997-06-26 18:40:57 -03:00
Roberto Ierusalimschy 3a98c8670d new name for _VERSION (standard of _UPPERCASES for pre-defined
global variable)
1997-06-26 17:47:43 -03:00
Roberto Ierusalimschy da585783e3 new method to handle current files, with global variables
_INPUT and _OUTPUT.
1997-06-26 17:39:10 -03:00
Roberto Ierusalimschy e81f184164 new #define LUA_COMPAT2_5 to select "compatibility mode". 1997-06-23 15:27:53 -03:00
Roberto Ierusalimschy 3e43275308 incompatibilities with 2.5 + restrictions on tag methods 1997-06-20 16:28:16 -03:00
Roberto Ierusalimschy f97307b548 zio should not care about how a user creates a FILE (pipe, socket, popen,
etc).
1997-06-20 16:25:54 -03:00
Roberto Ierusalimschy 6402bfb1f8 better error message for dostring (2nd argument) 1997-06-20 16:19:09 -03:00
Roberto Ierusalimschy 27b6bbdcf3 "testC" inactive (active only for tests) 1997-06-19 15:55:40 -03:00
Roberto Ierusalimschy 5a198de3c1 new prefix luaZ 1997-06-19 15:55:28 -03:00
Roberto Ierusalimschy c5d7ba7347 dostring has an optional error method argument 1997-06-19 15:49:40 -03:00
Roberto Ierusalimschy f84c2ebc4a dostring has an optional error method argument;
counter for gsub only when there is a table (to keep full compatibility)
1997-06-19 15:49:40 -03:00
Roberto Ierusalimschy c8897f2b08 counter for gsub only when there is a table (to keep full compatibility) 1997-06-19 15:49:40 -03:00
Roberto Ierusalimschy 6bb2cac3db setparsedfile is extern; luaz_... changed to luaZ_... 1997-06-19 15:04:34 -03:00
Roberto Ierusalimschy 88f020b626 new interface to "lua_seterrormethod" and "lua_settagmethod", to
allow the use of Lua functions too.
1997-06-19 15:03:04 -03:00
Roberto Ierusalimschy a38f093f05 luaI_initTFunc initializes fileName. 1997-06-19 14:46:12 -03:00
Roberto Ierusalimschy b6adfdd01b gsub aborts if error occurs. 1997-06-19 14:45:28 -03:00
Roberto Ierusalimschy b90b4bbd3a macros don't need prototypes. 1997-06-18 18:39:56 -03:00
Roberto Ierusalimschy f6d95140ae using old version functions. 1997-06-18 18:20:45 -03:00
Roberto Ierusalimschy eca17b24ea #if wraps compatibility issue 1997-06-18 18:20:45 -03:00
Roberto Ierusalimschy 6b61582e47 do not use $...$ to italic. 1997-06-18 18:11:53 -03:00
Roberto Ierusalimschy 60bdc370d9 new functions lua_doFILE and lua_dobuffer to help users... 1997-06-18 17:35:49 -03:00
Roberto Ierusalimschy b9e31f73ee exported functions follow "lua" prefix rule. 1997-06-18 17:30:52 -03:00
Roberto Ierusalimschy ea98620d98 new arguments for gsub 1997-06-18 17:15:47 -03:00
Roberto Ierusalimschy 6b78040840 lhf corrections + new gsub 1997-06-18 17:14:52 -03:00
Roberto Ierusalimschy e931c7c0f6 error message always shows file names. 1997-06-18 14:33:30 -03:00
Roberto Ierusalimschy 70160320b1 first version for Lua 3.0 (with tag methods, etc) 1997-06-17 15:45:16 -03:00
Roberto Ierusalimschy bfbf56f15a extra parameter for "call" is "pack" or "plain". 1997-06-17 15:44:31 -03:00
Roberto Ierusalimschy 732ef82c72 "luaI_undump1" cannot be static; luac uses it. 1997-06-17 15:19:17 -03:00
Roberto Ierusalimschy 2a1da64872 "lua_getobjname" must return "tag-method" and not "fallback". 1997-06-17 15:09:31 -03:00
Roberto Ierusalimschy 3c5d71cf77 "nil" may have fallbacks too. 1997-06-17 14:27:07 -03:00
Roberto Ierusalimschy 1848bcc15b "strsub" accepts negative indices (count from the end of the string). 1997-06-16 17:29:59 -03:00
Roberto Ierusalimschy 4d2de484f6 "lua_seterrormethod" should return the previous handler. 1997-06-16 16:48:18 -03:00
Roberto Ierusalimschy 312f78d925 default error method is not nil (nil does nothing). 1997-06-16 15:43:19 -03:00
Roberto Ierusalimschy e4b69d6c9c "order" and "concat" operations choose tag methods the same way that
"arith" operations do (first the first parameter, etc).
1997-06-16 15:42:32 -03:00
Roberto Ierusalimschy c9a2dfeb2c using "zio" for parsing Lua code. 1997-06-16 13:50:22 -03:00
Roberto Ierusalimschy 9fe5be3acf library for uniform buffered input. 1997-06-16 13:50:22 -03:00
Roberto Ierusalimschy 611680af08 functions "lua_settagmethod" and similars should be safe too. 1997-06-12 15:27:29 -03:00
Roberto Ierusalimschy cb1d8f0fa0 missing small dependency. 1997-06-11 15:57:00 -03:00
Roberto Ierusalimschy 33f4fef410 BUG: did not handle properly pragmas $endinput/$debug inside a $if. 1997-06-11 15:56:02 -03:00
Roberto Ierusalimschy e77534c08f small 'improvement' 1997-06-11 11:24:40 -03:00
Roberto Ierusalimschy a22b54bbb6 format more uniform, to facilitate filters .tex->.html 1997-06-09 15:16:33 -03:00
Roberto Ierusalimschy 414ebe8ce1 power method (^) acts only in numbers. 1997-06-09 14:30:10 -03:00
Roberto Ierusalimschy 33ace03b5a udata is just void * again, so there is no point for function "getbyte". 1997-06-09 14:29:45 -03:00
Roberto Ierusalimschy 085181a08a new function "testC", to help debug the API. 1997-06-09 14:29:16 -03:00
Roberto Ierusalimschy dd22ea4da5 new implementation for udata (again they are just void *);
new implementation for the API: most operations now do not disturb
structures lua2C and C2lua.
1997-06-09 14:28:14 -03:00
Roberto Ierusalimschy 5fdcfeb353 new lua_Objects are created below the C2lua stack, so most API functions
don't need to adjust stack.
1997-06-06 17:54:40 -03:00
Roberto Ierusalimschy d68d8287aa new lua_Objects are created below the C2lua stack, so most API functions
don't need to adjust stack. So lua_getref does not change the stack,
and lua_pushref can use it as a macro.
1997-06-06 17:54:40 -03:00
Roberto Ierusalimschy e1249970c2 new API function to force garbage collection. 1997-05-26 11:42:51 -03:00
Roberto Ierusalimschy 9d6f4e48a6 unused declaration. 1997-05-26 11:42:36 -03:00
Roberto Ierusalimschy b546a042e8 another (better?) implementation for "pushsubscript". 1997-05-26 11:23:55 -03:00
Roberto Ierusalimschy bd9e68cfcd new implementation of gc: "Pre-collect" garbage in temporary lists and
then call fallbacks.
1997-05-14 15:38:29 -03:00
Roberto Ierusalimschy 9747f3c87a double hashing + tables do not grow if there are empty slots 1997-05-08 17:43:30 -03:00
Roberto Ierusalimschy 12d9731a49 bug: string table must be initialized. 1997-05-05 17:21:23 -03:00
Roberto Ierusalimschy 4aade854bb correction of name (and "iternal" was wrong, any way). 1997-04-30 16:55:47 -03:00
Roberto Ierusalimschy 369dd65318 small optimization in the access to i.m. table. 1997-04-24 19:59:57 -03:00
Roberto Ierusalimschy 209602ac31 BUG: input file must be closed just after parser. 1997-04-15 14:32:47 -03:00
Roberto Ierusalimschy 6251d889ca missing definition of "lua_storesubscript", for compatibility 1997-04-15 13:52:20 -03:00
Roberto Ierusalimschy faec0b0824 new pragma "$endinput" 1997-04-14 16:08:09 -03:00
Roberto Ierusalimschy 64d93e7cf9 global variables (in $if) may have '_'. 1997-04-14 12:30:29 -03:00
Roberto Ierusalimschy d73c5bcfe7 function name is only needed *if* there is an error. 1997-04-14 12:30:03 -03:00
29 changed files with 2269 additions and 1513 deletions
+5 -5
View File
@@ -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 <stdio.h>
#include <stdarg.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) void luaL_arg_check(int cond, int numarg, char *extramsg)
{ {
char *funcname;
lua_getobjname(lua_stackedfunction(0), &funcname);
if (funcname == NULL)
funcname = "???";
if (!cond) { if (!cond) {
char *funcname;
lua_getobjname(lua_stackedfunction(0), &funcname);
if (funcname == NULL)
funcname = "???";
if (extramsg == NULL) if (extramsg == NULL)
luaL_verror("bad argument #%d to function `%s'", numarg, funcname); luaL_verror("bad argument #%d to function `%s'", numarg, funcname);
else else
+43 -36
View File
@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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 <stdio.h>
#include <string.h> #include <string.h>
@@ -111,24 +111,26 @@ static int luaI_checkevent (char *name, char *list[])
} }
static struct IM { struct IM *luaI_IMtable = NULL;
TObject int_method[IM_N];
} *luaI_IMtable = NULL;
static int IMtable_size = 0; static int IMtable_size = 0;
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */ 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 */ 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 */ {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 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, 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_CMARK */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */ {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, 0, 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_FUNCTION */
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */ {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, 0, 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 */ {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 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, 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) static int validevent (lua_Type t, int e)
@@ -141,7 +143,7 @@ static void init_entry (int tag)
{ {
int i; int i;
for (i=0; i<IM_N; 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) void luaI_initfallbacks (void)
@@ -188,38 +190,35 @@ void luaI_settag (int tag, TObject *o)
case LUA_T_ARRAY: case LUA_T_ARRAY:
o->value.a->htag = tag; o->value.a->htag = tag;
break; break;
case LUA_T_USERDATA:
o->value.ts->tag = tag;
break;
default: default:
luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]); 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); lua_Type t = ttype(o);
if (t == LUA_T_USERDATA) if (t == LUA_T_USERDATA) {
return o->value.ts->tag; int tag = o->value.ts->tag;
return (tag >= 0) ? LUA_T_USERDATA : tag;
}
else if (t == LUA_T_ARRAY) else if (t == LUA_T_ARRAY)
return o->value.a->htag; return o->value.a->htag;
else return t; 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) void luaI_gettagmethod (void)
{ {
int t = (int)luaL_check_number(1); int t = (int)luaL_check_number(1);
int e = luaI_checkevent(luaL_check_string(2), luaI_eventname); int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
checktag(t); checktag(t);
if (validevent(t, e)) 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); luaI_eventname[e], t);
luaL_arg_check(lua_isnil(func) || lua_isfunction(func), luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
3, "function expected"); 3, "function expected");
luaI_pushobject(&luaI_IMtable[-t].int_method[e]); luaI_pushobject(luaI_getim(t,e));
luaI_IMtable[-t].int_method[e] = *luaI_Address(func); *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) TObject *luaI_geterrorim (void)
@@ -264,7 +270,7 @@ char *luaI_travfallbacks (int (*fn)(TObject *))
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
int t; int t;
for (t=0; t>=last_tag; 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 luaI_eventname[e];
} }
return NULL; return NULL;
@@ -275,7 +281,7 @@ char *luaI_travfallbacks (int (*fn)(TObject *))
* =================================================================== * ===================================================================
* compatibility with old fallback system * compatibility with old fallback system
*/ */
#if LUA_COMPAT2_5
static void errorFB (void) static void errorFB (void)
{ {
@@ -301,9 +307,10 @@ static void fillvalids (IMS e, TObject *func)
int t; int t;
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++) for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
if (validevent(t, e)) if (validevent(t, e))
luaI_IMtable[-t].int_method[e] = *func; *luaI_getim(t, e) = *func;
} }
void luaI_setfallback (void) void luaI_setfallback (void)
{ {
static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL}; static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
@@ -320,13 +327,13 @@ void luaI_setfallback (void)
replace = errorFB; replace = errorFB;
break; break;
case 1: /* old getglobal fallback */ case 1: /* old getglobal fallback */
oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL]; oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func); *luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
replace = nilFB; replace = nilFB;
break; break;
case 2: { /* old arith fallback */ case 2: { /* old arith fallback */
int i; 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 */ for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func)); fillvalids(i, luaI_Address(func));
replace = typeFB; replace = typeFB;
@@ -334,7 +341,7 @@ void luaI_setfallback (void)
} }
case 3: { /* old order fallback */ case 3: { /* old order fallback */
int i; 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 */ for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func)); fillvalids(i, luaI_Address(func));
replace = typeFB; replace = typeFB;
@@ -343,7 +350,7 @@ void luaI_setfallback (void)
default: { default: {
int e; int e;
if ((e = luaI_findstring(name, luaI_eventname)) >= 0) { 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)); fillvalids(e, luaI_Address(func));
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
} }
@@ -358,4 +365,4 @@ void luaI_setfallback (void)
else else
lua_pushcfunction(replace); lua_pushcfunction(replace);
} }
#endif
+9 -4
View File
@@ -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 #ifndef fallback_h
@@ -35,8 +35,15 @@ typedef enum {
#define IM_N 18 #define IM_N 18
extern struct IM {
TObject int_method[IM_N];
} *luaI_IMtable;
extern char *luaI_eventname[]; 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); void luaI_setfallback (void);
int luaI_ref (TObject *object, int lock); 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_settag (int tag, TObject *o);
void luaI_realtag (int tag); 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); TObject *luaI_geterrorim (void);
int luaI_tag (TObject *o); int luaI_efectivetag (TObject *o);
void luaI_settagmethod (void); void luaI_settagmethod (void);
void luaI_gettagmethod (void); void luaI_gettagmethod (void);
void luaI_seterrormethod (void); void luaI_seterrormethod (void);
+22 -12
View File
@@ -5,6 +5,7 @@
#include "luamem.h" #include "luamem.h"
#include "func.h" #include "func.h"
#include "opcode.h" #include "opcode.h"
#include "inout.h"
static TFunc *function_root = NULL; static TFunc *function_root = NULL;
@@ -23,7 +24,7 @@ void luaI_initTFunc (TFunc *f)
f->size = 0; f->size = 0;
f->code = NULL; f->code = NULL;
f->lineDefined = 0; f->lineDefined = 0;
f->fileName = NULL; f->fileName = lua_parsedfile;
f->locvars = NULL; f->locvars = NULL;
} }
@@ -49,35 +50,44 @@ void luaI_freefunc (TFunc *f)
luaI_free (f); luaI_free (f);
} }
void luaI_funcfree (TFunc *l)
{
while (l) {
TFunc *next = l->next;
luaI_freefunc(l);
l = next;
}
}
/* /*
** Garbage collection function. ** 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 *curr = function_root;
TFunc *prev = NULL; TFunc *prev = NULL;
Long counter = 0; TFunc *frees = NULL;
while (curr) long counter = 0;
{ while (curr) {
TFunc *next = curr->next; TFunc *next = curr->next;
if (!curr->marked) if (!curr->marked) {
{
if (prev == NULL) if (prev == NULL)
function_root = next; function_root = next;
else else
prev->next = next; prev->next = next;
luaI_freefunc (curr); curr->next = frees;
frees = curr;
++counter; ++counter;
} }
else else {
{
curr->marked = 0; curr->marked = 0;
prev = curr; prev = curr;
} }
curr = next; curr = next;
} }
return counter; *acum += counter;
return frees;
} }
+3 -2
View File
@@ -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 #ifndef func_h
@@ -30,7 +30,8 @@ typedef struct TFunc
LocVar *locvars; LocVar *locvars;
} TFunc; } TFunc;
Long luaI_funccollector (void); TFunc *luaI_funccollector (long *cont);
void luaI_funcfree (TFunc *l);
void luaI_insertfunction (TFunc *f); void luaI_insertfunction (TFunc *f);
void luaI_initTFunc (TFunc *f); void luaI_initTFunc (TFunc *f);
+134 -119
View File
@@ -3,7 +3,7 @@
** hash manager for lua ** 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" #include "luamem.h"
@@ -33,7 +33,7 @@ static Hash *listhead = NULL;
/* hash dimensions values */ /* hash dimensions values */
static Long dimensions[] = 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, 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT}; 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
@@ -49,27 +49,6 @@ int luaI_redimension (int nhash)
return 0; /* to avoid warnings */ 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) 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)
{ static long int hashindex (TObject *ref)
int h = hashindex(t, ref); {
while (ttype(ref(node(t, h))) != LUA_T_NIL) long int h;
{ switch (ttype(ref)) {
if (lua_equalObj(ref, ref(node(t, h)))) case LUA_T_NUMBER:
return h; h = (long int)nvalue(ref); break;
h = (h+1) % nhash(t); case LUA_T_STRING: case LUA_T_USERDATA:
} h = tsvalue(ref)->hash; break;
return h; 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; TObject t;
ttype(&t) = LUA_T_ARRAY; ttype(&t) = LUA_T_ARRAY;
for (curr_array = listhead; curr_array; curr_array = curr_array->next) for (; l; l=l->next) {
if (markarray(curr_array) != 1) avalue(&t) = l;
{ luaI_gcIM(&t);
avalue(&t) = curr_array; }
luaI_gcIM(&t); }
}
void luaI_hashfree (Hash *frees)
{
while (frees) {
Hash *next = frees->next;
hashdelete(frees);
frees = next;
}
} }
/* Hash *luaI_hashcollector (long *acum)
** Garbage collection to arrays
** Delete all unmarked arrays.
*/
Long lua_hashcollector (void)
{ {
Hash *curr_array = listhead, *prev = NULL; Hash *curr_array = listhead, *prev = NULL, *frees = NULL;
Long counter = 0; long counter = 0;
while (curr_array != NULL) while (curr_array != NULL) {
{ Hash *next = curr_array->next;
Hash *next = curr_array->next; if (markarray(curr_array) != 1) {
if (markarray(curr_array) != 1) if (prev == NULL)
{ listhead = next;
if (prev == NULL) listhead = next; else
else prev->next = next; prev->next = next;
hashdelete(curr_array); curr_array->next = frees;
++counter; frees = curr_array;
++counter;
}
else {
markarray(curr_array) = 0;
prev = curr_array;
}
curr_array = next;
} }
else *acum += counter;
{ return frees;
markarray(curr_array) = 0;
prev = curr_array;
}
curr_array = next;
}
return counter;
} }
@@ -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) static void rehash (Hash *t)
{ {
int i; int nold = nhash(t);
int nold = nhash(t); Node *vold = nodevector(t);
Node *vold = nodevector(t); int i;
nhash(t) = luaI_redimension(nhash(t)); if (!emptyslots(t))
nodevector(t) = hashnodecreate(nhash(t)); nhash(t) = luaI_redimension(nhash(t));
for (i=0; i<nold; i++) nodevector(t) = hashnodecreate(nhash(t));
{ for (i=0; i<nold; i++) {
Node *n = vold+i; Node *n = vold+i;
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) 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 */ *node(t, present(t, ref(n))) = *n; /* copy old node to new hash */
} }
luaI_free(vold); luaI_free(vold);
} }
/* /*
@@ -257,23 +281,17 @@ TObject *lua_hashget (Hash *t, TObject *ref)
*/ */
TObject *lua_hashdefine (Hash *t, TObject *ref) TObject *lua_hashdefine (Hash *t, TObject *ref)
{ {
int h; Node *n = node(t, present(t, ref));
Node *n; if (ttype(ref(n)) == LUA_T_NIL) {
h = present(t, ref); nuse(t)++;
n = node(t, h); if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
if (ttype(ref(n)) == LUA_T_NIL) rehash(t);
{ n = node(t, present(t, ref));
nuse(t)++; }
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) *ref(n) = *ref;
{ ttype(val(n)) = LUA_T_NIL;
rehash(t);
h = present(t, ref);
n = node(t, h);
} }
*ref(n) = *ref; return (val(n));
ttype(val(n)) = LUA_T_NIL;
}
return (val(n));
} }
@@ -285,33 +303,30 @@ TObject *lua_hashdefine (Hash *t, TObject *ref)
*/ */
static void hashnext (Hash *t, int i) static void hashnext (Hash *t, int i)
{ {
if (i >= nhash(t)) Node *n;
return; int tsize = nhash(t);
while (ttype(ref(node(t,i))) == LUA_T_NIL || if (i >= tsize)
ttype(val(node(t,i))) == LUA_T_NIL) return;
{ n = node(t, i);
if (++i >= nhash(t)) while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
return; if (++i >= tsize)
} return;
luaI_pushobject(ref(node(t,i))); n = node(t, i);
luaI_pushobject(val(node(t,i))); }
luaI_pushobject(ref(node(t,i)));
luaI_pushobject(val(node(t,i)));
} }
void lua_next (void) void lua_next (void)
{ {
Hash *t; Hash *t;
lua_Object o = lua_getparam(1); lua_Object o = lua_getparam(1);
lua_Object r = lua_getparam(2); lua_Object r = lua_getparam(2);
luaL_arg_check(lua_istable(o), 1, "table expected"); luaL_arg_check(lua_istable(o), 1, "table expected");
luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected"); luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
t = avalue(luaI_Address(o)); t = avalue(luaI_Address(o));
if (lua_isnil(r)) if (lua_isnil(r))
{ hashnext(t, 0);
hashnext(t, 0); else
} hashnext(t, present(t, luaI_Address(r))+1);
else
{
int h = present (t, luaI_Address(r));
hashnext(t, h+1);
}
} }
+4 -3
View File
@@ -1,7 +1,7 @@
/* /*
** hash.h ** hash.h
** hash manager for lua ** 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 #ifndef hash_h
@@ -29,8 +29,9 @@ int lua_equalObj (TObject *t1, TObject *t2);
int luaI_redimension (int nhash); int luaI_redimension (int nhash);
Hash *lua_createarray (int nhash); Hash *lua_createarray (int nhash);
void lua_hashmark (Hash *h); void lua_hashmark (Hash *h);
Long lua_hashcollector (void); Hash *luaI_hashcollector (long *count);
void luaI_hashcallIM (void); void luaI_hashcallIM (Hash *l);
void luaI_hashfree (Hash *frees);
TObject *lua_hashget (Hash *t, TObject *ref); TObject *lua_hashget (Hash *t, TObject *ref);
TObject *lua_hashdefine (Hash *t, TObject *ref); TObject *lua_hashdefine (Hash *t, TObject *ref);
void lua_next (void); void lua_next (void);
+135 -94
View File
@@ -5,22 +5,24 @@
** Also provides some predefined lua functions. ** 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 <stdio.h>
#include <string.h> #include <string.h>
#include "auxlib.h" #include "auxlib.h"
#include "lex.h" #include "fallback.h"
#include "opcode.h" #include "hash.h"
#include "inout.h" #include "inout.h"
#include "lex.h"
#include "lua.h"
#include "luamem.h"
#include "luamem.h"
#include "opcode.h"
#include "table.h" #include "table.h"
#include "tree.h" #include "tree.h"
#include "lua.h" #include "undump.h"
#include "hash.h" #include "zio.h"
#include "luamem.h"
#include "fallback.h"
#include "luamem.h"
/* Exported variables */ /* Exported variables */
@@ -34,80 +36,87 @@ char *luaI_typenames[] = { /* ORDER LUA_T */
NULL NULL
}; };
static FILE *fp;
static char *st;
/*
** Function to get the next character from the input file void luaI_setparsedfile (char *name)
*/
static int fileinput (void)
{ {
int c = fgetc(fp); lua_parsedfile = luaI_createfixedstring(name)->str;
return (c == EOF) ? 0 : c;
} }
/*
** Function to get the next character from the input string
*/
static int stringinput (void)
{
return *st++;
}
/* int lua_doFILE (FILE *f, int bin)
** Function to open a file to be input unit.
** Return the file.
*/
FILE *lua_openfile (char *fn)
{ {
lua_setinput (fileinput); ZIO z;
if (fn == NULL) luaZ_Fopen(&z, f);
{ if (bin)
fp = stdin; return luaI_undump(&z);
fn = "(stdin)"; else {
} lua_setinput(&z);
else return lua_domain();
fp = fopen (fn, "r"); }
if (fp == NULL) }
return NULL;
lua_parsedfile = luaI_createfixedstring(fn)->str;
return fp;
}
/*
** Function to close an opened file int lua_dofile (char *filename)
*/
void lua_closefile (void)
{ {
if (fp != NULL && fp != stdin) int status;
{ int c;
fclose (fp); FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
fp = NULL; 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 */ #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]; int status;
lua_setinput(stringinput); ZIO z;
st = s; luaI_setparsedfile("(buffer)");
sprintf(buff, "(dostring) >> %.20s%s", s, luaZ_mopen(&z, buff, size);
(strlen(s) > SIZE_PREF) ? "..." : ""); status = luaI_undump(&z);
lua_parsedfile = luaI_createfixedstring(buff)->str; return status;
} }
/*
** Function to close an opened string int lua_dostring (char *str)
*/
void lua_closestring (void)
{ {
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) static int passresults (void)
{ {
int arg = 0; int arg = 0;
@@ -116,15 +125,45 @@ static int passresults (void)
lua_pushobject(obj); lua_pushobject(obj);
return arg-1; 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 ** Internal function: do a string
*/ */
static void lua_internaldostring (void) 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 (lua_dostring(luaL_check_string(1)) == 0)
if (passresults() == 0) if (passresults() == 0)
lua_pushuserdata(NULL); /* at least one result to signal no errors */ 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: case LUA_T_CFUNCTION: case LUA_T_NIL:
return luaI_typenames[-ttype(o)]; return luaI_typenames[-ttype(o)];
case LUA_T_USERDATA: { case LUA_T_USERDATA: {
char *buff = luaI_buffer(100); char *buff = luaI_buffer(30);
int size = o->value.ts->size; sprintf(buff, "userdata: %p", o->value.ts->u.v);
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]);
return buff; return buff;
} }
default: return "<unknown object>"; default: return "<unknown object>";
@@ -242,37 +275,37 @@ static void luatag (void)
lua_pushnumber(lua_tag(lua_getparam(1))); 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) static void luaI_call (void)
{ {
lua_Object f = lua_getparam(1); lua_Object f = lua_getparam(1);
lua_Object arg = lua_getparam(2); lua_Object arg = lua_getparam(2);
lua_Object temp, params[MAXPARAMS]; int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
int narg, i; int narg, i;
luaL_arg_check(lua_isfunction(f), 1, "function expected"); luaL_arg_check(lua_isfunction(f), 1, "function expected");
luaL_arg_check(lua_istable(arg), 2, "table expected"); luaL_arg_check(lua_istable(arg), 2, "table expected");
/* narg = arg.n */ narg = getnarg(arg);
lua_pushobject(arg); /* push arg[1...n] */
lua_pushstring("n");
temp = lua_getsubscript();
narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
/* read arg[1...n] */
for (i=0; i<narg; i++) { for (i=0; i<narg; i++) {
if (i>=MAXPARAMS) lua_Object temp;
lua_error("argument list too long in function `call'"); /* temp = arg[i+1] */
lua_pushobject(arg); lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
lua_pushnumber(i+1); if (narg == MAX_WORD && lua_isnil(temp))
params[i] = lua_getsubscript();
if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
narg = i;
break; break;
} lua_pushobject(temp);
} }
/* push parameters and do the call */
for (i=0; i<narg; i++)
lua_pushobject(params[i]);
if (lua_callfunction(f)) if (lua_callfunction(f))
lua_error(NULL); lua_error(NULL);
else if (withtable)
packresults();
else else
passresults(); passresults();
} }
@@ -280,7 +313,7 @@ static void luaI_call (void)
static void luaIl_settag (void) static void luaIl_settag (void)
{ {
lua_Object o = lua_getparam(1); 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_pushobject(o);
lua_settag(luaL_check_number(2)); 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 ** Internal functions
@@ -325,6 +363,7 @@ static struct {
} int_funcs[] = { } int_funcs[] = {
{"assert", luaI_assert}, {"assert", luaI_assert},
{"call", luaI_call}, {"call", luaI_call},
{"collectgarbage", luaI_collectgarbage},
{"dofile", lua_internaldofile}, {"dofile", lua_internaldofile},
{"dostring", lua_internaldostring}, {"dostring", lua_internaldostring},
{"error", luaI_error}, {"error", luaI_error},
@@ -338,7 +377,9 @@ static struct {
{"rawsetglobal", luaI_rawsetglobal}, {"rawsetglobal", luaI_rawsetglobal},
{"rawsettable", rawsettable}, {"rawsettable", rawsettable},
{"seterrormethod", luaI_seterrormethod}, {"seterrormethod", luaI_seterrormethod},
#if LUA_COMPAT2_5
{"setfallback", luaI_setfallback}, {"setfallback", luaI_setfallback},
#endif
{"setglobal", luaI_setglobal}, {"setglobal", luaI_setglobal},
{"settagmethod", luaI_settagmethod}, {"settagmethod", luaI_settagmethod},
{"gettagmethod", luaI_gettagmethod}, {"gettagmethod", luaI_gettagmethod},
@@ -360,7 +401,7 @@ void luaI_predefine (void)
n = luaI_findsymbolbyname(int_funcs[i].name); n = luaI_findsymbolbyname(int_funcs[i].name);
s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func; 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); s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
} }
+6 -5
View File
@@ -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 Word lua_debugline;
extern char *lua_parsedfile; extern char *lua_parsedfile;
FILE *lua_openfile (char *fn); void luaI_setparsedfile (char *name);
void lua_closefile (void);
void lua_openstring (char *s);
void lua_closestring (void);
void luaI_predefine (void); void luaI_predefine (void);
int lua_dobuffer (char *buff, int size);
int lua_doFILE (FILE *f, int bin);
#endif #endif
+66 -84
View File
@@ -10,8 +10,6 @@
#include "lualib.h" #include "lualib.h"
FILE *lua_infile, *lua_outfile;
int lua_tagio; 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) lua_Object f = lua_getglobal(name);
return; if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio)
if (f == lua_infile) luaL_verror("global variable %s is not a file handle", name);
lua_infile = stdin; return lua_getuserdata(f);
if (f == lua_outfile) }
lua_outfile = stdout;
static void closefile (char *name)
{
FILE *f = getfile(name);
if (f == stdin || f == stdout) return;
if (pclose(f) == -1) if (pclose(f) == -1)
fclose(f); 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) static void io_readfrom (void)
{ {
FILE *current;
lua_Object f = lua_getparam(1); lua_Object f = lua_getparam(1);
if (f == LUA_NOOBJECT) if (f == LUA_NOOBJECT) {
closefile(lua_infile); /* restore standart input */ closefile("_INPUT");
current = stdin;
}
else if (lua_tag(f) == lua_tagio) else if (lua_tag(f) == lua_tagio)
lua_infile = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(1); char *s = luaL_check_string(1);
FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
if (fp) if (current == NULL) {
lua_infile = fp;
else {
pushresult(0); pushresult(0);
return; return;
} }
} }
lua_pushusertag(lua_infile, lua_tagio); setreturn(current, "_INPUT");
} }
static void io_writeto (void) static void io_writeto (void)
{ {
FILE *current;
lua_Object f = lua_getparam(1); lua_Object f = lua_getparam(1);
if (f == LUA_NOOBJECT) if (f == LUA_NOOBJECT) {
closefile(lua_outfile); /* restore standart output */ closefile("_OUTPUT");
current = stdout;
}
else if (lua_tag(f) == lua_tagio) else if (lua_tag(f) == lua_tagio)
lua_outfile = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(1); char *s = luaL_check_string(1);
FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
if (fp) if (current == NULL) {
lua_outfile = fp;
else {
pushresult(0); pushresult(0);
return; 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); char *s = luaL_check_string(1);
FILE *fp = fopen (s, "a"); FILE *fp = fopen (s, "a");
if (fp != NULL) { if (fp != NULL)
lua_outfile = fp; setreturn(fp, "_OUTPUT");
lua_pushusertag(lua_outfile, lua_tagio);
}
else else
pushresult(0); pushresult(0);
} }
@@ -112,6 +129,7 @@ static void io_appendto (void)
static void io_read (void) static void io_read (void)
{ {
FILE *f = getfile("_INPUT");
char *buff; char *buff;
char *p = luaL_opt_string(1, "[^\n]*{\n}"); char *p = luaL_opt_string(1, "[^\n]*{\n}");
int inskip = 0; /* to control {skips} */ int inskip = 0; /* to control {skips} */
@@ -131,7 +149,7 @@ static void io_read (void)
else { else {
char *ep = luaL_item_end(p); /* get what is next */ char *ep = luaL_item_end(p); /* get what is next */
int m; /* match result */ 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); m = (c == EOF) ? 0 : luaL_singlematch((char)c, p);
if (m) { if (m) {
if (inskip == 0) luaI_addchar(c); if (inskip == 0) luaI_addchar(c);
@@ -152,7 +170,7 @@ static void io_read (void)
} }
} break_while: } break_while:
if (c >= 0) /* not EOF nor NEED_OTHER? */ if (c >= 0) /* not EOF nor NEED_OTHER? */
ungetc(c, lua_infile); ungetc(c, f);
buff = luaI_addchar(0); buff = luaI_addchar(0);
if (*buff != 0 || *p == 0) /* read something or did not fail? */ if (*buff != 0 || *p == 0) /* read something or did not fail? */
lua_pushstring(buff); lua_pushstring(buff);
@@ -161,11 +179,12 @@ static void io_read (void)
static void io_write (void) static void io_write (void)
{ {
FILE *f = getfile("_OUTPUT");
int arg = 1; int arg = 1;
int status = 1; int status = 1;
char *s; char *s;
while ((s = luaL_opt_string(arg++, NULL)) != NULL) while ((s = luaL_opt_string(arg++, NULL)) != NULL)
status = status && (fputs(s, lua_outfile) != EOF); status = status && (fputs(s, f) != EOF);
pushresult(status); pushresult(status);
} }
@@ -242,28 +261,31 @@ static void lua_printstack (FILE *f)
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
char *name; char *name;
int currentline; int currentline;
char *filename;
int linedefined;
lua_funcinfo(func, &filename, &linedefined);
fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t"); fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
switch (*lua_getobjname(func, &name)) { switch (*lua_getobjname(func, &name)) {
case 'g': case 'g':
fprintf(f, "function %s", name); fprintf(f, "function %s", name);
break; break;
case 'f': case 't':
fprintf(f, "`%s' iternal method", name); fprintf(f, "`%s' tag method", name);
break; break;
default: { default: {
char *filename;
int linedefined;
lua_funcinfo(func, &filename, &linedefined);
if (linedefined == 0) if (linedefined == 0)
fprintf(f, "main of %s", filename); fprintf(f, "main of %s", filename);
else if (linedefined < 0) else if (linedefined < 0)
fprintf(f, "%s", filename); fprintf(f, "%s", filename);
else else
fprintf(f, "function (%s:%d)", filename, linedefined); fprintf(f, "function (%s:%d)", filename, linedefined);
filename = NULL;
} }
} }
if ((currentline = lua_currentline(func)) > 0) if ((currentline = lua_currentline(func)) > 0)
fprintf(f, " at line %d", currentline); fprintf(f, " at line %d", currentline);
if (filename)
fprintf(f, " [in file %s]", filename);
fprintf(f, "\n"); 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[] = { static struct luaL_reg iolib[] = {
{"readfrom", io_readfrom}, {"readfrom", io_readfrom},
@@ -334,15 +313,18 @@ static struct luaL_reg iolib[] = {
{"date", io_date}, {"date", io_date},
{"exit", io_exit}, {"exit", io_exit},
{"debug", io_debug}, {"debug", io_debug},
{"getbyte", getbyte},
{"userdata", newuserdata},
{"print_stack", errorfb} {"print_stack", errorfb}
}; };
void iolib_open (void) void iolib_open (void)
{ {
lua_tagio = lua_newtag(); 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]))); luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
lua_seterrormethod(errorfb); lua_pushcfunction(errorfb);
lua_seterrormethod();
} }
+57 -46
View File
@@ -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> #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 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(x) (yytext[tokensize++] = (x))
#define save_and_next() (save(current), next()) #define save_and_next() (save(current), next())
static int current; /* look ahead character */ #define MAX_IFS 5
static Input input; /* input function */
#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 */ static int iflevel; /* level of nested $if's */
void lua_setinput (Input fn) void lua_setinput (ZIO *z)
{ {
current = '\n'; current = '\n';
lua_linenumber = 0; lua_linenumber = 0;
iflevel = 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) static int checkcond (char *buff)
{ {
if (strcmp(buff, "nil") == 0) static char *opts[] = {"nil", "1"};
return 0; int i = luaI_findstring(buff, opts);
else if (strcmp(buff, "1") == 0) if (i >= 0) return i;
return 1; else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
else if (isalpha((unsigned char)buff[0]))
return luaI_globaldefined(buff); return luaI_globaldefined(buff);
else { else {
luaI_auxsynterrbf("invalid $if condition", buff); luaI_auxsynterrbf("invalid $if condition", buff);
@@ -134,7 +135,7 @@ static void readname (char *buff)
{ {
int i = 0; int i = 0;
skipspace(); skipspace();
while (isalnum((unsigned char)current)) { while (isalnum((unsigned char)current) || current == '_') {
if (i >= PRAGMASIZE) { if (i >= PRAGMASIZE) {
buff[PRAGMASIZE] = 0; buff[PRAGMASIZE] = 0;
luaI_auxsynterrbf("pragma too long", buff); luaI_auxsynterrbf("pragma too long", buff);
@@ -149,13 +150,12 @@ static void readname (char *buff)
static void inclinenumber (void); static void inclinenumber (void);
static void ifskip (int thisiflevel) static void ifskip (void)
{ {
while (iflevel > thisiflevel && while (ifstate[iflevel].skip) {
(ifstate[thisiflevel] == 0 || ifstate[thisiflevel] == 3)) {
if (current == '\n') if (current == '\n')
inclinenumber(); inclinenumber();
else if (current == 0) else if (current == EOZ)
luaI_auxsyntaxerror("input ends inside a $if"); luaI_auxsyntaxerror("input ends inside a $if");
else next(); else next();
} }
@@ -165,49 +165,60 @@ static void ifskip (int thisiflevel)
static void inclinenumber (void) static void inclinenumber (void)
{ {
static char *pragmas [] = static char *pragmas [] =
{"debug", "nodebug", "end", "ifnot", "if", "else", NULL}; {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
next(); /* skip '\n' */ next(); /* skip '\n' */
++lua_linenumber; ++lua_linenumber;
if (current == '$') { /* is a pragma? */ if (current == '$') { /* is a pragma? */
char buff[PRAGMASIZE+1]; char buff[PRAGMASIZE+1];
int ifnot = 0; int ifnot = 0;
int skip = ifstate[iflevel].skip;
next(); /* skip $ */ next(); /* skip $ */
readname(buff); readname(buff);
switch (luaI_findstring(buff, pragmas)) { switch (luaI_findstring(buff, pragmas)) {
case 0: /* debug */ case 0: /* debug */
lua_debug = 1; if (!skip) lua_debug = 1;
break; break;
case 1: /* nodebug */ case 1: /* nodebug */
lua_debug = 0; if (!skip) lua_debug = 0;
break; break;
case 2: /* end */ case 2: /* endinput */
if (--iflevel < 0) if (!skip) {
current = EOZ;
iflevel = 0; /* to allow $endinput inside a $if */
}
break;
case 3: /* end */
if (iflevel-- == 0)
luaI_auxsyntaxerror("unmatched $endif"); luaI_auxsyntaxerror("unmatched $endif");
break; break;
case 3: /* ifnot */ case 4: /* ifnot */
ifnot = 1; ifnot = 1;
/* go through */ /* go through */
case 4: /* if */ case 5: /* if */
if (iflevel == MAX_IFS) if (iflevel == MAX_IFS-1)
luaI_auxsyntaxerror("too many nested `$ifs'"); luaI_auxsyntaxerror("too many nested `$ifs'");
readname(buff); 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; break;
case 5: /* else */ case 6: /* else */
if (iflevel <= 0 || (ifstate[iflevel-1] & 2)) if (ifstate[iflevel].elsepart)
luaI_auxsyntaxerror("unmatched $else"); 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; break;
default: default:
luaI_auxsynterrbf("invalid pragma", buff); luaI_auxsynterrbf("invalid pragma", buff);
} }
skipspace(); skipspace();
if (current == '\n') /* pragma must end with a '\n' */ if (current == '\n') /* pragma must end with a '\n' ... */
inclinenumber(); inclinenumber();
else if (current != 0) /* or eof */ else if (current != EOZ) /* or eof */
luaI_auxsyntaxerror("invalid pragma format"); luaI_auxsyntaxerror("invalid pragma format");
if (iflevel > 0) ifskip();
ifskip(iflevel-1);
} }
} }
@@ -221,7 +232,7 @@ static int read_long_string (char *yytext, int buffsize)
yytext = luaI_buffer(buffsize *= 2); yytext = luaI_buffer(buffsize *= 2);
switch (current) switch (current)
{ {
case 0: case EOZ:
save(0); save(0);
return WRONGTOKEN; return WRONGTOKEN;
case '[': case '[':
@@ -284,7 +295,7 @@ int luaY_lex (void)
case '-': case '-':
save_and_next(); save_and_next();
if (current != '-') return '-'; if (current != '-') return '-';
do { next(); } while (current != '\n' && current != 0); do { next(); } while (current != '\n' && current != EOZ);
continue; continue;
case '[': case '[':
@@ -327,7 +338,7 @@ int luaY_lex (void)
yytext = luaI_buffer(buffsize *= 2); yytext = luaI_buffer(buffsize *= 2);
switch (current) switch (current)
{ {
case 0: case EOZ:
case '\n': case '\n':
save(0); save(0);
return WRONGTOKEN; return WRONGTOKEN;
@@ -444,7 +455,7 @@ int luaY_lex (void)
return NUMBER; return NUMBER;
} }
case 0: case EOZ:
save(0); save(0);
if (iflevel > 0) if (iflevel > 0)
luaI_syntaxerror("missing $endif"); luaI_syntaxerror("missing $endif");
+3 -4
View File
@@ -1,16 +1,15 @@
/* /*
** lex.h ** lex.h
** TecCGraf - PUC-Rio ** 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 #ifndef lex_h
#define lex_h #define lex_h
#include "zio.h"
typedef int (*Input) (void); void lua_setinput (ZIO *z);
void lua_setinput (Input fn);
void luaI_syntaxerror (char *s); void luaI_syntaxerror (char *s);
int luaY_lex (void); int luaY_lex (void);
void luaI_addReserved (void); void luaI_addReserved (void);
+71 -1
View File
@@ -3,12 +3,13 @@
** Linguagem para Usuarios de Aplicacao ** 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 <stdio.h>
#include <string.h> #include <string.h>
#include "lua.h" #include "lua.h"
#include "auxlib.h"
#include "lualib.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 #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) static void manual_input (void)
{ {
if (isatty(0)) { if (isatty(0)) {
@@ -41,6 +110,7 @@ int main (int argc, char *argv[])
iolib_open (); iolib_open ();
strlib_open (); strlib_open ();
mathlib_open (); mathlib_open ();
lua_register("testC", testC);
if (argc < 2) if (argc < 2)
manual_input(); manual_input();
else for (i=1; i<argc; i++) { else for (i=1; i<argc; i++) {
+39 -19
View File
@@ -2,26 +2,28 @@
** LUA - An Extensible Extension Language ** LUA - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br ** 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 #ifndef lua_h
#define 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_COPYRIGHT "Copyright (C) 1994-1997 TeCGraf"
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo" #define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
#define LUA_NOOBJECT 0 #define LUA_NOOBJECT 0
#define LUA_ANYTAG (-1)
typedef void (*lua_CFunction) (void); typedef void (*lua_CFunction) (void);
typedef unsigned int lua_Object; typedef unsigned int lua_Object;
void lua_settagmethod (int tag, char *event, lua_CFunction method); lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
void lua_gettagmethod (int tag, char *event); /* out: method */ lua_Object lua_gettagmethod (int tag, char *event);
void lua_seterrormethod (lua_CFunction method); lua_Object lua_seterrormethod (void); /* In: new method */
int lua_newtag (void); int lua_newtag (void);
void lua_settag (int tag); /* In: object */ 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_dostring (char *string); /* Out: returns */
int lua_callfunction (lua_Object f); int lua_callfunction (lua_Object f);
/* In: parameters; Out: returns */ /* In: parameters; Out: returns */
int lua_call (char *funcname);
/* In: parameters; Out: returns */
void lua_beginblock (void); void lua_beginblock (void);
void lua_endblock (void); void lua_endblock (void);
@@ -52,17 +52,18 @@ int lua_isfunction (lua_Object object);
float lua_getnumber (lua_Object object); float lua_getnumber (lua_Object object);
char *lua_getstring (lua_Object object); char *lua_getstring (lua_Object object);
lua_CFunction lua_getcfunction (lua_Object object); lua_CFunction lua_getcfunction (lua_Object object);
void *lua_getbindata (lua_Object object); void *lua_getuserdata (lua_Object object);
int lua_getbindatasize (lua_Object object);
void lua_pushnil (void); void lua_pushnil (void);
void lua_pushnumber (float n); void lua_pushnumber (float n);
void lua_pushstring (char *s); void lua_pushstring (char *s);
void lua_pushcfunction (lua_CFunction fn); 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); void lua_pushobject (lua_Object object);
lua_Object lua_pop (void);
lua_Object lua_getglobal (char *name); lua_Object lua_getglobal (char *name);
lua_Object lua_rawgetglobal (char *name); lua_Object lua_rawgetglobal (char *name);
void lua_setglobal (char *name); /* In: value */ void lua_setglobal (char *name); /* In: value */
@@ -78,15 +79,20 @@ int lua_tag (lua_Object object);
int lua_ref (int lock); /* In: value */ int lua_ref (int lock); /* In: value */
lua_Object lua_getref (int ref); lua_Object lua_getref (int ref);
void lua_pushref (int ref);
void lua_unref (int ref); void lua_unref (int ref);
lua_Object lua_createtable (void); lua_Object lua_createtable (void);
long lua_collectgarbage (long limit);
/* =============================================================== */ /* =============================================================== */
/* some useful macros */ /* 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_refobject(o,l) (lua_pushobject(o), lua_ref(l))
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n)) #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) #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); lua_Object lua_setfallback (char *event, lua_CFunction fallback);
#define lua_storeglobal(n) lua_setglobal(n) #define lua_storeglobal lua_setglobal
#define lua_type(o) (lua_tag(o)) #define lua_type lua_tag
void *lua_getuserdata (lua_Object object);
#define lua_lockobject(o) lua_refobject(o,1) #define lua_lockobject(o) lua_refobject(o,1)
#define lua_lock() lua_ref(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_pushliteral(o) lua_pushstring(o)
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_gettable()) #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_copystring(o) (strdup(lua_getstring(o)))
#define lua_getsubscript lua_gettable #define lua_getsubscript lua_gettable
#define lua_storesubscript lua_settable
#endif
#endif #endif
+1 -2
View File
@@ -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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -489,7 +489,6 @@ body : '(' parlist ')' block END
luaI_initTFunc($$); luaI_initTFunc($$);
$$->size = pc; $$->size = pc;
$$->code = newvector(pc, Byte); $$->code = newvector(pc, Byte);
$$->fileName = lua_parsedfile;
$$->lineDefined = $2; $$->lineDefined = $2;
memcpy($$->code, basepc, pc*sizeof(Byte)); memcpy($$->code, basepc, pc*sizeof(Byte));
if (lua_debug) if (lua_debug)
+22 -16
View File
@@ -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 #configuration
@@ -6,6 +6,8 @@
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant # define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
#define (undefine) NOSTRERROR if your system does NOT have function "strerror" #define (undefine) NOSTRERROR if your system does NOT have function "strerror"
# (although this is ANSI, SunOS does not comply; so, add "-DNOSTRERROR" on SunOS) # (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 CONFIG = -DPOPEN -D_POSIX_SOURCE
# Compilation parameters # Compilation parameters
CC = gcc CC = gcc
@@ -32,7 +34,8 @@ LUAOBJS = \
luamem.o \ luamem.o \
func.o \ func.o \
undump.o \ undump.o \
auxlib.o auxlib.o \
zio.o
LIBOBJS = \ LIBOBJS = \
iolib.o \ iolib.o \
@@ -77,29 +80,32 @@ clear :
co $@ 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 \ fallback.o: fallback.c auxlib.h lua.h luamem.h fallback.h opcode.h \
types.h tree.h func.h table.h hash.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 \ func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \
luamem.h luamem.h
hash.o: hash.c luamem.h opcode.h lua.h types.h tree.h func.h hash.h \ hash.o: hash.c luamem.h opcode.h lua.h types.h tree.h func.h hash.h \
table.h auxlib.h table.h auxlib.h
inout.o: inout.c auxlib.h lua.h lex.h opcode.h types.h tree.h func.h \ inout.o: inout.c auxlib.h lua.h fallback.h opcode.h types.h tree.h \
inout.h table.h hash.h luamem.h fallback.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 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 \ 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 func.h lex.h zio.h inout.h luadebug.h parser.h
lua.o: lua.c lua.h lualib.h lua.o: lua.c lua.h auxlib.h lualib.h
luamem.o: luamem.c luamem.h lua.h luamem.o: luamem.c luamem.h lua.h
mathlib.o: mathlib.c lualib.h lua.h auxlib.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 \ 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 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 opcode.h types.h \ parser.o: parser.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
tree.h func.h hash.h inout.h table.h types.h tree.h func.h hash.h inout.h table.h
strlib.o: strlib.c lua.h auxlib.h lualib.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 \ table.o: table.c luamem.h auxlib.h lua.h func.h types.h tree.h \
func.h hash.h table.h inout.h fallback.h luadebug.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 hash.h opcode.h \ tree.o: tree.c luamem.h lua.h tree.h types.h lex.h zio.h hash.h \
func.h table.h fallback.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 \ undump.o: undump.c auxlib.h lua.h opcode.h types.h tree.h func.h \
table.h undump.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
+1129 -664
View File
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -3,7 +3,7 @@
** Mathematics library to LUA ** 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 <stdlib.h>
#include <math.h> #include <math.h>
@@ -210,6 +210,8 @@ static struct luaL_reg mathlib[] = {
void mathlib_open (void) void mathlib_open (void)
{ {
luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); 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");
} }
+177 -232
View File
@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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 <setjmp.h>
#include <stdio.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 "table.h"
#include "lua.h" #include "lua.h"
#include "fallback.h" #include "fallback.h"
#include "undump.h"
#include "auxlib.h" #include "auxlib.h"
#include "lex.h"
#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0)) #define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
#define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(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() #define incr_top if (++top >= stackLimit) growstack()
struct C_Lua_Stack { struct C_Lua_Stack {
StkId base; /* when Lua calls C or C calls Lua, points to */ StkId base; /* when Lua calls C or C calls Lua, points to */
/* the first slot after the last parameter. */ /* the first slot after the last parameter. */
int num; /* when Lua calls C, has the number of parameters; */ StkId lua2C; /* points to first element of "array" lua2C */
/* when C calls Lua, has the number of results. */ 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 */ 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. ** 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; TObject *nt;
lua_checkstack(stack+newtop); lua_checkstack(stack+newtop);
nt = stack+newtop; /* warning: previous call may change stack */ nt = stack+newtop; /* warning: previous call may change stack */
while (top < nt) ttype(top++) = LUA_T_NIL; 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) #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. ** 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 ** call Line hook
*/ */
static void lineHook (int line) static void lineHook (int line)
{ {
struct C_Lua_Stack oldCLS = CLS_current; 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; CLS_current.num = 0;
(*lua_linehook)(line); (*lua_linehook)(line);
top = stack+old_top; top = stack+old_top;
@@ -223,7 +258,7 @@ static void lineHook (int line)
static void callHook (StkId base, lua_Type type, int isreturn) static void callHook (StkId base, lua_Type type, int isreturn)
{ {
struct C_Lua_Stack oldCLS = CLS_current; 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; CLS_current.num = 0;
if (isreturn) if (isreturn)
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0); (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
@@ -251,6 +286,7 @@ static StkId callC (lua_CFunction func, StkId base)
StkId firstResult; StkId firstResult;
CLS_current.num = (top-stack) - base; CLS_current.num = (top-stack) - base;
/* incorporate parameters on the stack */ /* incorporate parameters on the stack */
CLS_current.lua2C = base;
CLS_current.base = base+CLS_current.num; /* == top-stack */ CLS_current.base = base+CLS_current.num; /* == top-stack */
if (lua_callhook) if (lua_callhook)
callHook(base, LUA_T_CMARK, 0); 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); firstResult = lua_execute(func->value.tf->code, base);
} }
else { /* func is not a function */ 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); TObject *im = luaI_getimbyObj(func, IM_FUNCTION);
if (ttype(im) == LUA_T_NIL) if (ttype(im) == LUA_T_NIL)
lua_error("call expression not a function"); lua_error("call expression not a function");
@@ -300,7 +336,7 @@ static void do_call (StkId base, int nResults)
return; return;
} }
/* adjust the number of results */ /* adjust the number of results */
if (nResults != MULT_RET && top - (stack+firstResult) != nResults) if (nResults != MULT_RET)
adjust_top(firstResult+nResults); adjust_top(firstResult+nResults);
/* move results to base-1 (to erase parameters and function) */ /* move results to base-1 (to erase parameters and function) */
base--; base--;
@@ -317,9 +353,13 @@ static void do_call (StkId base, int nResults)
*/ */
static void pushsubscript (void) static void pushsubscript (void)
{ {
int tg = luaI_tag(top-2); TObject *im;
TObject *im = luaI_getim(tg, IM_GETTABLE); if (ttype(top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
if (ttype(top-2) == LUA_T_ARRAY && ttype(im) == LUA_T_NIL) { 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); TObject *h = lua_hashget(avalue(top-2), top-1);
if (h != NULL && ttype(h) != LUA_T_NIL) { if (h != NULL && ttype(h) != LUA_T_NIL) {
--top; --top;
@@ -331,19 +371,21 @@ static void pushsubscript (void)
--top; --top;
ttype(top-1) = LUA_T_NIL; 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 */ /* object is not a table, or it has a "gettable" method */
if (ttype(im) != LUA_T_NIL) if (ttype(im) != LUA_T_NIL)
callIM(im, 2, 1); callIM(im, 2, 1);
else else
lua_error("indexed expression not a table"); lua_error("indexed expression not a table");
}
} }
lua_Object lua_rawgettable (void) lua_Object lua_rawgettable (void)
{ {
adjustC(2); checkCparams(2);
if (ttype(top-2) != LUA_T_ARRAY) if (ttype(top-2) != LUA_T_ARRAY)
lua_error("indexed expression not a table in raw gettable"); lua_error("indexed expression not a table in raw gettable");
else { else {
@@ -354,8 +396,7 @@ lua_Object lua_rawgettable (void)
else else
ttype(top-1) = LUA_T_NIL; ttype(top-1) = LUA_T_NIL;
} }
CLS_current.base++; /* incorporate object in the stack */ return put_luaObjectonTop();
return (Ref(top-1));
} }
@@ -426,9 +467,7 @@ void lua_travstack (int (*fn)(TObject *))
static void lua_message (char *s) static void lua_message (char *s)
{ {
TObject *im = luaI_geterrorim(); TObject *im = luaI_geterrorim();
if (ttype(im) == LUA_T_NIL) if (ttype(im) != LUA_T_NIL) {
fprintf(stderr, "lua: %s\n", s);
else {
lua_pushstring(s); lua_pushstring(s);
callIM(im, 1, 0); callIM(im, 1, 0);
} }
@@ -486,7 +525,7 @@ int lua_setlocal (lua_Function func, int local_number)
{ {
TObject *f = Address(func); TObject *f = Address(func);
char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func)); char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
adjustC(1); checkCparams(1);
--top; --top;
if (name) if (name)
{ {
@@ -505,18 +544,21 @@ int lua_setlocal (lua_Function func, int local_number)
*/ */
static void do_callinc (int nResults) static void do_callinc (int nResults)
{ {
do_call(CLS_current.base+1, nResults); StkId base = CLS_current.base;
CLS_current.num = (top-stack) - CLS_current.base; /* number of results */ do_call(base+1, nResults);
CLS_current.base += CLS_current.num; /* incorporate results on the stack */ 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) static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
{ {
adjustC(nParams); StkId base = (top-stack)-nParams;
open_stack((top-stack)-CLS_current.base); open_stack(nParams);
stack[CLS_current.base].ttype = LUA_T_CFUNCTION; stack[base].ttype = LUA_T_CFUNCTION;
stack[CLS_current.base].value.f = f; stack[base].value.f = f;
do_callinc(nResults); do_call(base+1, nResults);
} }
@@ -554,7 +596,8 @@ int luaI_dorun (TFunc *tf)
return status; return status;
} }
static int do_protectedmain (void)
int lua_domain (void)
{ {
TFunc tf; TFunc tf;
int status; int status;
@@ -562,23 +605,21 @@ static int do_protectedmain (void)
jmp_buf *oldErr = errorJmp; jmp_buf *oldErr = errorJmp;
errorJmp = &myErrorJmp; errorJmp = &myErrorJmp;
luaI_initTFunc(&tf); luaI_initTFunc(&tf);
tf.fileName = lua_parsedfile; if (setjmp(myErrorJmp) == 0) {
if (setjmp(myErrorJmp) == 0)
{
lua_parse(&tf); lua_parse(&tf);
status = luaI_dorun(&tf); status = 0;
} }
else else {
{
status = 1;
adjustC(0); /* erase extra slot */ adjustC(0); /* erase extra slot */
status = 1;
} }
if (status == 0)
status = luaI_dorun(&tf);
errorJmp = oldErr; errorJmp = oldErr;
luaI_free(tf.code); luaI_free(tf.code);
return status; return status;
} }
/* /*
** Execute the given lua function. Return 0 on success or 1 on error. ** 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) lua_Object lua_gettagmethod (int tag, char *event)
{
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_pushnumber(tag); lua_pushnumber(tag);
lua_pushstring(event); lua_pushstring(event);
do_unprotectedrun(luaI_gettagmethod, 2, 1); 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_pushnumber(tag);
lua_pushstring(event); lua_pushstring(event);
if (method) *top = newmethod; incr_top;
lua_pushcfunction (method);
else
lua_pushnil();
do_unprotectedrun(luaI_settagmethod, 3, 1); do_unprotectedrun(luaI_settagmethod, 3, 1);
return put_luaObjectonTop();
} }
void lua_seterrormethod (lua_CFunction method) lua_Object lua_seterrormethod (void)
{ {
lua_pushcfunction (method); checkCparams(1);
do_unprotectedrun(luaI_seterrormethod, 1, 0); do_unprotectedrun(luaI_seterrormethod, 1, 1);
return put_luaObjectonTop();
} }
@@ -688,10 +670,9 @@ void lua_seterrormethod (lua_CFunction method)
*/ */
lua_Object lua_gettable (void) lua_Object lua_gettable (void)
{ {
adjustC(2); checkCparams(2);
pushsubscript(); pushsubscript();
CLS_current.base++; /* incorporate object in the stack */ return put_luaObjectonTop();
return (Ref(top-1));
} }
@@ -723,7 +704,7 @@ void lua_endblock (void)
void lua_settag (int tag) void lua_settag (int tag)
{ {
adjustC(1); checkCparams(1);
luaI_settag(tag, --top); luaI_settag(tag, --top);
} }
@@ -732,13 +713,13 @@ void lua_settag (int tag)
*/ */
void lua_settable (void) void lua_settable (void)
{ {
adjustC(3); checkCparams(3);
storesubscript(top-3, 1); storesubscript(top-3, 1);
} }
void lua_rawsettable (void) void lua_rawsettable (void)
{ {
adjustC(3); checkCparams(3);
storesubscript(top-3, 0); storesubscript(top-3, 0);
} }
@@ -747,12 +728,10 @@ void lua_rawsettable (void)
*/ */
lua_Object lua_createtable (void) lua_Object lua_createtable (void)
{ {
adjustC(0); TObject o;
avalue(top) = lua_createarray(0); avalue(&o) = lua_createarray(0);
ttype(top) = LUA_T_ARRAY; ttype(&o) = LUA_T_ARRAY;
incr_top; return put_luaObject(&o);
CLS_current.base++; /* incorporate object in the stack */
return Ref(top-1);
} }
/* /*
@@ -761,10 +740,10 @@ lua_Object lua_createtable (void)
*/ */
lua_Object lua_lua2C (int number) lua_Object lua_lua2C (int number)
{ {
if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT; if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
/* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) == /* Ref(stack+(CLS_current.lua2C+number-1)) ==
stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */ stack+(CLS_current.lua2C+number-1)-stack+1 == */
return CLS_current.base-CLS_current.num+number; return CLS_current.lua2C+number;
} }
int lua_isnil (lua_Object o) int lua_isnil (lua_Object o)
@@ -821,33 +800,20 @@ real lua_getnumber (lua_Object object)
*/ */
char *lua_getstring (lua_Object object) char *lua_getstring (lua_Object object)
{ {
if (object == LUA_NOOBJECT) return NULL; if (object == LUA_NOOBJECT || tostring (Address(object)))
if (tostring (Address(object))) return NULL; return NULL;
else return (svalue(Address(object))); 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 *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) if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
return 0; return NULL;
else return (Address(object))->value.ts->size; else return tsvalue(Address(object))->u.v;
} }
/* /*
** Given an object handle, return its cfuntion pointer. On error, return NULL. ** 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); TObject *o = luaI_getref(ref);
if (o == NULL) if (o == NULL)
return LUA_NOOBJECT; return LUA_NOOBJECT;
adjustC(0); return put_luaObject(o);
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);
} }
int lua_ref (int lock) int lua_ref (int lock)
{ {
adjustC(1); checkCparams(1);
return luaI_ref(--top, lock); return luaI_ref(--top, lock);
} }
@@ -894,20 +848,14 @@ int lua_ref (int lock)
*/ */
lua_Object lua_getglobal (char *name) lua_Object lua_getglobal (char *name)
{ {
adjustC(0); getglobal(luaI_findsymbolbyname(name));
getglobal(luaI_findsymbolbyname(name)); return put_luaObjectonTop();
CLS_current.base++; /* incorporate object in the stack */
return Ref(top-1);
} }
lua_Object lua_rawgetglobal (char *name) lua_Object lua_rawgetglobal (char *name)
{ {
adjustC(0); return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object);
*top = lua_table[luaI_findsymbolbyname(name)].object;
incr_top;
CLS_current.base++; /* incorporate object in the stack */
return Ref(top-1);
} }
@@ -935,15 +883,15 @@ static void setglobal (Word n)
void lua_setglobal (char *name) void lua_setglobal (char *name)
{ {
adjustC(1); checkCparams(1);
setglobal(luaI_findsymbolbyname(name)); setglobal(luaI_findsymbolbyname(name));
} }
void lua_rawsetglobal (char *name) void lua_rawsetglobal (char *name)
{ {
Word n = luaI_findsymbolbyname(name); Word n = luaI_findsymbolbyname(name);
adjustC(1); checkCparams(1);
s_object(n) = *(--top); s_object(n) = *(--top);
} }
/* /*
@@ -951,8 +899,8 @@ void lua_rawsetglobal (char *name)
*/ */
void lua_pushnil (void) void lua_pushnil (void)
{ {
ttype(top) = LUA_T_NIL; ttype(top) = LUA_T_NIL;
incr_top; incr_top;
} }
/* /*
@@ -978,8 +926,7 @@ void lua_pushstring (char *s)
} }
incr_top; incr_top;
} }
/*>>>>>>>>>#undef lua_pushliteral
void lua_pushliteral(char *s) { lua_pushstring(s); }*/
/* /*
** Push an object (ttype=cfunction) to stack. ** Push an object (ttype=cfunction) to stack.
@@ -990,25 +937,15 @@ void lua_pushcfunction (lua_CFunction fn)
incr_top; 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) 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) void lua_pushobject (lua_Object o)
{ {
if (o == LUA_NOOBJECT) if (o == LUA_NOOBJECT)
lua_error("attempt to push a NOOBJECT"); lua_error("API error - attempt to push a NOOBJECT");
*top = *Address(o); *top = *Address(o);
if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION; if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION; else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
incr_top; 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 */ TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
if (ttype(im) == LUA_T_NIL) { if (ttype(im) == LUA_T_NIL) {
@@ -1058,29 +1004,19 @@ static void call_arith (IMS event)
if (ttype(im) == LUA_T_NIL) { if (ttype(im) == LUA_T_NIL) {
im = luaI_getim(0, event); /* try a 'global' i.m. */ im = luaI_getim(0, event); /* try a 'global' i.m. */
if (ttype(im) == LUA_T_NIL) if (ttype(im) == LUA_T_NIL)
lua_error("unexpected type at conversion to number"); lua_error(msg);
} }
} }
lua_pushstring(luaI_eventname[event]); lua_pushstring(luaI_eventname[event]);
callIM(im, 3, 1); callIM(im, 3, 1);
} }
static void concim (TObject *o)
static void call_arith (IMS event)
{ {
TObject *im = luaI_getimbyObj(o, IM_CONCAT); call_binTM(event, "unexpected type at arithmetic operation");
if (ttype(im) == LUA_T_NIL)
lua_error("unexpected type at conversion to string");
callIM(im, 2, 1);
} }
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, static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
lua_Type ttype_great, IMS op) lua_Type ttype_great, IMS op)
@@ -1090,16 +1026,12 @@ static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
int result; int result;
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER) if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1; result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
else if (tostring(l)) { else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
ordim(l, op);
return;
}
else if (tostring(r)) {
ordim(r, op);
return;
}
else
result = strcmp(svalue(l), svalue(r)); result = strcmp(svalue(l), svalue(r));
else {
call_binTM(op, "unexpected type at comparison");
return;
}
top--; top--;
nvalue(top-1) = 1; nvalue(top-1) = 1;
ttype(top-1) = (result < 0) ? ttype_less : ttype(top-1) = (result < 0) ? ttype_less :
@@ -1308,9 +1240,11 @@ static StkId lua_execute (Byte *pc, StkId base)
adjust_top(base); adjust_top(base);
break; break;
case ADJUST: case ADJUST: {
adjust_top(base + *(pc++)); StkId newtop = base + *(pc++);
adjust_top(newtop);
break; break;
}
case VARARGS: case VARARGS:
adjust_varargs(base + *(pc++)); adjust_varargs(base + *(pc++));
@@ -1414,10 +1348,8 @@ static StkId lua_execute (Byte *pc, StkId base)
case CONCOP: { case CONCOP: {
TObject *l = top-2; TObject *l = top-2;
TObject *r = top-1; TObject *r = top-1;
if (tostring(l)) /* first argument is not a string */ if (tostring(l) || tostring(r))
concim(l); call_binTM(IM_CONCAT, "unexpected type for concatenation");
else if (tostring(r)) /* second argument is not a string */
concim(r);
else { else {
tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r))); tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
--top; --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
+2 -1
View File
@@ -1,6 +1,6 @@
/* /*
** TeCGraf - PUC-Rio ** 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 #ifndef opcode_h
@@ -166,5 +166,6 @@ TObject *luaI_Address (lua_Object o);
void luaI_pushobject (TObject *o); void luaI_pushobject (TObject *o);
void luaI_gcIM (TObject *o); void luaI_gcIM (TObject *o);
int luaI_dorun (TFunc *tf); int luaI_dorun (TFunc *tf);
int lua_domain (void);
#endif #endif
+20 -32
View File
@@ -3,7 +3,7 @@
** String library to LUA ** 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 <string.h>
#include <stdio.h> #include <stdio.h>
@@ -69,28 +69,6 @@ static void addstr (char *s)
addnchar(s, strlen(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 ** Return the string length
@@ -106,14 +84,17 @@ static void str_len (void)
static void str_sub (void) static void str_sub (void)
{ {
char *s = luaL_check_string(1); char *s = luaL_check_string(1);
long l = strlen(s);
long start = (long)luaL_check_number(2); long start = (long)luaL_check_number(2);
long end = (long)luaL_opt_number(3, strlen(s)); long end = (long)luaL_opt_number(3, -1);
if (1 <= start && start <= end && end <= strlen(s)) { if (start < 0) start = l+start+1;
if (end < 0) end = l+end+1;
if (1 <= start && start <= end && end <= l) {
luaI_emptybuff(); luaI_emptybuff();
addnchar(s+start-1, end-start+1); addnchar(s+start-1, end-start+1);
lua_pushstring(luaI_addchar(0)); 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; return res;
} }
case ESC: case ESC:
if (isdigit((unsigned char)*(p+1))) { /* capture */ if (isdigit((unsigned char)(*(p+1)))) { /* capture */
int l = check_cap(*(p+1), level); int l = check_cap(*(p+1), level);
if (strncmp(capture[l].init, s, capture[l].len) == 0) { if (strncmp(capture[l].init, s, capture[l].len) == 0) {
/* return match(p+2, s+capture[l].len, level); */ /* 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)) { if (lua_isstring(newp)) {
char *news = lua_getstring(newp); char *news = lua_getstring(newp);
@@ -407,15 +388,22 @@ static void add_s (lua_Object newp)
else if (lua_isfunction(newp)) { else if (lua_isfunction(newp)) {
lua_Object res; lua_Object res;
struct lbuff oldbuff; struct lbuff oldbuff;
int status;
lua_beginblock(); lua_beginblock();
if (lua_istable(table)) {
lua_pushobject(table);
lua_pushnumber(n);
}
push_captures(); push_captures();
/* function may use lbuffer, so save it and create a new one */ /* function may use lbuffer, so save it and create a new one */
oldbuff = lbuffer; oldbuff = lbuffer;
lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0; lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0;
lua_callfunction(newp); status = lua_callfunction(newp);
/* restore old buffer */ /* restore old buffer */
free(lbuffer.b); free(lbuffer.b);
lbuffer = oldbuff; lbuffer = oldbuff;
if (status != 0)
lua_error(NULL);
res = lua_getresult(1); res = lua_getresult(1);
addstr(lua_isstring(res) ? lua_getstring(res) : ""); addstr(lua_isstring(res) ? lua_getstring(res) : "");
lua_endblock(); lua_endblock();
@@ -428,15 +416,16 @@ static void str_gsub (void)
char *src = luaL_check_string(1); char *src = luaL_check_string(1);
char *p = luaL_check_string(2); char *p = luaL_check_string(2);
lua_Object newp = lua_getparam(3); 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 anchor = (*p == '^') ? (p++, 1) : 0;
int n = 0; int n = 0;
luaI_emptybuff(); luaI_emptybuff();
while (n < max_s) { while (n < max_s) {
char *e = match(src, p, 0); char *e = match(src, p, 0);
if (e) { if (e) {
add_s(newp);
n++; n++;
add_s(newp, table, n);
} }
if (e && e>src) /* non empty match? */ if (e && e>src) /* non empty match? */
src = e; /* skip it */ src = e; /* skip it */
@@ -524,7 +513,6 @@ static void str_format (void)
static struct luaL_reg strlib[] = { static struct luaL_reg strlib[] = {
{"strtok", str_tok},
{"strlen", str_len}, {"strlen", str_len},
{"strsub", str_sub}, {"strsub", str_sub},
{"strset", str_set}, {"strset", str_set},
+39 -25
View File
@@ -3,10 +3,11 @@
** Module to control static tables ** 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 "luamem.h"
#include "auxlib.h" #include "auxlib.h"
#include "func.h"
#include "opcode.h" #include "opcode.h"
#include "tree.h" #include "tree.h"
#include "hash.h" #include "hash.h"
@@ -28,7 +29,7 @@ Word lua_nconstant = 0;
static Long lua_maxconstant = 0; static Long lua_maxconstant = 0;
#define GARBAGE_BLOCK 50 #define GARBAGE_BLOCK 100
void luaI_initsymbol (void) void luaI_initsymbol (void)
@@ -58,17 +59,17 @@ void luaI_initconstant (void)
*/ */
Word luaI_findsymbol (TaggedString *t) Word luaI_findsymbol (TaggedString *t)
{ {
if (t->varindex == NOT_USED) if (t->u.s.varindex == NOT_USED)
{ {
if (lua_ntable == lua_maxsymbol) if (lua_ntable == lua_maxsymbol)
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol, lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
symbolEM, MAX_WORD); symbolEM, MAX_WORD);
t->varindex = lua_ntable; t->u.s.varindex = lua_ntable;
lua_table[lua_ntable].varname = t; lua_table[lua_ntable].varname = t;
s_ttype(lua_ntable) = LUA_T_NIL; s_ttype(lua_ntable) = LUA_T_NIL;
lua_ntable++; lua_ntable++;
} }
return t->varindex; return t->u.s.varindex;
} }
@@ -84,16 +85,16 @@ Word luaI_findsymbolbyname (char *name)
*/ */
Word luaI_findconstant (TaggedString *t) Word luaI_findconstant (TaggedString *t)
{ {
if (t->constindex == NOT_USED) if (t->u.s.constindex == NOT_USED)
{ {
if (lua_nconstant == lua_maxconstant) if (lua_nconstant == lua_maxconstant)
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *, lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
constantEM, MAX_WORD); constantEM, MAX_WORD);
t->constindex = lua_nconstant; t->u.s.constindex = lua_nconstant;
lua_constant[lua_nconstant] = t; lua_constant[lua_nconstant] = t;
lua_nconstant++; lua_nconstant++;
} }
return t->constindex; return t->u.s.constindex;
} }
@@ -153,7 +154,7 @@ int luaI_ismarked (TObject *o)
{ {
switch (o->ttype) switch (o->ttype)
{ {
case LUA_T_STRING: case LUA_T_STRING: case LUA_T_USERDATA:
return o->value.ts->marked; return o->value.ts->marked;
case LUA_T_FUNCTION: case LUA_T_FUNCTION:
return o->value.tf->marked; return o->value.tf->marked;
@@ -176,32 +177,45 @@ static void call_nilIM (void)
** Garbage collection. ** Garbage collection.
** Delete all unused strings and arrays. ** 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_travstack(lua_markobject); /* mark stack objects */
lua_travsymbol(lua_markobject); /* mark symbol table objects */ lua_travsymbol(lua_markobject); /* mark symbol table objects */
luaI_travlock(lua_markobject); /* mark locked objects */ luaI_travlock(lua_markobject); /* mark locked objects */
luaI_travfallbacks(lua_markobject); /* mark fallbacks */ 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(); luaI_invalidaterefs();
recovered += lua_strcollector(); freetable = luaI_hashcollector(&recovered);
recovered += lua_hashcollector(); freestr = luaI_strcollector(&recovered);
recovered += luaI_funccollector(); 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; return recovered;
} }
void lua_pack (void) void lua_pack (void)
{ {
static unsigned long block = GARBAGE_BLOCK; if (++gc_nentity >= gc_block)
static unsigned long nentity = 0; /* total of strings, arrays, etc */ lua_collectgarbage(0);
unsigned long recovered = 0;
if (nentity++ < block) return;
recovered = luaI_collectgarbage();
block = 2*(block-recovered);
nentity -= recovered;
} }
@@ -244,7 +258,7 @@ char *lua_getobjname (lua_Object o, char **name)
{ /* try to find a name for given function */ { /* try to find a name for given function */
functofind = luaI_Address(o); functofind = luaI_Address(o);
if ((*name = luaI_travfallbacks(checkfunc)) != NULL) if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
return "fallback"; return "tag-method";
else if ((*name = lua_travsymbol(checkfunc)) != NULL) else if ((*name = lua_travsymbol(checkfunc)) != NULL)
return "global"; return "global";
else return ""; else return "";
+1 -2
View File
@@ -1,7 +1,7 @@
/* /*
** Module to control static tables ** Module to control static tables
** TeCGraf - PUC-Rio ** 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 #ifndef table_h
@@ -33,7 +33,6 @@ void luaI_nextvar (void);
TaggedString *luaI_createfixedstring (char *str); TaggedString *luaI_createfixedstring (char *str);
int lua_markobject (TObject *o); int lua_markobject (TObject *o);
int luaI_ismarked (TObject *o); int luaI_ismarked (TObject *o);
Long luaI_collectgarbage (void);
void lua_pack (void); void lua_pack (void);
+75 -38
View File
@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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> #include <string.h>
@@ -29,20 +29,39 @@ static int initialized = 0;
static stringtable string_root[NUM_HASHS]; 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; unsigned long h;
while (size--) if (tag != LUA_T_STRING)
h = ((h<<5)-h)^(unsigned char)*(buff++); h = (unsigned long)s;
else {
h = 0;
while (*s)
h = ((h<<5)-h)^(unsigned char)*(s++);
}
return h; 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) static void initialize (void)
{ {
initialized = 1; initialized = 1;
luaI_inittree();
luaI_addReserved(); luaI_addReserved();
luaI_initsymbol(); luaI_initsymbol();
luaI_initconstant(); luaI_initconstant();
@@ -60,8 +79,7 @@ static void grow (stringtable *tb)
/* rehash */ /* rehash */
tb->nuse = 0; tb->nuse = 0;
for (i=0; i<tb->size; i++) 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; int h = tb->hash[i]->hash%newsize;
while (newhash[h]) while (newhash[h])
h = (h+1)%newsize; h = (h+1)%newsize;
@@ -73,10 +91,30 @@ static void grow (stringtable *tb)
tb->hash = newhash; 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; 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 i;
int j = -1; int j = -1;
if ((Long)tb->nuse*3 >= (Long)tb->size*2) 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) if (ts == &EMPTY)
j = i; j = i;
else if (ts->size == size && ts->tag == tag && else if ((ts->tag == LUA_T_STRING) ?
memcmp(buff, ts->str, size) == 0) (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
return ts; return ts;
i = (i+1)%tb->size; i = (i+1)%tb->size;
} }
@@ -101,53 +140,49 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
i = j; i = j;
else else
tb->nuse++; tb->nuse++;
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1); ts = tb->hash[i] = newone(buff, tag, h);
memcpy(ts->str, buff, size);
ts->tag = tag;
ts->size = size;
ts->marked = 0;
ts->hash = h;
ts->varindex = ts->constindex = NOT_USED;
return ts; 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) 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; TObject o;
ttype(&o) = LUA_T_USERDATA; ttype(&o) = LUA_T_USERDATA;
for (i=0; i<NUM_HASHS; i++) { for (; l; l=l->next) {
stringtable *tb = &string_root[i]; tsvalue(&o) = l;
int j; luaI_gcIM(&o);
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); void luaI_strfree (TaggedString *l)
} {
} while (l) {
TaggedString *next = l->next;
luaI_free(l);
l = next;
} }
} }
/* /*
** Garbage collection function. ** Garbage collection function.
** This function traverse the string list freeing unindexed strings
*/ */
Long lua_strcollector (void) TaggedString *luaI_strcollector (long *acum)
{ {
Long counter = 0; Long counter = 0;
TaggedString *frees = NULL;
int i; int i;
for (i=0; i<NUM_HASHS; i++) for (i=0; i<NUM_HASHS; i++)
{ {
@@ -162,13 +197,15 @@ Long lua_strcollector (void)
t->marked = 0; t->marked = 0;
else else
{ {
luaI_free(t); t->next = frees;
frees = t;
tb->hash[j] = &EMPTY; tb->hash[j] = &EMPTY;
counter++; counter++;
} }
} }
} }
} }
return counter; *acum += counter;
return frees;
} }
+14 -8
View File
@@ -1,7 +1,7 @@
/* /*
** tree.h ** tree.h
** TecCGraf - PUC-Rio ** 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 #ifndef tree_h
@@ -15,18 +15,24 @@
typedef struct TaggedString typedef struct TaggedString
{ {
int tag; /* if != LUA_T_STRING, this is a userdata */ int tag; /* if != LUA_T_STRING, this is a userdata */
long size; struct TaggedString *next;
Word varindex; /* != NOT_USED if this is a symbol */ union {
Word constindex; /* != NOT_USED if this is a constant */ 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 */ unsigned long hash; /* 0 if not initialized */
int marked; /* for garbage collection; never collect (nor change) if > 1 */ 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;
TaggedString *lua_createstring (char *str); TaggedString *lua_createstring (char *str);
TaggedString *luaI_createuserdata (char *buff, long size, int tag); TaggedString *luaI_createudata (void *udata, int tag);
Long lua_strcollector (void); TaggedString *luaI_strcollector (long *cont);
void luaI_strcallIM (void); void luaI_strfree (TaggedString *l);
void luaI_strcallIM (TaggedString *l);
#endif #endif
+51 -54
View File
@@ -3,7 +3,7 @@
** load bytecodes from files ** 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 <stdio.h>
#include <string.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 "luamem.h"
#include "table.h" #include "table.h"
#include "undump.h" #include "undump.h"
#include "zio.h"
static int swapword=0; static int swapword=0;
static int swapfloat=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; Word w;
fread(&w,sizeof(w),1,D); zread(Z,&w,sizeof(w));
if (swapword) if (swapword)
{ {
Byte* p=(Byte*)&w; Byte* p=(Byte*)&w;
@@ -160,101 +161,101 @@ static int LoadWord(FILE* D)
return w; return w;
} }
static int LoadSize(FILE* D) static int LoadSize(ZIO* Z)
{ {
Word hi=LoadWord(D); Word hi=LoadWord(Z);
Word lo=LoadWord(D); Word lo=LoadWord(Z);
int s=(hi<<16)|lo; int s=(hi<<16)|lo;
if ((Word)s != s) lua_error("code too long"); if ((Word)s != s) lua_error("code too long");
return s; return s;
} }
static void* LoadBlock(int size, FILE* D) static void* LoadBlock(int size, ZIO* Z)
{ {
void* b=luaI_malloc(size); void* b=luaI_malloc(size);
fread(b,size,1,D); zread(Z,b,size);
return b; 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); char *b=luaI_buffer(size);
fread(b,size,1,D); zread(Z,b,size);
return b; 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); TFunc* tf=new(TFunc);
tf->next=NULL; tf->next=NULL;
tf->locvars=NULL; tf->locvars=NULL;
tf->size=LoadSize(D); tf->size=LoadSize(Z);
tf->lineDefined=LoadWord(D); tf->lineDefined=LoadWord(Z);
if (IsMain(tf)) /* new main */ if (IsMain(tf)) /* new main */
{ {
tf->fileName=LoadNewString(D); tf->fileName=LoadNewString(Z);
Main=lastF=tf; Main=lastF=tf;
} }
else /* fix PUSHFUNCTION */ else /* fix PUSHFUNCTION */
{ {
tf->marked=LoadWord(D); tf->marked=LoadWord(Z);
tf->fileName=Main->fileName; tf->fileName=Main->fileName;
memcpy(Main->code+tf->marked,&tf,sizeof(tf)); memcpy(Main->code+tf->marked,&tf,sizeof(tf));
lastF=lastF->next=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); if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
while (1) /* unthread */ while (1) /* unthread */
{ {
int c=getc(D); int c=zgetc(Z);
if (c==ID_VAR) /* global var */ if (c==ID_VAR) /* global var */
{ {
int i=LoadWord(D); int i=LoadWord(Z);
char* s=LoadString(D); char* s=LoadString(Z);
int v=luaI_findsymbolbyname(s); int v=luaI_findsymbolbyname(s);
Unthread(tf->code,i,v); Unthread(tf->code,i,v);
} }
else if (c==ID_STR) /* constant string */ else if (c==ID_STR) /* constant string */
{ {
int i=LoadWord(D); int i=LoadWord(Z);
char* s=LoadString(D); char* s=LoadString(Z);
int v=luaI_findconstantbyname(s); int v=luaI_findconstantbyname(s);
Unthread(tf->code,i,v); Unthread(tf->code,i,v);
} }
else else
{ {
ungetc(c,D); zungetc(Z);
break; break;
} }
} }
} }
static void LoadSignature(FILE* D) static void LoadSignature(ZIO* Z)
{ {
char* s=SIGNATURE; char* s=SIGNATURE;
while (*s!=0 && getc(D)==*s) while (*s!=0 && zgetc(Z)==*s)
++s; ++s;
if (*s!=0) lua_error("cannot load binary file: bad signature"); 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; Word w,tw=TEST_WORD;
float f,tf=TEST_FLOAT; float f,tf=TEST_FLOAT;
int version; int version;
LoadSignature(D); LoadSignature(Z);
version=getc(D); version=zgetc(Z);
if (version>0x23) /* after 2.5 */ if (version>0x23) /* after 2.5 */
{ {
int oldsizeofW=getc(D); int oldsizeofW=zgetc(Z);
int oldsizeofF=getc(D); int oldsizeofF=zgetc(Z);
int oldsizeofP=getc(D); int oldsizeofP=zgetc(Z);
if (oldsizeofW!=2) if (oldsizeofW!=2)
luaL_verror( luaL_verror(
"cannot load binary file created on machine with sizeof(Word)=%d; " "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? */ if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
luaL_verror( luaL_verror(
"cannot load binary file created on machine with sizeof(TFunc*)=%d; " "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) if (w!=tw)
{ {
swapword=1; swapword=1;
} }
fread(&f,sizeof(f),1,D); /* test float */ zread(Z,&f,sizeof(f)); /* test float */
if (f!=tf) if (f!=tf)
{ {
Byte* p=(Byte*)&f; 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) while (1)
{ {
int c=getc(D); int c=zgetc(Z);
if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; } 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. ** load one chunk from a file.
** return list of functions found, headed by main, or NULL at EOF. ** 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); LoadChunk(Z);
if (c==ID_CHUNK) return Main;
{
LoadChunk(D);
return Main;
}
else if (c==EOF)
return NULL;
else
lua_error("not a lua binary file");
} }
else if (c!=EOZ)
lua_error("not a lua binary file");
return NULL;
} }
/* /*
** load and run all chunks in a file ** load and run all chunks in a file
*/ */
int luaI_undump(FILE* D) int luaI_undump(ZIO* Z)
{ {
TFunc* m; TFunc* m;
while ((m=luaI_undump1(D))) while ((m=luaI_undump1(Z)))
{ {
int status=luaI_dorun(m); int status=luaI_dorun(m);
luaI_freefunc(m); luaI_freefunc(m);
+10 -3
View File
@@ -1,10 +1,14 @@
/* /*
** undump.h ** undump.h
** definitions for lua decompiler ** 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 "func.h"
#include "zio.h"
#define IsMain(f) (f->lineDefined==0) #define IsMain(f) (f->lineDefined==0)
@@ -19,5 +23,8 @@
#define TEST_WORD 0x1234 /* a word for testing byte ordering */ #define TEST_WORD 0x1234 /* a word for testing byte ordering */
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */ #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
View 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
View 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