diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-05-05 12:53:23 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-05-05 12:53:23 -0300 |
| commit | 25b4e35ee894f6075efff93e39cd1e3ec220f7cc (patch) | |
| tree | dc4bcccdc32c4623e778499e925c0f8d3a52915a | |
| parent | 7e1facaa4e06a3cf120d03d30ea295962095e462 (diff) | |
| download | lua-25b4e35ee894f6075efff93e39cd1e3ec220f7cc.tar.gz lua-25b4e35ee894f6075efff93e39cd1e3ec220f7cc.tar.bz2 lua-25b4e35ee894f6075efff93e39cd1e3ec220f7cc.zip | |
new macros abstracting TValue representation (to easy change to
other representations)
| -rw-r--r-- | lobject.h | 91 |
1 files changed, 48 insertions, 43 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 2.50 2011/05/03 16:01:57 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.51 2011/05/04 17:04:06 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 | */ |
| @@ -99,6 +99,9 @@ typedef struct lua_TValue { | |||
| 99 | #define NILCONSTANT {NULL}, LUA_TNIL | 99 | #define NILCONSTANT {NULL}, LUA_TNIL |
| 100 | 100 | ||
| 101 | 101 | ||
| 102 | #define val_(o) ((o)->value_) | ||
| 103 | |||
| 104 | |||
| 102 | /* raw type tag of a TValue */ | 105 | /* raw type tag of a TValue */ |
| 103 | #define rttype(o) ((o)->tt_) | 106 | #define rttype(o) ((o)->tt_) |
| 104 | 107 | ||
| @@ -111,10 +114,10 @@ typedef struct lua_TValue { | |||
| 111 | 114 | ||
| 112 | 115 | ||
| 113 | /* Macros to test type */ | 116 | /* Macros to test type */ |
| 117 | #define ttisnumber(o) (rttype(o) == LUA_TNUMBER) | ||
| 114 | #define ttisnil(o) (rttype(o) == LUA_TNIL) | 118 | #define ttisnil(o) (rttype(o) == LUA_TNIL) |
| 115 | #define ttisboolean(o) (rttype(o) == LUA_TBOOLEAN) | 119 | #define ttisboolean(o) (rttype(o) == LUA_TBOOLEAN) |
| 116 | #define ttislightuserdata(o) (rttype(o) == LUA_TLIGHTUSERDATA) | 120 | #define ttislightuserdata(o) (rttype(o) == LUA_TLIGHTUSERDATA) |
| 117 | #define ttisnumber(o) (rttype(o) == LUA_TNUMBER) | ||
| 118 | #define ttisstring(o) (rttype(o) == ctb(LUA_TSTRING)) | 121 | #define ttisstring(o) (rttype(o) == ctb(LUA_TSTRING)) |
| 119 | #define ttistable(o) (rttype(o) == ctb(LUA_TTABLE)) | 122 | #define ttistable(o) (rttype(o) == ctb(LUA_TTABLE)) |
| 120 | #define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION) | 123 | #define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION) |
| @@ -127,18 +130,18 @@ typedef struct lua_TValue { | |||
| 127 | #define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) | 130 | #define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) |
| 128 | 131 | ||
| 129 | /* Macros to access values */ | 132 | /* Macros to access values */ |
| 130 | #define gcvalue(o) check_exp(iscollectable(o), (o)->value_.gc) | 133 | #define nvalue(o) check_exp(ttisnumber(o), val_(o).n) |
| 131 | #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value_.p) | 134 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) |
| 132 | #define nvalue(o) check_exp(ttisnumber(o), (o)->value_.n) | 135 | #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) |
| 133 | #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value_.gc->ts) | 136 | #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) |
| 134 | #define tsvalue(o) (&rawtsvalue(o)->tsv) | 137 | #define tsvalue(o) (&rawtsvalue(o)->tsv) |
| 135 | #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u) | 138 | #define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u) |
| 136 | #define uvalue(o) (&rawuvalue(o)->uv) | 139 | #define uvalue(o) (&rawuvalue(o)->uv) |
| 137 | #define clvalue(o) check_exp(ttisclosure(o), &(o)->value_.gc->cl) | 140 | #define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl) |
| 138 | #define fvalue(o) check_exp(ttislcf(o), (o)->value_.f) | 141 | #define fvalue(o) check_exp(ttislcf(o), val_(o).f) |
| 139 | #define hvalue(o) check_exp(ttistable(o), &(o)->value_.gc->h) | 142 | #define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h) |
| 140 | #define bvalue(o) check_exp(ttisboolean(o), (o)->value_.b) | 143 | #define bvalue(o) check_exp(ttisboolean(o), val_(o).b) |
| 141 | #define thvalue(o) check_exp(ttisthread(o), &(o)->value_.gc->th) | 144 | #define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th) |
| 142 | 145 | ||
| 143 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) | 146 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) |
| 144 | 147 | ||
| @@ -156,64 +159,66 @@ typedef struct lua_TValue { | |||
| 156 | 159 | ||
| 157 | 160 | ||
| 158 | /* Macros to set values */ | 161 | /* Macros to set values */ |
| 159 | #define setnilvalue(obj) ((obj)->tt_=LUA_TNIL) | 162 | #define settt_(o,t) ((o)->tt_=(t)) |
| 160 | 163 | ||
| 161 | #define setnvalue(obj,x) \ | 164 | #define setnvalue(obj,x) \ |
| 162 | { TValue *io_=(obj); io_->value_.n=(x); io_->tt_=LUA_TNUMBER; } | 165 | { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMBER); } |
| 163 | 166 | ||
| 164 | #define setfvalue(obj,x) \ | 167 | #define changenvalue(o,x) check_exp(ttisnumber(o), val_(o).n=(x)) |
| 165 | { TValue *io_=(obj); io_->value_.f=(x); io_->tt_=LUA_TLCF; } | 168 | |
| 169 | #define setnilvalue(obj) settt_(obj, LUA_TNIL) | ||
| 166 | 170 | ||
| 167 | #define changenvalue(o,x) check_exp(ttisnumber(o), (o)->value_.n=(x)) | 171 | #define setfvalue(obj,x) \ |
| 172 | { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } | ||
| 168 | 173 | ||
| 169 | #define setpvalue(obj,x) \ | 174 | #define setpvalue(obj,x) \ |
| 170 | { TValue *io_=(obj); io_->value_.p=(x); io_->tt_=LUA_TLIGHTUSERDATA; } | 175 | { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } |
| 171 | 176 | ||
| 172 | #define setbvalue(obj,x) \ | 177 | #define setbvalue(obj,x) \ |
| 173 | { TValue *io_=(obj); io_->value_.b=(x); io_->tt_=LUA_TBOOLEAN; } | 178 | { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } |
| 174 | 179 | ||
| 175 | #define setgcovalue(L,obj,x) \ | 180 | #define setgcovalue(L,obj,x) \ |
| 176 | { TValue *io_=(obj); GCObject *i_g=(x); \ | 181 | { TValue *io=(obj); GCObject *i_g=(x); \ |
| 177 | io_->value_.gc=i_g; io_->tt_=ctb(gch(i_g)->tt); } | 182 | val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); } |
| 178 | 183 | ||
| 179 | #define setsvalue(L,obj,x) \ | 184 | #define setsvalue(L,obj,x) \ |
| 180 | { TValue *io_=(obj); \ | 185 | { TValue *io=(obj); \ |
| 181 | io_->value_.gc=cast(GCObject *, (x)); io_->tt_=ctb(LUA_TSTRING); \ | 186 | val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \ |
| 182 | checkliveness(G(L),io_); } | 187 | checkliveness(G(L),io); } |
| 183 | 188 | ||
| 184 | #define setuvalue(L,obj,x) \ | 189 | #define setuvalue(L,obj,x) \ |
| 185 | { TValue *io_=(obj); \ | 190 | { TValue *io=(obj); \ |
| 186 | io_->value_.gc=cast(GCObject *, (x)); io_->tt_=ctb(LUA_TUSERDATA); \ | 191 | val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \ |
| 187 | checkliveness(G(L),io_); } | 192 | checkliveness(G(L),io); } |
| 188 | 193 | ||
| 189 | #define setthvalue(L,obj,x) \ | 194 | #define setthvalue(L,obj,x) \ |
| 190 | { TValue *io_=(obj); \ | 195 | { TValue *io=(obj); \ |
| 191 | io_->value_.gc=cast(GCObject *, (x)); io_->tt_=ctb(LUA_TTHREAD); \ | 196 | val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \ |
| 192 | checkliveness(G(L),io_); } | 197 | checkliveness(G(L),io); } |
| 193 | 198 | ||
| 194 | #define setclvalue(L,obj,x) \ | 199 | #define setclvalue(L,obj,x) \ |
| 195 | { TValue *io_=(obj); \ | 200 | { TValue *io=(obj); \ |
| 196 | io_->value_.gc=cast(GCObject *, (x)); io_->tt_=ctb(LUA_TFUNCTION); \ | 201 | val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TFUNCTION)); \ |
| 197 | checkliveness(G(L),io_); } | 202 | checkliveness(G(L),io); } |
| 198 | 203 | ||
| 199 | #define sethvalue(L,obj,x) \ | 204 | #define sethvalue(L,obj,x) \ |
| 200 | { TValue *io_=(obj); \ | 205 | { TValue *io=(obj); \ |
| 201 | io_->value_.gc=cast(GCObject *, (x)); io_->tt_=ctb(LUA_TTABLE); \ | 206 | val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \ |
| 202 | checkliveness(G(L),io_); } | 207 | checkliveness(G(L),io); } |
| 203 | 208 | ||
| 204 | #define setptvalue(L,obj,x) \ | 209 | #define setptvalue(L,obj,x) \ |
| 205 | { TValue *io_=(obj); \ | 210 | { TValue *io=(obj); \ |
| 206 | io_->value_.gc=cast(GCObject *, (x)); io_->tt_=ctb(LUA_TPROTO); \ | 211 | val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \ |
| 207 | checkliveness(G(L),io_); } | 212 | checkliveness(G(L),io); } |
| 208 | 213 | ||
| 209 | #define setdeadvalue(obj) ((obj)->tt_=ctb(LUA_TDEADKEY)) | 214 | #define setdeadvalue(obj) settt_(obj, ctb(LUA_TDEADKEY)) |
| 210 | 215 | ||
| 211 | 216 | ||
| 212 | 217 | ||
| 213 | #define setobj(L,obj1,obj2) \ | 218 | #define setobj(L,obj1,obj2) \ |
| 214 | { const TValue *o2_=(obj2); TValue *o1_=(obj1); \ | 219 | { const TValue *io2=(obj2); TValue *io1=(obj1); \ |
| 215 | o1_->value_ = o2_->value_; o1_->tt_=o2_->tt_; \ | 220 | io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ |
| 216 | checkliveness(G(L),o1_); } | 221 | checkliveness(G(L),io1); } |
| 217 | 222 | ||
| 218 | 223 | ||
| 219 | /* | 224 | /* |
