diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-26 12:02:26 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-26 12:02:26 -0300 |
| commit | a580480b07cdf7201306b246deeb2fe84f2c25a9 (patch) | |
| tree | 30e9d4798228156eea5be2589834f1ff2db4355e | |
| parent | 0dd6d1080e7f58eb17cb8a2ad3fc5801ed7c0532 (diff) | |
| download | lua-a580480b07cdf7201306b246deeb2fe84f2c25a9.tar.gz lua-a580480b07cdf7201306b246deeb2fe84f2c25a9.tar.bz2 lua-a580480b07cdf7201306b246deeb2fe84f2c25a9.zip | |
new implementation for globals: Global value is stored in TaggedString
| -rw-r--r-- | lapi.c | 22 | ||||
| -rw-r--r-- | lauxlib.c | 11 | ||||
| -rw-r--r-- | lauxlib.h | 3 | ||||
| -rw-r--r-- | lbuiltin.c | 76 | ||||
| -rw-r--r-- | ldo.c | 4 | ||||
| -rw-r--r-- | lgc.c | 31 | ||||
| -rw-r--r-- | lglobal.c | 71 | ||||
| -rw-r--r-- | lglobal.h | 35 | ||||
| -rw-r--r-- | llex.c | 11 | ||||
| -rw-r--r-- | lobject.h | 113 | ||||
| -rw-r--r-- | lstring.c | 84 | ||||
| -rw-r--r-- | lstring.h | 6 | ||||
| -rw-r--r-- | ltm.c | 4 | ||||
| -rw-r--r-- | lua.stx | 8 | ||||
| -rw-r--r-- | lvm.c | 21 | ||||
| -rw-r--r-- | lvm.h | 6 | ||||
| -rw-r--r-- | makefile | 33 |
17 files changed, 251 insertions, 288 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.1 1997/08/14 13:40:46 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -13,7 +13,6 @@ | |||
| 13 | #include "ldo.h" | 13 | #include "ldo.h" |
| 14 | #include "lfunc.h" | 14 | #include "lfunc.h" |
| 15 | #include "lgc.h" | 15 | #include "lgc.h" |
| 16 | #include "lglobal.h" | ||
| 17 | #include "lmem.h" | 16 | #include "lmem.h" |
| 18 | #include "lobject.h" | 17 | #include "lobject.h" |
| 19 | #include "lstring.h" | 18 | #include "lstring.h" |
| @@ -182,14 +181,15 @@ lua_Object lua_createtable (void) | |||
| 182 | lua_Object lua_getglobal (char *name) | 181 | lua_Object lua_getglobal (char *name) |
| 183 | { | 182 | { |
| 184 | luaD_checkstack(2); /* may need that to call T.M. */ | 183 | luaD_checkstack(2); /* may need that to call T.M. */ |
| 185 | luaV_getglobal(luaG_findsymbolbyname(name)); | 184 | luaV_getglobal(luaS_new(name)); |
| 186 | return put_luaObjectonTop(); | 185 | return put_luaObjectonTop(); |
| 187 | } | 186 | } |
| 188 | 187 | ||
| 189 | 188 | ||
| 190 | lua_Object lua_rawgetglobal (char *name) | 189 | lua_Object lua_rawgetglobal (char *name) |
| 191 | { | 190 | { |
| 192 | return put_luaObject(&luaG_global[luaG_findsymbolbyname(name)].object); | 191 | TaggedString *ts = luaS_new(name); |
| 192 | return put_luaObject(&ts->u.globalval); | ||
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | 195 | ||
| @@ -197,15 +197,15 @@ void lua_setglobal (char *name) | |||
| 197 | { | 197 | { |
| 198 | checkCparams(1); | 198 | checkCparams(1); |
| 199 | luaD_checkstack(2); /* may need that to call T.M. */ | 199 | luaD_checkstack(2); /* may need that to call T.M. */ |
| 200 | luaV_setglobal(luaG_findsymbolbyname(name)); | 200 | luaV_setglobal(luaS_new(name)); |
| 201 | } | 201 | } |
| 202 | 202 | ||
| 203 | 203 | ||
| 204 | void lua_rawsetglobal (char *name) | 204 | void lua_rawsetglobal (char *name) |
| 205 | { | 205 | { |
| 206 | Word n = luaG_findsymbolbyname(name); | 206 | TaggedString *ts = luaS_new(name); |
| 207 | checkCparams(1); | 207 | checkCparams(1); |
| 208 | s_object(n) = *(--luaD_stack.top); | 208 | luaS_rawsetglobal(ts, --luaD_stack.top); |
| 209 | } | 209 | } |
| 210 | 210 | ||
| 211 | 211 | ||
| @@ -268,7 +268,7 @@ void *lua_getuserdata (lua_Object object) | |||
| 268 | { | 268 | { |
| 269 | if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) | 269 | if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) |
| 270 | return NULL; | 270 | return NULL; |
| 271 | else return tsvalue(Address(object))->u.v; | 271 | else return tsvalue(Address(object))->u.d.v; |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | lua_CFunction lua_getcfunction (lua_Object object) | 274 | lua_CFunction lua_getcfunction (lua_Object object) |
| @@ -352,7 +352,7 @@ int lua_tag (lua_Object lo) | |||
| 352 | TObject *o = Address(lo); | 352 | TObject *o = Address(lo); |
| 353 | lua_Type t = ttype(o); | 353 | lua_Type t = ttype(o); |
| 354 | if (t == LUA_T_USERDATA) | 354 | if (t == LUA_T_USERDATA) |
| 355 | return o->value.ts->tag; | 355 | return o->value.ts->u.d.tag; |
| 356 | else if (t == LUA_T_ARRAY) | 356 | else if (t == LUA_T_ARRAY) |
| 357 | return o->value.a->htag; | 357 | return o->value.a->htag; |
| 358 | else return t; | 358 | else return t; |
| @@ -369,7 +369,7 @@ void lua_settag (int tag) | |||
| 369 | (luaD_stack.top-1)->value.a->htag = tag; | 369 | (luaD_stack.top-1)->value.a->htag = tag; |
| 370 | break; | 370 | break; |
| 371 | case LUA_T_USERDATA: | 371 | case LUA_T_USERDATA: |
| 372 | (luaD_stack.top-1)->value.ts->tag = tag; | 372 | (luaD_stack.top-1)->value.ts->u.d.tag = tag; |
| 373 | break; | 373 | break; |
| 374 | default: | 374 | default: |
| 375 | luaL_verror("cannot change the tag of a %s", | 375 | luaL_verror("cannot change the tag of a %s", |
| @@ -483,7 +483,7 @@ char *lua_getobjname (lua_Object o, char **name) | |||
| 483 | functofind = Address(o); | 483 | functofind = Address(o); |
| 484 | if ((*name = luaT_travtagmethods(checkfunc)) != NULL) | 484 | if ((*name = luaT_travtagmethods(checkfunc)) != NULL) |
| 485 | return "tag-method"; | 485 | return "tag-method"; |
| 486 | else if ((*name = luaG_travsymbol(checkfunc)) != NULL) | 486 | else if ((*name = luaS_travsymbol(checkfunc)) != NULL) |
| 487 | return "global"; | 487 | return "global"; |
| 488 | else return ""; | 488 | else return ""; |
| 489 | } | 489 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: lauxlib.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Auxiliar functions for building Lua libraries | 3 | ** Auxiliar functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -57,6 +57,13 @@ double luaL_opt_number (int numArg, double def) | |||
| 57 | luaL_check_number(numArg); | 57 | luaL_check_number(numArg); |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | lua_Object luaL_nonnullarg (int numArg) | ||
| 61 | { | ||
| 62 | lua_Object o = lua_getparam(numArg); | ||
| 63 | luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected"); | ||
| 64 | return o; | ||
| 65 | } | ||
| 66 | |||
| 60 | void luaL_openlib (struct luaL_reg *l, int n) | 67 | void luaL_openlib (struct luaL_reg *l, int n) |
| 61 | { | 68 | { |
| 62 | int i; | 69 | int i; |
| @@ -74,3 +81,5 @@ void luaL_verror (char *fmt, ...) | |||
| 74 | va_end(argp); | 81 | va_end(argp); |
| 75 | lua_error(buff); | 82 | lua_error(buff); |
| 76 | } | 83 | } |
| 84 | |||
| 85 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: lauxlib.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Auxiliar functions for building Lua libraries | 3 | ** Auxiliar functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -23,6 +23,7 @@ char *luaL_check_string (int numArg); | |||
| 23 | char *luaL_opt_string (int numArg, char *def); | 23 | char *luaL_opt_string (int numArg, char *def); |
| 24 | double luaL_check_number (int numArg); | 24 | double luaL_check_number (int numArg); |
| 25 | double luaL_opt_number (int numArg, double def); | 25 | double luaL_opt_number (int numArg, double def); |
| 26 | lua_Object luaL_nonnullarg (int numArg); | ||
| 26 | void luaL_verror (char *fmt, ...); | 27 | void luaL_verror (char *fmt, ...); |
| 27 | 28 | ||
| 28 | 29 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: lbuiltin.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -10,7 +10,6 @@ | |||
| 10 | #include "lapi.h" | 10 | #include "lapi.h" |
| 11 | #include "lauxlib.h" | 11 | #include "lauxlib.h" |
| 12 | #include "lbuiltin.h" | 12 | #include "lbuiltin.h" |
| 13 | #include "lglobal.h" | ||
| 14 | #include "lmem.h" | 13 | #include "lmem.h" |
| 15 | #include "lstring.h" | 14 | #include "lstring.h" |
| 16 | #include "ltable.h" | 15 | #include "ltable.h" |
| @@ -18,13 +17,25 @@ | |||
| 18 | #include "lua.h" | 17 | #include "lua.h" |
| 19 | 18 | ||
| 20 | 19 | ||
| 20 | |||
| 21 | static void nextvar (void) | 21 | static void nextvar (void) |
| 22 | { | 22 | { |
| 23 | int i = luaG_nextvar(lua_isnil(lua_getparam(1)) ? 0 : | 23 | lua_Object v = luaL_nonnullarg(1); |
| 24 | luaG_findsymbolbyname(luaL_check_string(1))+1); | 24 | TaggedString *g; |
| 25 | if (i >= 0) { | 25 | if (lua_isnil(v)) |
| 26 | lua_pushstring(luaG_global[i].varname->str); | 26 | g = (TaggedString *)luaS_root.next; |
| 27 | luaA_pushobject(&s_object(i)); | 27 | else { |
| 28 | TObject *o = luaA_Address(v); | ||
| 29 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); | ||
| 30 | g = tsvalue(o); | ||
| 31 | luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected"); | ||
| 32 | g = (TaggedString *)g->head.next; | ||
| 33 | } | ||
| 34 | while (g && g->u.globalval.ttype == LUA_T_NIL) | ||
| 35 | g = (TaggedString *)g->head.next; | ||
| 36 | if (g) { | ||
| 37 | lua_pushstring(g->str); | ||
| 38 | luaA_pushobject(&g->u.globalval); | ||
| 28 | } | 39 | } |
| 29 | } | 40 | } |
| 30 | 41 | ||
| @@ -32,10 +43,9 @@ static void nextvar (void) | |||
| 32 | static void next (void) | 43 | static void next (void) |
| 33 | { | 44 | { |
| 34 | lua_Object o = lua_getparam(1); | 45 | lua_Object o = lua_getparam(1); |
| 35 | lua_Object r = lua_getparam(2); | 46 | lua_Object r = luaL_nonnullarg(2); |
| 36 | Node *n; | 47 | Node *n; |
| 37 | luaL_arg_check(lua_istable(o), 1, "table expected"); | 48 | luaL_arg_check(lua_istable(o), 1, "table expected"); |
| 38 | luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected"); | ||
| 39 | n = luaH_next(luaA_Address(o), luaA_Address(r)); | 49 | n = luaH_next(luaA_Address(o), luaA_Address(r)); |
| 40 | if (n) { | 50 | if (n) { |
| 41 | luaA_pushobject(&n->ref); | 51 | luaA_pushobject(&n->ref); |
| @@ -90,7 +100,7 @@ static char *to_string (lua_Object obj) | |||
| 90 | return buff; | 100 | return buff; |
| 91 | } | 101 | } |
| 92 | case LUA_T_USERDATA: { | 102 | case LUA_T_USERDATA: { |
| 93 | sprintf(buff, "userdata: %p", o->value.ts->u.v); | 103 | sprintf(buff, "userdata: %p", o->value.ts->u.d.v); |
| 94 | return buff; | 104 | return buff; |
| 95 | } | 105 | } |
| 96 | case LUA_T_NIL: | 106 | case LUA_T_NIL: |
| @@ -116,8 +126,7 @@ static void luaI_print (void) | |||
| 116 | 126 | ||
| 117 | static void luaI_type (void) | 127 | static void luaI_type (void) |
| 118 | { | 128 | { |
| 119 | lua_Object o = lua_getparam(1); | 129 | lua_Object o = luaL_nonnullarg(1); |
| 120 | luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument"); | ||
| 121 | lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]); | 130 | lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]); |
| 122 | lua_pushnumber(lua_tag(o)); | 131 | lua_pushnumber(lua_tag(o)); |
| 123 | } | 132 | } |
| @@ -149,8 +158,7 @@ static void luaI_assert (void) | |||
| 149 | 158 | ||
| 150 | static void setglobal (void) | 159 | static void setglobal (void) |
| 151 | { | 160 | { |
| 152 | lua_Object value = lua_getparam(2); | 161 | lua_Object value = luaL_nonnullarg(2); |
| 153 | luaL_arg_check(value != LUA_NOOBJECT, 2, NULL); | ||
| 154 | lua_pushobject(value); | 162 | lua_pushobject(value); |
| 155 | lua_setglobal(luaL_check_string(1)); | 163 | lua_setglobal(luaL_check_string(1)); |
| 156 | lua_pushobject(value); /* return given value */ | 164 | lua_pushobject(value); /* return given value */ |
| @@ -158,8 +166,7 @@ static void setglobal (void) | |||
| 158 | 166 | ||
| 159 | static void rawsetglobal (void) | 167 | static void rawsetglobal (void) |
| 160 | { | 168 | { |
| 161 | lua_Object value = lua_getparam(2); | 169 | lua_Object value = luaL_nonnullarg(2); |
| 162 | luaL_arg_check(value != LUA_NOOBJECT, 2, NULL); | ||
| 163 | lua_pushobject(value); | 170 | lua_pushobject(value); |
| 164 | lua_rawsetglobal(luaL_check_string(1)); | 171 | lua_rawsetglobal(luaL_check_string(1)); |
| 165 | lua_pushobject(value); /* return given value */ | 172 | lua_pushobject(value); /* return given value */ |
| @@ -233,10 +240,8 @@ static void newtag (void) | |||
| 233 | 240 | ||
| 234 | static void rawgettable (void) | 241 | static void rawgettable (void) |
| 235 | { | 242 | { |
| 236 | lua_Object t = lua_getparam(1); | 243 | lua_Object t = luaL_nonnullarg(1); |
| 237 | lua_Object i = lua_getparam(2); | 244 | lua_Object i = luaL_nonnullarg(2); |
| 238 | luaL_arg_check(t != LUA_NOOBJECT, 1, NULL); | ||
| 239 | luaL_arg_check(i != LUA_NOOBJECT, 2, NULL); | ||
| 240 | lua_pushobject(t); | 245 | lua_pushobject(t); |
| 241 | lua_pushobject(i); | 246 | lua_pushobject(i); |
| 242 | lua_pushobject(lua_rawgettable()); | 247 | lua_pushobject(lua_rawgettable()); |
| @@ -245,11 +250,9 @@ static void rawgettable (void) | |||
| 245 | 250 | ||
| 246 | static void rawsettable (void) | 251 | static void rawsettable (void) |
| 247 | { | 252 | { |
| 248 | lua_Object t = lua_getparam(1); | 253 | lua_Object t = luaL_nonnullarg(1); |
| 249 | lua_Object i = lua_getparam(2); | 254 | lua_Object i = luaL_nonnullarg(2); |
| 250 | lua_Object v = lua_getparam(3); | 255 | lua_Object v = luaL_nonnullarg(3); |
| 251 | luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, | ||
| 252 | 0, NULL); | ||
| 253 | lua_pushobject(t); | 256 | lua_pushobject(t); |
| 254 | lua_pushobject(i); | 257 | lua_pushobject(i); |
| 255 | lua_pushobject(v); | 258 | lua_pushobject(v); |
| @@ -259,8 +262,7 @@ static void rawsettable (void) | |||
| 259 | 262 | ||
| 260 | static void settagmethod (void) | 263 | static void settagmethod (void) |
| 261 | { | 264 | { |
| 262 | lua_Object nf = lua_getparam(3); | 265 | lua_Object nf = luaL_nonnullarg(3); |
| 263 | luaL_arg_check(nf != LUA_NOOBJECT, 3, "value expected"); | ||
| 264 | lua_pushobject(nf); | 266 | lua_pushobject(nf); |
| 265 | lua_pushobject(lua_settagmethod((int)luaL_check_number(1), | 267 | lua_pushobject(lua_settagmethod((int)luaL_check_number(1), |
| 266 | luaL_check_string(2))); | 268 | luaL_check_string(2))); |
| @@ -276,8 +278,7 @@ static void gettagmethod (void) | |||
| 276 | 278 | ||
| 277 | static void seterrormethod (void) | 279 | static void seterrormethod (void) |
| 278 | { | 280 | { |
| 279 | lua_Object nf = lua_getparam(1); | 281 | lua_Object nf = luaL_nonnullarg(1); |
| 280 | luaL_arg_check(nf != LUA_NOOBJECT, 1, "value expected"); | ||
| 281 | lua_pushobject(nf); | 282 | lua_pushobject(nf); |
| 282 | lua_pushobject(lua_seterrormethod()); | 283 | lua_pushobject(lua_seterrormethod()); |
| 283 | } | 284 | } |
| @@ -387,18 +388,21 @@ static struct luaL_reg int_funcs[] = { | |||
| 387 | void luaB_predefine (void) | 388 | void luaB_predefine (void) |
| 388 | { | 389 | { |
| 389 | int i; | 390 | int i; |
| 390 | Word n; | 391 | TaggedString *ts; |
| 392 | TObject o; | ||
| 391 | /* pre-register mem error messages, to avoid loop when error arises */ | 393 | /* pre-register mem error messages, to avoid loop when error arises */ |
| 392 | luaS_newfixedstring(tableEM); | 394 | luaS_newfixedstring(tableEM); |
| 393 | luaS_newfixedstring(memEM); | 395 | luaS_newfixedstring(memEM); |
| 396 | o.ttype = LUA_T_CFUNCTION; | ||
| 394 | for (i=0; i<INTFUNCSIZE; i++) { | 397 | for (i=0; i<INTFUNCSIZE; i++) { |
| 395 | n = luaG_findsymbolbyname(int_funcs[i].name); | 398 | ts = luaS_new(int_funcs[i].name); |
| 396 | s_ttype(n) = LUA_T_CFUNCTION; | 399 | fvalue(&o) = int_funcs[i].func; |
| 397 | fvalue(&s_object(n)) = int_funcs[i].func; | 400 | luaS_rawsetglobal(ts, &o); |
| 398 | } | 401 | } |
| 399 | n = luaG_findsymbolbyname("_VERSION"); | 402 | ts = luaS_new("_VERSION"); |
| 400 | s_ttype(n) = LUA_T_STRING; | 403 | ttype(&o) = LUA_T_STRING; |
| 401 | tsvalue(&s_object(n)) = luaS_new(LUA_VERSION); | 404 | tsvalue(&o) = luaS_new(LUA_VERSION); |
| 405 | luaS_rawsetglobal(ts, &o); | ||
| 402 | } | 406 | } |
| 403 | 407 | ||
| 404 | 408 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: ldo.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -9,6 +9,7 @@ | |||
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| 10 | #include <string.h> | 10 | #include <string.h> |
| 11 | 11 | ||
| 12 | #include "lbuiltin.h" | ||
| 12 | #include "ldo.h" | 13 | #include "ldo.h" |
| 13 | #include "lgc.h" | 14 | #include "lgc.h" |
| 14 | #include "lmem.h" | 15 | #include "lmem.h" |
| @@ -50,6 +51,7 @@ static void initstack (int n) | |||
| 50 | luaD_stack.last = luaD_stack.stack+(maxstack-1); | 51 | luaD_stack.last = luaD_stack.stack+(maxstack-1); |
| 51 | luaD_stack.top = luaD_stack.stack; | 52 | luaD_stack.top = luaD_stack.stack; |
| 52 | *(luaD_stack.top++) = initial_stack; | 53 | *(luaD_stack.top++) = initial_stack; |
| 54 | luaB_predefine(); | ||
| 53 | } | 55 | } |
| 54 | 56 | ||
| 55 | 57 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: lgc.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -8,7 +8,6 @@ | |||
| 8 | #include "ldo.h" | 8 | #include "ldo.h" |
| 9 | #include "lfunc.h" | 9 | #include "lfunc.h" |
| 10 | #include "lgc.h" | 10 | #include "lgc.h" |
| 11 | #include "lglobal.h" | ||
| 12 | #include "lmem.h" | 11 | #include "lmem.h" |
| 13 | #include "lobject.h" | 12 | #include "lobject.h" |
| 14 | #include "lstring.h" | 13 | #include "lstring.h" |
| @@ -91,7 +90,7 @@ static int ismarked (TObject *o) | |||
| 91 | { | 90 | { |
| 92 | switch (o->ttype) { | 91 | switch (o->ttype) { |
| 93 | case LUA_T_STRING: case LUA_T_USERDATA: | 92 | case LUA_T_STRING: case LUA_T_USERDATA: |
| 94 | return o->value.ts->marked; | 93 | return o->value.ts->head.marked; |
| 95 | case LUA_T_FUNCTION: | 94 | case LUA_T_FUNCTION: |
| 96 | return o->value.cl->head.marked; | 95 | return o->value.cl->head.marked; |
| 97 | case LUA_T_PROTO: | 96 | case LUA_T_PROTO: |
| @@ -129,10 +128,11 @@ static void strcallIM (TaggedString *l) | |||
| 129 | { | 128 | { |
| 130 | TObject o; | 129 | TObject o; |
| 131 | ttype(&o) = LUA_T_USERDATA; | 130 | ttype(&o) = LUA_T_USERDATA; |
| 132 | for (; l; l=l->uu.next) { | 131 | for (; l; l=(TaggedString *)l->head.next) |
| 133 | tsvalue(&o) = l; | 132 | if (l->constindex == -1) { /* is userdata? */ |
| 134 | luaD_gcIM(&o); | 133 | tsvalue(&o) = l; |
| 135 | } | 134 | luaD_gcIM(&o); |
| 135 | } | ||
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | 138 | ||
| @@ -164,8 +164,8 @@ static GCnode *listcollect (GCnode **root) | |||
| 164 | 164 | ||
| 165 | static void strmark (TaggedString *s) | 165 | static void strmark (TaggedString *s) |
| 166 | { | 166 | { |
| 167 | if (!s->marked) | 167 | if (!s->head.marked) |
| 168 | s->marked = 1; | 168 | s->head.marked = 1; |
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | 171 | ||
| @@ -215,6 +215,17 @@ static void hashmark (Hash *h) | |||
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | 217 | ||
| 218 | static void globalmark (void) | ||
| 219 | { | ||
| 220 | TaggedString *g; | ||
| 221 | for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) | ||
| 222 | if (g->u.globalval.ttype != LUA_T_NIL) { | ||
| 223 | markobject(&g->u.globalval); | ||
| 224 | strmark(g); /* cannot collect non nil global variables */ | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 218 | static int markobject (TObject *o) | 229 | static int markobject (TObject *o) |
| 219 | { | 230 | { |
| 220 | switch (ttype(o)) { | 231 | switch (ttype(o)) { |
| @@ -253,7 +264,7 @@ long luaC_threshold = GARBAGE_BLOCK; | |||
| 253 | static void markall (void) | 264 | static void markall (void) |
| 254 | { | 265 | { |
| 255 | luaD_travstack(markobject); /* mark stack objects */ | 266 | luaD_travstack(markobject); /* mark stack objects */ |
| 256 | luaG_travsymbol(markobject); /* mark symbol table objects */ | 267 | globalmark(); /* mark global variable values and names */ |
| 257 | travlock(); /* mark locked objects */ | 268 | travlock(); /* mark locked objects */ |
| 258 | luaT_travtagmethods(markobject); /* mark fallbacks */ | 269 | luaT_travtagmethods(markobject); /* mark fallbacks */ |
| 259 | } | 270 | } |
diff --git a/lglobal.c b/lglobal.c deleted file mode 100644 index 9f2b9f90..00000000 --- a/lglobal.c +++ /dev/null | |||
| @@ -1,71 +0,0 @@ | |||
| 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 deleted file mode 100644 index 00edb4c6..00000000 --- a/lglobal.h +++ /dev/null | |||
| @@ -1,35 +0,0 @@ | |||
| 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 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: llex.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Lexical Analizer | 3 | ** Lexical Analizer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -8,7 +8,6 @@ | |||
| 8 | #include <ctype.h> | 8 | #include <ctype.h> |
| 9 | #include <string.h> | 9 | #include <string.h> |
| 10 | 10 | ||
| 11 | #include "lglobal.h" | ||
| 12 | #include "llex.h" | 11 | #include "llex.h" |
| 13 | #include "lmem.h" | 12 | #include "lmem.h" |
| 14 | #include "lobject.h" | 13 | #include "lobject.h" |
| @@ -48,7 +47,7 @@ static void addReserved (void) | |||
| 48 | firsttime = 0; | 47 | firsttime = 0; |
| 49 | for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { | 48 | for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { |
| 50 | TaggedString *ts = luaS_new(reserved[i].name); | 49 | TaggedString *ts = luaS_new(reserved[i].name); |
| 51 | ts->marked = reserved[i].token; /* reserved word (always > 255) */ | 50 | ts->head.marked = reserved[i].token; /* reserved word (always > 255) */ |
| 52 | } | 51 | } |
| 53 | } | 52 | } |
| 54 | } | 53 | } |
| @@ -120,7 +119,7 @@ static int checkcond (char *buff) | |||
| 120 | int i = luaO_findstring(buff, opts); | 119 | int i = luaO_findstring(buff, opts); |
| 121 | if (i >= 0) return i; | 120 | if (i >= 0) return i; |
| 122 | else if (isalpha((unsigned char)buff[0]) || buff[0] == '_') | 121 | else if (isalpha((unsigned char)buff[0]) || buff[0] == '_') |
| 123 | return luaG_globaldefined(buff); | 122 | return luaS_globaldefined(buff); |
| 124 | else { | 123 | else { |
| 125 | luaY_syntaxerror("invalid $if condition", buff); | 124 | luaY_syntaxerror("invalid $if condition", buff); |
| 126 | return 0; /* to avoid warnings */ | 125 | return 0; /* to avoid warnings */ |
| @@ -451,8 +450,8 @@ int luaY_lex (void) | |||
| 451 | } while (isalnum(current) || current == '_'); | 450 | } while (isalnum(current) || current == '_'); |
| 452 | save(0); | 451 | save(0); |
| 453 | ts = luaS_new(textbuff.text); | 452 | ts = luaS_new(textbuff.text); |
| 454 | if (ts->marked > 255) | 453 | if (ts->head.marked > 255) |
| 455 | return ts->marked; /* reserved word */ | 454 | return ts->head.marked; /* reserved word */ |
| 456 | luaY_lval.pTStr = ts; | 455 | luaY_lval.pTStr = ts; |
| 457 | return NAME; | 456 | return NAME; |
| 458 | } | 457 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: lobject.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -29,30 +29,43 @@ typedef unsigned short Word; /* unsigned 16 bits */ | |||
| 29 | typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */ | 29 | typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */ |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | |||
| 33 | |||
| 34 | /* | 32 | /* |
| 35 | ** String headers for string table | 33 | ** Lua TYPES |
| 34 | ** WARNING: if you change the order of this enumeration, | ||
| 35 | ** grep "ORDER LUA_T" | ||
| 36 | */ | 36 | */ |
| 37 | typedef enum { | ||
| 38 | LUA_T_NIL = -10, | ||
| 39 | LUA_T_NUMBER = -9, | ||
| 40 | LUA_T_STRING = -8, | ||
| 41 | LUA_T_ARRAY = -7, /* array==table */ | ||
| 42 | LUA_T_PROTO = -6, | ||
| 43 | LUA_T_FUNCTION = -5, | ||
| 44 | LUA_T_CFUNCTION= -4, | ||
| 45 | LUA_T_MARK = -3, | ||
| 46 | LUA_T_CMARK = -2, | ||
| 47 | LUA_T_LINE = -1, | ||
| 48 | LUA_T_USERDATA = 0 | ||
| 49 | } lua_Type; | ||
| 37 | 50 | ||
| 38 | #define NOT_USED 0xFFFE | 51 | #define NUM_TYPES 11 |
| 52 | |||
| 53 | |||
| 54 | typedef union { | ||
| 55 | lua_CFunction f; /* LUA_T_CFUNCTION, LUA_T_CMARK */ | ||
| 56 | real n; /* LUA_T_NUMBER */ | ||
| 57 | struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */ | ||
| 58 | struct TProtoFunc *tf; /* LUA_T_PROTO */ | ||
| 59 | struct Closure *cl; /* LUA_T_FUNCTION, LUA_T_MARK */ | ||
| 60 | struct Hash *a; /* LUA_T_ARRAY */ | ||
| 61 | int i; /* LUA_T_LINE */ | ||
| 62 | } Value; | ||
| 39 | 63 | ||
| 40 | typedef struct TaggedString { | 64 | |
| 41 | int tag; /* if != LUA_T_STRING, this is a userdata */ | 65 | typedef struct TObject { |
| 42 | union { | 66 | lua_Type ttype; |
| 43 | unsigned long hash; | 67 | Value value; |
| 44 | struct TaggedString *next; | 68 | } TObject; |
| 45 | } uu; | ||
| 46 | union { | ||
| 47 | struct { | ||
| 48 | Word varindex; /* != NOT_USED if this is a symbol */ | ||
| 49 | Word constindex; /* hint to reuse constant indexes */ | ||
| 50 | } s; | ||
| 51 | void *v; /* if this is a userdata, here is its value */ | ||
| 52 | } u; | ||
| 53 | int marked; /* for garbage collection; never collect (nor change) if > 1 */ | ||
| 54 | char str[1]; /* \0 byte already reserved */ | ||
| 55 | } TaggedString; | ||
| 56 | 69 | ||
| 57 | 70 | ||
| 58 | 71 | ||
| @@ -66,6 +79,27 @@ typedef struct GCnode { | |||
| 66 | 79 | ||
| 67 | 80 | ||
| 68 | /* | 81 | /* |
| 82 | ** String headers for string table | ||
| 83 | */ | ||
| 84 | |||
| 85 | typedef struct TaggedString { | ||
| 86 | GCnode head; | ||
| 87 | int constindex; /* hint to reuse constants (= -1 if this is a userdata) */ | ||
| 88 | unsigned long hash; | ||
| 89 | union { | ||
| 90 | TObject globalval; | ||
| 91 | struct { | ||
| 92 | void *v; /* if this is a userdata, here is its value */ | ||
| 93 | int tag; | ||
| 94 | } d; | ||
| 95 | } u; | ||
| 96 | char str[1]; /* \0 byte already reserved */ | ||
| 97 | } TaggedString; | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | /* | ||
| 69 | ** Function Prototypes | 103 | ** Function Prototypes |
| 70 | */ | 104 | */ |
| 71 | typedef struct TProtoFunc { | 105 | typedef struct TProtoFunc { |
| @@ -87,43 +121,6 @@ typedef struct LocVar { | |||
| 87 | 121 | ||
| 88 | 122 | ||
| 89 | 123 | ||
| 90 | /* | ||
| 91 | ** Lua TYPES | ||
| 92 | ** WARNING: if you change the order of this enumeration, | ||
| 93 | ** grep "ORDER LUA_T" | ||
| 94 | */ | ||
| 95 | typedef enum { | ||
| 96 | LUA_T_NIL = -10, | ||
| 97 | LUA_T_NUMBER = -9, | ||
| 98 | LUA_T_STRING = -8, | ||
| 99 | LUA_T_ARRAY = -7, /* array==table */ | ||
| 100 | LUA_T_PROTO = -6, | ||
| 101 | LUA_T_FUNCTION = -5, | ||
| 102 | LUA_T_CFUNCTION= -4, | ||
| 103 | LUA_T_MARK = -3, | ||
| 104 | LUA_T_CMARK = -2, | ||
| 105 | LUA_T_LINE = -1, | ||
| 106 | LUA_T_USERDATA = 0 | ||
| 107 | } lua_Type; | ||
| 108 | |||
| 109 | #define NUM_TYPES 11 | ||
| 110 | |||
| 111 | |||
| 112 | typedef union { | ||
| 113 | lua_CFunction f; | ||
| 114 | real n; | ||
| 115 | TaggedString *ts; | ||
| 116 | TProtoFunc *tf; | ||
| 117 | struct Closure *cl; | ||
| 118 | struct Hash *a; | ||
| 119 | int i; | ||
| 120 | } Value; | ||
| 121 | |||
| 122 | typedef struct TObject { | ||
| 123 | lua_Type ttype; | ||
| 124 | Value value; | ||
| 125 | } TObject; | ||
| 126 | |||
| 127 | 124 | ||
| 128 | /* Macros to access structure members */ | 125 | /* Macros to access structure members */ |
| 129 | #define ttype(o) ((o)->ttype) | 126 | #define ttype(o) ((o)->ttype) |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.c,v 1.1 1997/08/14 20:23:30 roberto Exp $ | 2 | ** $Id: lstring.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -15,6 +15,10 @@ | |||
| 15 | 15 | ||
| 16 | #define NUM_HASHS 61 | 16 | #define NUM_HASHS 61 |
| 17 | 17 | ||
| 18 | |||
| 19 | GCnode luaS_root = {NULL, 0}; /* list of global variables */ | ||
| 20 | |||
| 21 | |||
| 18 | typedef struct { | 22 | typedef struct { |
| 19 | int size; | 23 | int size; |
| 20 | int nuse; /* number of elements (including EMPTYs) */ | 24 | int nuse; /* number of elements (including EMPTYs) */ |
| @@ -39,7 +43,7 @@ static stringtable string_root[NUM_HASHS] = { | |||
| 39 | }; | 43 | }; |
| 40 | 44 | ||
| 41 | 45 | ||
| 42 | static TaggedString EMPTY = {LUA_T_STRING, {0}, {{NOT_USED, NOT_USED}}, 2, {0}}; | 46 | static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; |
| 43 | 47 | ||
| 44 | 48 | ||
| 45 | 49 | ||
| @@ -68,7 +72,7 @@ static void grow (stringtable *tb) | |||
| 68 | tb->nuse = 0; | 72 | tb->nuse = 0; |
| 69 | for (i=0; i<tb->size; i++) { | 73 | for (i=0; i<tb->size; i++) { |
| 70 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) { | 74 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) { |
| 71 | int h = tb->hash[i]->uu.hash%newsize; | 75 | int h = tb->hash[i]->hash%newsize; |
| 72 | while (newhash[h]) | 76 | while (newhash[h]) |
| 73 | h = (h+1)%newsize; | 77 | h = (h+1)%newsize; |
| 74 | newhash[h] = tb->hash[i]; | 78 | newhash[h] = tb->hash[i]; |
| @@ -87,16 +91,18 @@ static TaggedString *newone(char *buff, int tag, unsigned long h) | |||
| 87 | if (tag == LUA_T_STRING) { | 91 | if (tag == LUA_T_STRING) { |
| 88 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+strlen(buff)); | 92 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+strlen(buff)); |
| 89 | strcpy(ts->str, buff); | 93 | strcpy(ts->str, buff); |
| 90 | ts->u.s.varindex = ts->u.s.constindex = NOT_USED; | 94 | ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */ |
| 91 | ts->tag = LUA_T_STRING; | 95 | ts->constindex = 0; |
| 92 | } | 96 | } |
| 93 | else { | 97 | else { |
| 94 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); | 98 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); |
| 95 | ts->u.v = buff; | 99 | ts->u.d.v = buff; |
| 96 | ts->tag = tag == LUA_ANYTAG ? 0 : tag; | 100 | ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag; |
| 101 | ts->constindex = -1; /* tag -> this is a userdata */ | ||
| 97 | } | 102 | } |
| 98 | ts->marked = 0; | 103 | ts->head.marked = 0; |
| 99 | ts->uu.hash = h; | 104 | ts->head.next = (GCnode *)ts; /* signal it is in no list */ |
| 105 | ts->hash = h; | ||
| 100 | return ts; | 106 | return ts; |
| 101 | } | 107 | } |
| 102 | 108 | ||
| @@ -113,9 +119,9 @@ static TaggedString *insert (char *buff, int tag, stringtable *tb) | |||
| 113 | { | 119 | { |
| 114 | if (ts == &EMPTY) | 120 | if (ts == &EMPTY) |
| 115 | j = i; | 121 | j = i; |
| 116 | else if ((ts->tag == LUA_T_STRING) ? | 122 | else if ((ts->constindex >= 0) ? /* is a string? */ |
| 117 | (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) : | 123 | (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) : |
| 118 | ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v)) | 124 | ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v)) |
| 119 | return ts; | 125 | return ts; |
| 120 | i = (i+1)%tb->size; | 126 | i = (i+1)%tb->size; |
| 121 | } | 127 | } |
| @@ -142,8 +148,8 @@ TaggedString *luaS_new (char *str) | |||
| 142 | TaggedString *luaS_newfixedstring (char *str) | 148 | TaggedString *luaS_newfixedstring (char *str) |
| 143 | { | 149 | { |
| 144 | TaggedString *ts = luaS_new(str); | 150 | TaggedString *ts = luaS_new(str); |
| 145 | if (ts->marked == 0) | 151 | if (ts->head.marked == 0) |
| 146 | ts->marked = 2; /* avoid GC */ | 152 | ts->head.marked = 2; /* avoid GC */ |
| 147 | return ts; | 153 | return ts; |
| 148 | } | 154 | } |
| 149 | 155 | ||
| @@ -151,7 +157,7 @@ TaggedString *luaS_newfixedstring (char *str) | |||
| 151 | void luaS_free (TaggedString *l) | 157 | void luaS_free (TaggedString *l) |
| 152 | { | 158 | { |
| 153 | while (l) { | 159 | while (l) { |
| 154 | TaggedString *next = l->uu.next; | 160 | TaggedString *next = (TaggedString *)l->head.next; |
| 155 | luaM_free(l); | 161 | luaM_free(l); |
| 156 | l = next; | 162 | l = next; |
| 157 | } | 163 | } |
| @@ -159,22 +165,35 @@ void luaS_free (TaggedString *l) | |||
| 159 | 165 | ||
| 160 | 166 | ||
| 161 | /* | 167 | /* |
| 162 | ** Garbage collection function. | 168 | ** Garbage collection functions. |
| 163 | */ | 169 | */ |
| 170 | |||
| 171 | static void remove_from_list (GCnode *l) | ||
| 172 | { | ||
| 173 | while (l) { | ||
| 174 | GCnode *next = l->next; | ||
| 175 | while (next && !next->marked) | ||
| 176 | next = l->next = next->next; | ||
| 177 | l = next; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 164 | TaggedString *luaS_collector (void) | 182 | TaggedString *luaS_collector (void) |
| 165 | { | 183 | { |
| 166 | TaggedString *frees = NULL; | 184 | TaggedString *frees = NULL; |
| 167 | int i; | 185 | int i; |
| 186 | remove_from_list(&luaS_root); | ||
| 168 | for (i=0; i<NUM_HASHS; i++) { | 187 | for (i=0; i<NUM_HASHS; i++) { |
| 169 | stringtable *tb = &string_root[i]; | 188 | stringtable *tb = &string_root[i]; |
| 170 | int j; | 189 | int j; |
| 171 | for (j=0; j<tb->size; j++) { | 190 | for (j=0; j<tb->size; j++) { |
| 172 | TaggedString *t = tb->hash[j]; | 191 | TaggedString *t = tb->hash[j]; |
| 173 | if (t == NULL) continue; | 192 | if (t == NULL) continue; |
| 174 | if (t->marked == 1) | 193 | if (t->head.marked == 1) |
| 175 | t->marked = 0; | 194 | t->head.marked = 0; |
| 176 | else if (!t->marked) { | 195 | else if (!t->head.marked) { |
| 177 | t->uu.next = frees; | 196 | t->head.next = (GCnode *)frees; |
| 178 | frees = t; | 197 | frees = t; |
| 179 | tb->hash[j] = &EMPTY; | 198 | tb->hash[j] = &EMPTY; |
| 180 | --luaO_nentities; | 199 | --luaO_nentities; |
| @@ -184,3 +203,30 @@ TaggedString *luaS_collector (void) | |||
| 184 | return frees; | 203 | return frees; |
| 185 | } | 204 | } |
| 186 | 205 | ||
| 206 | |||
| 207 | void luaS_rawsetglobal (TaggedString *ts, TObject *newval) | ||
| 208 | { | ||
| 209 | ts->u.globalval = *newval; | ||
| 210 | if (ts->head.next == (GCnode *)ts) { /* is not in list? */ | ||
| 211 | ts->head.next = luaS_root.next; | ||
| 212 | luaS_root.next = (GCnode *)ts; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | char *luaS_travsymbol (int (*fn)(TObject *)) | ||
| 218 | { | ||
| 219 | TaggedString *g; | ||
| 220 | for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) | ||
| 221 | if (fn(&g->u.globalval)) | ||
| 222 | return g->str; | ||
| 223 | return NULL; | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | int luaS_globaldefined (char *name) | ||
| 228 | { | ||
| 229 | TaggedString *ts = luaS_new(name); | ||
| 230 | return ts->u.globalval.ttype != LUA_T_NIL; | ||
| 231 | } | ||
| 232 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.h,v 1.1 1997/08/14 20:23:40 roberto Exp roberto $ | 2 | ** $Id: lstring.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | #include "lobject.h" | 11 | #include "lobject.h" |
| 12 | 12 | ||
| 13 | extern GCnode luaS_root; | ||
| 13 | 14 | ||
| 14 | TaggedString *luaS_createudata (void *udata, int tag); | 15 | TaggedString *luaS_createudata (void *udata, int tag); |
| 15 | TaggedString *luaS_collector (void); | 16 | TaggedString *luaS_collector (void); |
| @@ -17,5 +18,8 @@ void luaS_free (TaggedString *l); | |||
| 17 | void luaS_callIM (TaggedString *l); | 18 | void luaS_callIM (TaggedString *l); |
| 18 | TaggedString *luaS_new (char *str); | 19 | TaggedString *luaS_new (char *str); |
| 19 | TaggedString *luaS_newfixedstring (char *str); | 20 | TaggedString *luaS_newfixedstring (char *str); |
| 21 | void luaS_rawsetglobal (TaggedString *ts, TObject *newval); | ||
| 22 | char *luaS_travsymbol (int (*fn)(TObject *)); | ||
| 23 | int luaS_globaldefined (char *name); | ||
| 20 | 24 | ||
| 21 | #endif | 25 | #endif |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: ltm.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Tag methods | 3 | ** Tag methods |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -176,7 +176,7 @@ int luaT_efectivetag (TObject *o) | |||
| 176 | { | 176 | { |
| 177 | lua_Type t = ttype(o); | 177 | lua_Type t = ttype(o); |
| 178 | if (t == LUA_T_USERDATA) { | 178 | if (t == LUA_T_USERDATA) { |
| 179 | int tag = o->value.ts->tag; | 179 | int tag = o->value.ts->u.d.tag; |
| 180 | return (tag >= 0) ? LUA_T_USERDATA : tag; | 180 | return (tag >= 0) ? LUA_T_USERDATA : tag; |
| 181 | } | 181 | } |
| 182 | else if (t == LUA_T_ARRAY) | 182 | else if (t == LUA_T_ARRAY) |
| @@ -1,6 +1,6 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | /* | 2 | /* |
| 3 | ** $Id: lua.stx,v 1.4 1997/09/22 20:53:20 roberto Exp roberto $ | 3 | ** $Id: lua.stx,v 1.5 1997/09/24 19:43:11 roberto Exp roberto $ |
| 4 | ** Syntax analizer and code generator | 4 | ** Syntax analizer and code generator |
| 5 | ** See Copyright Notice in lua.h | 5 | ** See Copyright Notice in lua.h |
| 6 | */ | 6 | */ |
| @@ -203,13 +203,13 @@ static int next_constant (State *cs) | |||
| 203 | static int string_constant (TaggedString *s, State *cs) | 203 | static int string_constant (TaggedString *s, State *cs) |
| 204 | { | 204 | { |
| 205 | TProtoFunc *f = cs->f; | 205 | TProtoFunc *f = cs->f; |
| 206 | int c = s->u.s.constindex; | 206 | int c = s->constindex; |
| 207 | if (!(0 <= c && c < f->nconsts && | 207 | if (!(c < f->nconsts && |
| 208 | ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { | 208 | ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { |
| 209 | c = next_constant(cs); | 209 | c = next_constant(cs); |
| 210 | ttype(&f->consts[c]) = LUA_T_STRING; | 210 | ttype(&f->consts[c]) = LUA_T_STRING; |
| 211 | tsvalue(&f->consts[c]) = s; | 211 | tsvalue(&f->consts[c]) = s; |
| 212 | s->u.s.constindex = c; /* hint for next time */ | 212 | s->constindex = c; /* hint for next time */ |
| 213 | } | 213 | } |
| 214 | return c; | 214 | return c; |
| 215 | } | 215 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.4 1997/09/22 20:53:20 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.5 1997/09/24 19:43:11 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -12,7 +12,6 @@ | |||
| 12 | #include "ldo.h" | 12 | #include "ldo.h" |
| 13 | #include "lfunc.h" | 13 | #include "lfunc.h" |
| 14 | #include "lgc.h" | 14 | #include "lgc.h" |
| 15 | #include "lglobal.h" | ||
| 16 | #include "lmem.h" | 15 | #include "lmem.h" |
| 17 | #include "lopcodes.h" | 16 | #include "lopcodes.h" |
| 18 | #include "lstring.h" | 17 | #include "lstring.h" |
| @@ -155,17 +154,17 @@ void luaV_settable (TObject *t, int mode) | |||
| 155 | } | 154 | } |
| 156 | 155 | ||
| 157 | 156 | ||
| 158 | void luaV_getglobal (Word n) | 157 | void luaV_getglobal (TaggedString *ts) |
| 159 | { | 158 | { |
| 160 | /* WARNING: caller must assure stack space */ | 159 | /* WARNING: caller must assure stack space */ |
| 161 | TObject *value = &luaG_global[n].object; | 160 | TObject *value = &ts->u.globalval; |
| 162 | TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); | 161 | TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); |
| 163 | if (ttype(im) == LUA_T_NIL) { /* default behavior */ | 162 | if (ttype(im) == LUA_T_NIL) { /* default behavior */ |
| 164 | *luaD_stack.top++ = *value; | 163 | *luaD_stack.top++ = *value; |
| 165 | } | 164 | } |
| 166 | else { | 165 | else { |
| 167 | ttype(luaD_stack.top) = LUA_T_STRING; | 166 | ttype(luaD_stack.top) = LUA_T_STRING; |
| 168 | tsvalue(luaD_stack.top) = luaG_global[n].varname; | 167 | tsvalue(luaD_stack.top) = ts; |
| 169 | luaD_stack.top++; | 168 | luaD_stack.top++; |
| 170 | *luaD_stack.top++ = *value; | 169 | *luaD_stack.top++ = *value; |
| 171 | luaD_callTM(im, 2, 1); | 170 | luaD_callTM(im, 2, 1); |
| @@ -173,17 +172,17 @@ void luaV_getglobal (Word n) | |||
| 173 | } | 172 | } |
| 174 | 173 | ||
| 175 | 174 | ||
| 176 | void luaV_setglobal (Word n) | 175 | void luaV_setglobal (TaggedString *ts) |
| 177 | { | 176 | { |
| 178 | TObject *oldvalue = &luaG_global[n].object; | 177 | TObject *oldvalue = &ts->u.globalval; |
| 179 | TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL); | 178 | TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL); |
| 180 | if (ttype(im) == LUA_T_NIL) /* default behavior */ | 179 | if (ttype(im) == LUA_T_NIL) /* default behavior */ |
| 181 | s_object(n) = *(--luaD_stack.top); | 180 | luaS_rawsetglobal(ts, --luaD_stack.top); |
| 182 | else { | 181 | else { |
| 183 | /* WARNING: caller must assure stack space */ | 182 | /* WARNING: caller must assure stack space */ |
| 184 | TObject newvalue = *(luaD_stack.top-1); | 183 | TObject newvalue = *(luaD_stack.top-1); |
| 185 | ttype(luaD_stack.top-1) = LUA_T_STRING; | 184 | ttype(luaD_stack.top-1) = LUA_T_STRING; |
| 186 | tsvalue(luaD_stack.top-1) = luaG_global[n].varname; | 185 | tsvalue(luaD_stack.top-1) = ts; |
| 187 | *luaD_stack.top++ = *oldvalue; | 186 | *luaD_stack.top++ = *oldvalue; |
| 188 | *luaD_stack.top++ = newvalue; | 187 | *luaD_stack.top++ = newvalue; |
| 189 | luaD_callTM(im, 3, 0); | 188 | luaD_callTM(im, 3, 0); |
| @@ -334,7 +333,7 @@ StkId luaV_execute (Closure *cl, StkId base) | |||
| 334 | case GETGLOBAL9: | 333 | case GETGLOBAL9: |
| 335 | aux -= GETGLOBAL0; | 334 | aux -= GETGLOBAL0; |
| 336 | getglobal: | 335 | getglobal: |
| 337 | luaV_getglobal(luaG_findsymbol(tsvalue(&consts[aux]))); | 336 | luaV_getglobal(tsvalue(&consts[aux])); |
| 338 | break; | 337 | break; |
| 339 | 338 | ||
| 340 | case GETTABLE: | 339 | case GETTABLE: |
| @@ -396,7 +395,7 @@ StkId luaV_execute (Closure *cl, StkId base) | |||
| 396 | case SETGLOBALB: | 395 | case SETGLOBALB: |
| 397 | aux = *pc++; | 396 | aux = *pc++; |
| 398 | setglobal: | 397 | setglobal: |
| 399 | luaV_setglobal(luaG_findsymbol(tsvalue(&consts[aux]))); | 398 | luaV_setglobal(tsvalue(&consts[aux])); |
| 400 | break; | 399 | break; |
| 401 | 400 | ||
| 402 | case SETTABLE0: | 401 | case SETTABLE0: |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: lvm.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -21,8 +21,8 @@ int luaV_tonumber (TObject *obj); | |||
| 21 | int luaV_tostring (TObject *obj); | 21 | int luaV_tostring (TObject *obj); |
| 22 | void luaV_gettable (void); | 22 | void luaV_gettable (void); |
| 23 | void luaV_settable (TObject *t, int mode); | 23 | void luaV_settable (TObject *t, int mode); |
| 24 | void luaV_getglobal (Word n); | 24 | void luaV_getglobal (TaggedString *ts); |
| 25 | void luaV_setglobal (Word n); | 25 | void luaV_setglobal (TaggedString *ts); |
| 26 | StkId luaV_execute (Closure *func, StkId base); | 26 | StkId luaV_execute (Closure *func, StkId base); |
| 27 | void luaV_closure (void); | 27 | void luaV_closure (void); |
| 28 | 28 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | # | 1 | # |
| 2 | ## $Id: $ | 2 | ## $Id: makefile,v 1.1 1997/09/16 19:33:21 roberto Exp roberto $ |
| 3 | ## Makefile | 3 | ## Makefile |
| 4 | ## See Copyright Notice in lua.h | 4 | ## See Copyright Notice in lua.h |
| 5 | # | 5 | # |
| @@ -18,7 +18,7 @@ | |||
| 18 | # define LUA_COMPAT2_5=0 if yous system does not need to be compatible with | 18 | # define LUA_COMPAT2_5=0 if yous system does not need to be compatible with |
| 19 | # version 2.5 (or older) | 19 | # version 2.5 (or older) |
| 20 | 20 | ||
| 21 | CONFIG = -DPOPEN -D_POSIX_SOURCE | 21 | CONFIG = -DPOPEN -D_POSIX_SOURCE |
| 22 | #CONFIG = -DLUA_COMPAT2_5=0 -DOLD_ANSI -DDEBUG | 22 | #CONFIG = -DLUA_COMPAT2_5=0 -DOLD_ANSI -DDEBUG |
| 23 | 23 | ||
| 24 | 24 | ||
| @@ -40,7 +40,6 @@ LUAOBJS = \ | |||
| 40 | ldo.o \ | 40 | ldo.o \ |
| 41 | lfunc.o \ | 41 | lfunc.o \ |
| 42 | lgc.o \ | 42 | lgc.o \ |
| 43 | lglobal.o \ | ||
| 44 | llex.o \ | 43 | llex.o \ |
| 45 | lmem.o \ | 44 | lmem.o \ |
| 46 | lobject.o \ | 45 | lobject.o \ |
| @@ -96,31 +95,29 @@ clear : | |||
| 96 | 95 | ||
| 97 | 96 | ||
| 98 | lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lfunc.h lgc.h \ | 97 | lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lfunc.h lgc.h \ |
| 99 | lglobal.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h | 98 | lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h |
| 100 | lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h | 99 | lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h |
| 101 | lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ | 100 | lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ |
| 102 | lglobal.h lmem.h lstring.h ltable.h ltm.h | 101 | lmem.h lstring.h ltable.h ltm.h |
| 103 | ldo.o: ldo.c ldo.h lobject.h lua.h lgc.h lmem.h lparser.h lzio.h ltm.h \ | 102 | ldo.o: ldo.c lbuiltin.h ldo.h lobject.h lua.h lgc.h lmem.h lparser.h \ |
| 104 | luadebug.h lundump.h lvm.h | 103 | lzio.h ltm.h luadebug.h lundump.h lvm.h |
| 105 | lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h | 104 | lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h |
| 106 | lgc.o: lgc.c ldo.h lobject.h lua.h lfunc.h lgc.h lglobal.h lmem.h \ | 105 | lgc.o: lgc.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h lstring.h \ |
| 107 | lstring.h ltable.h ltm.h | 106 | ltable.h ltm.h |
| 108 | lglobal.o: lglobal.c lbuiltin.h lglobal.h lobject.h lua.h lmem.h \ | ||
| 109 | lstring.h | ||
| 110 | liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h | 107 | liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h |
| 111 | llex.o: llex.c lglobal.h lobject.h lua.h llex.h lzio.h lmem.h \ | 108 | llex.o: llex.c llex.h lobject.h lua.h lzio.h lmem.h lparser.h \ |
| 112 | lparser.h lstring.h ltokens.h luadebug.h | 109 | lstring.h ltokens.h luadebug.h |
| 113 | lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h | 110 | lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h |
| 114 | lmem.o: lmem.c lmem.h lua.h | 111 | lmem.o: lmem.c lmem.h lua.h |
| 115 | lobject.o: lobject.c lobject.h lua.h | 112 | lobject.o: lobject.c lobject.h lua.h |
| 116 | lparser.o: lparser.c lauxlib.h lua.h ldo.h lobject.h lfunc.h lglobal.h \ | 113 | lparser.o: lparser.c lauxlib.h lua.h ldo.h lobject.h lfunc.h llex.h \ |
| 117 | llex.h lzio.h lmem.h lopcodes.h lparser.h lstring.h luadebug.h | 114 | lzio.h lmem.h lopcodes.h lparser.h lstring.h luadebug.h |
| 118 | lstring.o: lstring.c lmem.h lobject.h lua.h lstring.h | 115 | lstring.o: lstring.c lmem.h lobject.h lua.h lstring.h |
| 119 | lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h | 116 | lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h |
| 120 | ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h ltable.h | 117 | ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h ltable.h |
| 121 | ltm.o: ltm.c lauxlib.h lua.h ldo.h lobject.h lmem.h ltm.h lapi.h | 118 | ltm.o: ltm.c lauxlib.h lua.h ldo.h lobject.h lmem.h ltm.h lapi.h |
| 122 | lua.o: lua.c lua.h lualib.h luadebug.h | 119 | lua.o: lua.c lua.h luadebug.h lualib.h |
| 123 | lundump.o: lundump.c lundump.h lobject.h lua.h lzio.h | 120 | lundump.o: lundump.c lundump.h lobject.h lua.h lzio.h |
| 124 | lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lfunc.h lgc.h lglobal.h \ | 121 | lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lfunc.h lgc.h lmem.h \ |
| 125 | lmem.h lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h | 122 | lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h |
| 126 | lzio.o: lzio.c lzio.h | 123 | lzio.o: lzio.c lzio.h |
