diff options
| -rw-r--r-- | lglobal.c | 71 | ||||
| -rw-r--r-- | lglobal.h | 35 | ||||
| -rw-r--r-- | table.c | 251 | ||||
| -rw-r--r-- | table.h | 37 |
4 files changed, 106 insertions, 288 deletions
diff --git a/lglobal.c b/lglobal.c new file mode 100644 index 00000000..9f2b9f90 --- /dev/null +++ b/lglobal.c | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: $ | ||
| 3 | ** Global variables | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <stdlib.h> | ||
| 8 | |||
| 9 | #include "lbuiltin.h" | ||
| 10 | #include "lglobal.h" | ||
| 11 | #include "lmem.h" | ||
| 12 | #include "lobject.h" | ||
| 13 | #include "lstring.h" | ||
| 14 | |||
| 15 | |||
| 16 | Symbol *luaG_global = NULL; | ||
| 17 | int luaG_nglobal = 0; | ||
| 18 | static int maxglobal = 0; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | Word luaG_findsymbol (TaggedString *t) | ||
| 23 | { | ||
| 24 | if (maxglobal == 0) { /* first time? */ | ||
| 25 | maxglobal = 50; | ||
| 26 | luaG_global = luaM_newvector(maxglobal, Symbol); | ||
| 27 | luaB_predefine(); | ||
| 28 | } | ||
| 29 | if (t->u.s.varindex == NOT_USED) { | ||
| 30 | if (!t->marked) t->marked = 2; /* avoid GC of global variable names */ | ||
| 31 | if (luaG_nglobal >= maxglobal) | ||
| 32 | maxglobal = luaM_growvector(&luaG_global, maxglobal, Symbol, | ||
| 33 | symbolEM, MAX_WORD); | ||
| 34 | t->u.s.varindex = luaG_nglobal; | ||
| 35 | luaG_global[luaG_nglobal].varname = t; | ||
| 36 | s_ttype(luaG_nglobal) = LUA_T_NIL; | ||
| 37 | luaG_nglobal++; | ||
| 38 | } | ||
| 39 | return t->u.s.varindex; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | Word luaG_findsymbolbyname (char *name) | ||
| 44 | { | ||
| 45 | return luaG_findsymbol(luaS_new(name)); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | int luaG_globaldefined (char *name) | ||
| 50 | { | ||
| 51 | return s_ttype(luaG_findsymbolbyname(name)) != LUA_T_NIL; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | int luaG_nextvar (Word next) | ||
| 56 | { | ||
| 57 | while (next < luaG_nglobal && s_ttype(next) == LUA_T_NIL) | ||
| 58 | next++; | ||
| 59 | return (next < luaG_nglobal ? next : -1); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | char *luaG_travsymbol (int (*fn)(TObject *)) | ||
| 64 | { | ||
| 65 | int i; | ||
| 66 | for (i=0; i<luaG_nglobal; i++) | ||
| 67 | if (fn(&s_object(i))) | ||
| 68 | return luaG_global[i].varname->str; | ||
| 69 | return NULL; | ||
| 70 | } | ||
| 71 | |||
diff --git a/lglobal.h b/lglobal.h new file mode 100644 index 00000000..00edb4c6 --- /dev/null +++ b/lglobal.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: $ | ||
| 3 | ** Global variables | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef lglobal_h | ||
| 8 | #define lglobal_h | ||
| 9 | |||
| 10 | |||
| 11 | #include "lobject.h" | ||
| 12 | |||
| 13 | |||
| 14 | typedef struct { | ||
| 15 | TObject object; | ||
| 16 | TaggedString *varname; | ||
| 17 | } Symbol; | ||
| 18 | |||
| 19 | |||
| 20 | extern Symbol *luaG_global; /* global variables */ | ||
| 21 | extern int luaG_nglobal; /* number of global variable (for luac) */ | ||
| 22 | |||
| 23 | |||
| 24 | Word luaG_findsymbolbyname (char *name); | ||
| 25 | Word luaG_findsymbol (TaggedString *t); | ||
| 26 | int luaG_globaldefined (char *name); | ||
| 27 | int luaG_nextvar (Word next); | ||
| 28 | char *luaG_travsymbol (int (*fn)(TObject *)); | ||
| 29 | |||
| 30 | |||
| 31 | #define s_object(i) (luaG_global[i].object) | ||
| 32 | #define s_ttype(i) (ttype(&s_object(i))) | ||
| 33 | |||
| 34 | |||
| 35 | #endif | ||
diff --git a/table.c b/table.c deleted file mode 100644 index 7420f68e..00000000 --- a/table.c +++ /dev/null | |||
| @@ -1,251 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ** table.c | ||
| 3 | ** Module to control static tables | ||
| 4 | */ | ||
| 5 | |||
| 6 | char *rcs_table="$Id: table.c,v 2.73 1997/07/07 16:44:26 roberto Exp roberto $"; | ||
| 7 | |||
| 8 | #include "luamem.h" | ||
| 9 | #include "auxlib.h" | ||
| 10 | #include "func.h" | ||
| 11 | #include "opcode.h" | ||
| 12 | #include "tree.h" | ||
| 13 | #include "hash.h" | ||
| 14 | #include "table.h" | ||
| 15 | #include "inout.h" | ||
| 16 | #include "lua.h" | ||
| 17 | #include "fallback.h" | ||
| 18 | #include "luadebug.h" | ||
| 19 | |||
| 20 | |||
| 21 | #define BUFFER_BLOCK 256 | ||
| 22 | |||
| 23 | Symbol *lua_table = NULL; | ||
| 24 | Word lua_ntable = 0; | ||
| 25 | static Long lua_maxsymbol = 0; | ||
| 26 | |||
| 27 | |||
| 28 | #define GARBAGE_BLOCK 100 | ||
| 29 | |||
| 30 | |||
| 31 | static TaggedString *luaI_createfixedstring (char *name) | ||
| 32 | { | ||
| 33 | TaggedString *ts = luaI_createstring(name); | ||
| 34 | luaI_fixstring(ts); | ||
| 35 | return ts; | ||
| 36 | } | ||
| 37 | |||
| 38 | void luaI_initsymbol (void) | ||
| 39 | { | ||
| 40 | lua_maxsymbol = BUFFER_BLOCK; | ||
| 41 | lua_table = newvector(lua_maxsymbol, Symbol); | ||
| 42 | luaI_predefine(); | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | void luaI_initconstant (void) | ||
| 47 | { | ||
| 48 | /* pre-register mem error messages, to avoid loop when error arises */ | ||
| 49 | luaI_createfixedstring(tableEM); | ||
| 50 | luaI_createfixedstring(memEM); | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | /* | ||
| 55 | ** Given a name, search it at symbol table and return its index. If not | ||
| 56 | ** found, allocate it. | ||
| 57 | */ | ||
| 58 | Word luaI_findsymbol (TaggedString *t) | ||
| 59 | { | ||
| 60 | if (t->u.s.varindex == NOT_USED) | ||
| 61 | { | ||
| 62 | if (lua_ntable == lua_maxsymbol) | ||
| 63 | lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol, | ||
| 64 | symbolEM, MAX_WORD); | ||
| 65 | t->u.s.varindex = lua_ntable; | ||
| 66 | lua_table[lua_ntable].varname = t; | ||
| 67 | s_ttype(lua_ntable) = LUA_T_NIL; | ||
| 68 | lua_ntable++; | ||
| 69 | } | ||
| 70 | return t->u.s.varindex; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | Word luaI_findsymbolbyname (char *name) | ||
| 75 | { | ||
| 76 | return luaI_findsymbol(luaI_createfixedstring(name)); | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | void luaI_releasestring (TaggedString *t) | ||
| 81 | { | ||
| 82 | if (t->marked == 2) /* string has temporary mark? */ | ||
| 83 | t->marked = 0; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | void luaI_fixstring (TaggedString *t) | ||
| 88 | { | ||
| 89 | if (t->marked < 3) | ||
| 90 | t->marked = 3; /* avoid GC */ | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | TaggedString *luaI_createtempstring (char *name) | ||
| 95 | { | ||
| 96 | TaggedString *ts = luaI_createstring(name); | ||
| 97 | if (!ts->marked) | ||
| 98 | ts->marked = 2; /* avoid (temporarily) GC */ | ||
| 99 | return ts; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | int luaI_globaldefined (char *name) | ||
| 104 | { | ||
| 105 | return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | /* | ||
| 110 | ** Traverse symbol table objects | ||
| 111 | */ | ||
| 112 | static char *lua_travsymbol (int (*fn)(TObject *)) | ||
| 113 | { | ||
| 114 | Word i; | ||
| 115 | for (i=0; i<lua_ntable; i++) | ||
| 116 | if (fn(&s_object(i))) | ||
| 117 | return lua_table[i].varname->str; | ||
| 118 | return NULL; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | int lua_markobject (TObject *o) | ||
| 123 | {/* if already marked, does not change mark value */ | ||
| 124 | if (ttype(o) == LUA_T_USERDATA || | ||
| 125 | (ttype(o) == LUA_T_STRING && !tsvalue(o)->marked)) | ||
| 126 | tsvalue(o)->marked = 1; | ||
| 127 | else if (ttype(o) == LUA_T_ARRAY) | ||
| 128 | lua_hashmark (avalue(o)); | ||
| 129 | else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK) | ||
| 130 | && !o->value.tf->marked) | ||
| 131 | luaI_funcmark(o->value.tf); | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | |||
| 135 | /* | ||
| 136 | * returns 0 if the object is going to be (garbage) collected | ||
| 137 | */ | ||
| 138 | int luaI_ismarked (TObject *o) | ||
| 139 | { | ||
| 140 | switch (o->ttype) | ||
| 141 | { | ||
| 142 | case LUA_T_STRING: case LUA_T_USERDATA: | ||
| 143 | return o->value.ts->marked; | ||
| 144 | case LUA_T_FUNCTION: | ||
| 145 | return o->value.tf->marked; | ||
| 146 | case LUA_T_ARRAY: | ||
| 147 | return o->value.a->mark; | ||
| 148 | default: /* nil, number, cfunction, or user data */ | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | static void call_nilIM (void) | ||
| 155 | { /* signals end of garbage collection */ | ||
| 156 | TObject t; | ||
| 157 | ttype(&t) = LUA_T_NIL; | ||
| 158 | luaI_gcIM(&t); /* end of list */ | ||
| 159 | } | ||
| 160 | |||
| 161 | /* | ||
| 162 | ** Garbage collection. | ||
| 163 | ** Delete all unused strings and arrays. | ||
| 164 | */ | ||
| 165 | static long gc_block = GARBAGE_BLOCK; | ||
| 166 | static long gc_nentity = 0; /* total of strings, arrays, etc */ | ||
| 167 | |||
| 168 | static void markall (void) | ||
| 169 | { | ||
| 170 | lua_travstack(lua_markobject); /* mark stack objects */ | ||
| 171 | lua_travsymbol(lua_markobject); /* mark symbol table objects */ | ||
| 172 | luaI_travlock(lua_markobject); /* mark locked objects */ | ||
| 173 | luaI_travfallbacks(lua_markobject); /* mark fallbacks */ | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | long lua_collectgarbage (long limit) | ||
| 178 | { | ||
| 179 | long recovered = 0; | ||
| 180 | Hash *freetable; | ||
| 181 | TaggedString *freestr; | ||
| 182 | TFunc *freefunc; | ||
| 183 | markall(); | ||
| 184 | luaI_invalidaterefs(); | ||
| 185 | freetable = luaI_hashcollector(&recovered); | ||
| 186 | freestr = luaI_strcollector(&recovered); | ||
| 187 | freefunc = luaI_funccollector(&recovered); | ||
| 188 | gc_nentity -= recovered; | ||
| 189 | gc_block = (limit == 0) ? 2*(gc_block-recovered) : gc_nentity+limit; | ||
| 190 | luaI_hashcallIM(freetable); | ||
| 191 | luaI_strcallIM(freestr); | ||
| 192 | call_nilIM(); | ||
| 193 | luaI_hashfree(freetable); | ||
| 194 | luaI_strfree(freestr); | ||
| 195 | luaI_funcfree(freefunc); | ||
| 196 | /*printf("total %d coletados %d\n", (int)gc_nentity, (int)recovered);*/ | ||
| 197 | return recovered; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | void lua_pack (void) | ||
| 202 | { | ||
| 203 | if (++gc_nentity >= gc_block) | ||
| 204 | lua_collectgarbage(0); | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | /* | ||
| 209 | ** Internal function: return next global variable | ||
| 210 | */ | ||
| 211 | void luaI_nextvar (void) | ||
| 212 | { | ||
| 213 | Word next = lua_isnil(lua_getparam(1)) ? 0 : | ||
| 214 | luaI_findsymbolbyname(luaL_check_string(1))+1; | ||
| 215 | if (next != 0) | ||
| 216 | luaL_arg_check(s_ttype(next-1)!=LUA_T_NIL, 1, "undefined global name"); | ||
| 217 | while (next < lua_ntable && s_ttype(next) == LUA_T_NIL) | ||
| 218 | next++; | ||
| 219 | if (next < lua_ntable) { | ||
| 220 | lua_pushstring(lua_table[next].varname->str); | ||
| 221 | luaI_pushobject(&s_object(next)); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | static TObject *functofind; | ||
| 227 | static int checkfunc (TObject *o) | ||
| 228 | { | ||
| 229 | if (o->ttype == LUA_T_FUNCTION) | ||
| 230 | return | ||
| 231 | ((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK) | ||
| 232 | && (functofind->value.tf == o->value.tf)); | ||
| 233 | if (o->ttype == LUA_T_CFUNCTION) | ||
| 234 | return | ||
| 235 | ((functofind->ttype == LUA_T_CFUNCTION || | ||
| 236 | functofind->ttype == LUA_T_CMARK) && | ||
| 237 | (functofind->value.f == o->value.f)); | ||
| 238 | return 0; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | char *lua_getobjname (lua_Object o, char **name) | ||
| 243 | { /* try to find a name for given function */ | ||
| 244 | functofind = luaI_Address(o); | ||
| 245 | if ((*name = luaI_travfallbacks(checkfunc)) != NULL) | ||
| 246 | return "tag-method"; | ||
| 247 | else if ((*name = lua_travsymbol(checkfunc)) != NULL) | ||
| 248 | return "global"; | ||
| 249 | else return ""; | ||
| 250 | } | ||
| 251 | |||
diff --git a/table.h b/table.h deleted file mode 100644 index 93d549f9..00000000 --- a/table.h +++ /dev/null | |||
| @@ -1,37 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ** Module to control static tables | ||
| 3 | ** TeCGraf - PUC-Rio | ||
| 4 | ** $Id: table.h,v 2.25 1997/05/26 14:42:36 roberto Exp roberto $ | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef table_h | ||
| 8 | #define table_h | ||
| 9 | |||
| 10 | #include "tree.h" | ||
| 11 | #include "opcode.h" | ||
| 12 | |||
| 13 | typedef struct | ||
| 14 | { | ||
| 15 | TObject object; | ||
| 16 | TaggedString *varname; | ||
| 17 | } Symbol; | ||
| 18 | |||
| 19 | |||
| 20 | extern Symbol *lua_table; | ||
| 21 | extern Word lua_ntable; | ||
| 22 | |||
| 23 | void luaI_initsymbol (void); | ||
| 24 | void luaI_initconstant (void); | ||
| 25 | Word luaI_findsymbolbyname (char *name); | ||
| 26 | Word luaI_findsymbol (TaggedString *t); | ||
| 27 | int luaI_globaldefined (char *name); | ||
| 28 | void luaI_nextvar (void); | ||
| 29 | TaggedString *luaI_createtempstring (char *name); | ||
| 30 | void luaI_releasestring (TaggedString *t); | ||
| 31 | void luaI_fixstring (TaggedString *t); | ||
| 32 | int lua_markobject (TObject *o); | ||
| 33 | int luaI_ismarked (TObject *o); | ||
| 34 | void lua_pack (void); | ||
| 35 | |||
| 36 | |||
| 37 | #endif | ||
