diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-22 17:49:29 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-22 17:49:29 -0300 |
| commit | b800c38b6920863725a279332f001bce1d457020 (patch) | |
| tree | e7eeaff456ea2545c52eb6c565afc154a25de218 | |
| parent | 5c0e79847c2a7a8006b0270210de56b53cb33564 (diff) | |
| download | lua-b800c38b6920863725a279332f001bce1d457020.tar.gz lua-b800c38b6920863725a279332f001bce1d457020.tar.bz2 lua-b800c38b6920863725a279332f001bce1d457020.zip | |
simpler code for settable and gettable
| -rw-r--r-- | lvm.c | 86 |
1 files changed, 39 insertions, 47 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.126 2000/08/11 16:17:28 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.127 2000/08/14 14:05:06 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 | */ |
| @@ -115,35 +115,31 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { | |||
| 115 | ** Receives the table at top-2 and the index at top-1. | 115 | ** Receives the table at top-2 and the index at top-1. |
| 116 | */ | 116 | */ |
| 117 | void luaV_gettable (lua_State *L, StkId top) { | 117 | void luaV_gettable (lua_State *L, StkId top) { |
| 118 | StkId table = top-2; | 118 | StkId t = top-2; |
| 119 | const TObject *im; | 119 | int tg; |
| 120 | if (ttype(table) != TAG_TABLE) { /* not a table, get gettable TM */ | 120 | if (ttype(t) == TAG_TABLE && /* `t' is a table? */ |
| 121 | im = luaT_getimbyObj(L, table, IM_GETTABLE); | 121 | ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ |
| 122 | if (ttype(im) == TAG_NIL) { | 122 | ttype(luaT_getim(L, tg, IM_GETTABLE)) == TAG_NIL)) { /* or no TM? */ |
| 123 | /* do a primitive get */ | ||
| 124 | const TObject *h = luaH_get(L, hvalue(t), t+1); | ||
| 125 | /* result is no nil or there is no `index' tag method? */ | ||
| 126 | const TObject *im; | ||
| 127 | if (ttype(h) != TAG_NIL || | ||
| 128 | (ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL)) | ||
| 129 | *t = *h; /* put result into table position */ | ||
| 130 | else { /* call `index' tag method */ | ||
| 123 | L->top = top; | 131 | L->top = top; |
| 124 | luaG_typeerror(L, table, "index"); | 132 | luaD_callTM(L, im, 2, 1); |
| 125 | } | 133 | } |
| 126 | } | 134 | } |
| 127 | else { /* object is a table... */ | 135 | else { /* try a 'gettable' TM */ |
| 128 | int tg = hvalue(table)->htag; | 136 | const TObject *im = luaT_getimbyObj(L, t, IM_GETTABLE); |
| 129 | im = luaT_getim(L, tg, IM_GETTABLE); | 137 | L->top = top; |
| 130 | if (ttype(im) == TAG_NIL) { /* and does not have a `gettable' TM */ | 138 | if (ttype(im) != TAG_NIL) /* call `gettable' tag method */ |
| 131 | const TObject *h = luaH_get(L, hvalue(table), table+1); | 139 | luaD_callTM(L, im, 2, 1); |
| 132 | if (ttype(h) == TAG_NIL && | 140 | else /* no tag method */ |
| 133 | (ttype(im=luaT_getim(L, tg, IM_INDEX)) != TAG_NIL)) { | 141 | luaG_typeerror(L, t, "index"); |
| 134 | /* result is nil and there is an `index' tag method */ | ||
| 135 | L->top = top; | ||
| 136 | luaD_callTM(L, im, 2, 1); /* calls it */ | ||
| 137 | } | ||
| 138 | else | ||
| 139 | *table = *h; /* `push' result into table position */ | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | /* else it has a `gettable' TM, go through to next command */ | ||
| 143 | } | 142 | } |
| 144 | /* object is not a table, or it has a `gettable' TM */ | ||
| 145 | L->top = top; | ||
| 146 | luaD_callTM(L, im, 2, 1); | ||
| 147 | } | 143 | } |
| 148 | 144 | ||
| 149 | 145 | ||
| @@ -151,30 +147,26 @@ void luaV_gettable (lua_State *L, StkId top) { | |||
| 151 | ** Receives table at *t, index at *(t+1) and value at `top'. | 147 | ** Receives table at *t, index at *(t+1) and value at `top'. |
| 152 | */ | 148 | */ |
| 153 | void luaV_settable (lua_State *L, StkId t, StkId top) { | 149 | void luaV_settable (lua_State *L, StkId t, StkId top) { |
| 154 | const TObject *im; | 150 | int tg; |
| 155 | if (ttype(t) != TAG_TABLE) { /* not a table, get `settable' method */ | 151 | if (ttype(t) == TAG_TABLE && /* `t' is a table? */ |
| 152 | ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ | ||
| 153 | ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */ | ||
| 154 | *luaH_set(L, hvalue(t), t+1) = *(top-1); /* do a primitive set */ | ||
| 155 | else { /* try a `settable' tag method */ | ||
| 156 | const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE); | ||
| 156 | L->top = top; | 157 | L->top = top; |
| 157 | im = luaT_getimbyObj(L, t, IM_SETTABLE); | 158 | if (ttype(im) != TAG_NIL) { |
| 158 | if (ttype(im) == TAG_NIL) | 159 | luaD_checkstack(L, 3); |
| 159 | luaG_typeerror(L, t, "index"); | 160 | *(top+2) = *(top-1); |
| 160 | } | 161 | *(top+1) = *(t+1); |
| 161 | else { /* object is a table... */ | 162 | *(top) = *t; |
| 162 | im = luaT_getim(L, hvalue(t)->htag, IM_SETTABLE); | 163 | *(top-1) = *im; |
| 163 | if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */ | 164 | L->top = top+3; |
| 164 | *luaH_set(L, hvalue(t), t+1) = *(top-1); | 165 | luaD_call(L, top-1, 0); /* call `settable' tag method */ |
| 165 | return; | ||
| 166 | } | 166 | } |
| 167 | /* else it has a `settable' method, go through to next command */ | 167 | else /* no tag method... */ |
| 168 | luaG_typeerror(L, t, "index"); | ||
| 168 | } | 169 | } |
| 169 | /* object is not a table, or it has a `settable' method */ | ||
| 170 | /* prepare arguments and call the tag method */ | ||
| 171 | luaD_checkstack(L, 3); | ||
| 172 | *(top+2) = *(top-1); | ||
| 173 | *(top+1) = *(t+1); | ||
| 174 | *(top) = *t; | ||
| 175 | *(top-1) = *im; | ||
| 176 | L->top = top+3; | ||
| 177 | luaD_call(L, top-1, 0); | ||
| 178 | } | 170 | } |
| 179 | 171 | ||
| 180 | 172 | ||
