diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | b8a049abed1f021009930a3030b484e70f89f001 (patch) | |
tree | 20d636a16f78b3a4ff357de97d365a8af4ebbdfb /table.c | |
parent | e18f6813330ad141cdac86d194175e75b418df2d (diff) | |
download | lua-b8a049abed1f021009930a3030b484e70f89f001.tar.gz lua-b8a049abed1f021009930a3030b484e70f89f001.tar.bz2 lua-b8a049abed1f021009930a3030b484e70f89f001.zip |
Global variables
Diffstat (limited to 'table.c')
-rw-r--r-- | table.c | 251 |
1 files changed, 0 insertions, 251 deletions
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 | |||