diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-07 13:19:51 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-07 13:19:51 -0200 |
| commit | 62ec3797d518f55513c992fe1819dc5dbb072226 (patch) | |
| tree | 49e67df51adfdf3887cf0a835b263d2e96a42128 | |
| parent | 0a5dce57044b782a211ce53896967b90fb81ce69 (diff) | |
| download | lua-62ec3797d518f55513c992fe1819dc5dbb072226.tar.gz lua-62ec3797d518f55513c992fe1819dc5dbb072226.tar.bz2 lua-62ec3797d518f55513c992fe1819dc5dbb072226.zip | |
inheritance is not built-in
| -rw-r--r-- | hash.c | 40 |
1 files changed, 5 insertions, 35 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** hash manager for lua | 3 | ** hash manager for lua |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_hash="$Id: hash.c,v 2.11 1994/11/02 20:29:09 roberto Exp roberto $"; | 6 | char *rcs_hash="$Id: hash.c,v 2.12 1994/11/03 22:20:15 roberto Exp $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
| @@ -235,47 +235,17 @@ static void rehash (Hash *t) | |||
| 235 | } | 235 | } |
| 236 | 236 | ||
| 237 | /* | 237 | /* |
| 238 | ** If the hash node is present, return its pointer, otherwise search | 238 | ** If the hash node is present, return its pointer, otherwise return |
| 239 | ** the node at parent table, recursively, if there is parent. | 239 | ** null. |
| 240 | ** If no parent and the node is not present, return a static nil object. | ||
| 241 | */ | 240 | */ |
| 242 | Object *lua_hashget (Hash *t, Object *ref) | 241 | Object *lua_hashget (Hash *t, Object *ref) |
| 243 | { | 242 | { |
| 244 | static int count = 1000; | ||
| 245 | static Object nil_obj = {LUA_T_NIL, {NULL}}; | ||
| 246 | int h = present(t, ref); | 243 | int h = present(t, ref); |
| 247 | if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h)); | 244 | if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h)); |
| 248 | if (--count == 0) | 245 | else return NULL; |
| 249 | lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)"); | ||
| 250 | { /* check "parent" or "godparent" field */ | ||
| 251 | Hash *p; | ||
| 252 | Object parent; | ||
| 253 | Object godparent; | ||
| 254 | tag(&parent) = LUA_T_STRING; svalue(&parent) = "parent"; | ||
| 255 | tag(&godparent) = LUA_T_STRING; svalue(&godparent) = "godparent"; | ||
| 256 | |||
| 257 | h = present(t, &parent); | ||
| 258 | p = tag(ref(node(t, h))) != LUA_T_NIL && tag(val(node(t, h))) == LUA_T_ARRAY ? | ||
| 259 | avalue(val(node(t, h))) : NULL; | ||
| 260 | if (p != NULL) | ||
| 261 | { | ||
| 262 | Object *r = lua_hashget(p, ref); | ||
| 263 | if (tag(r) != LUA_T_NIL) { count++; return r; } | ||
| 264 | } | ||
| 265 | |||
| 266 | h = present(t, &godparent); | ||
| 267 | p = tag(ref(node(t, h))) != LUA_T_NIL && tag(val(node(t, h))) == LUA_T_ARRAY ? | ||
| 268 | avalue(val(node(t, h))) : NULL; | ||
| 269 | if (p != NULL) | ||
| 270 | { | ||
| 271 | Object *r = lua_hashget(p, ref); | ||
| 272 | if (tag(r) != LUA_T_NIL) { count++; return r; } | ||
| 273 | } | ||
| 274 | } | ||
| 275 | count++; | ||
| 276 | return &nil_obj; | ||
| 277 | } | 246 | } |
| 278 | 247 | ||
| 248 | |||
| 279 | /* | 249 | /* |
| 280 | ** If the hash node is present, return its pointer, otherwise create a new | 250 | ** If the hash node is present, return its pointer, otherwise create a new |
| 281 | ** node for the given reference and also return its pointer. | 251 | ** node for the given reference and also return its pointer. |
