lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
lua / ldump.c
| 1 | /* |
| 2 | ** $Id: ldump.c $ |
| 3 | ** save precompiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h |
| 5 | */ |
| 6 | |
| 7 | #define ldump_c |
| 8 | #define LUA_CORE |
| 9 | |
| 10 | #include "lprefix.h" |
| 11 | |
| 12 | |
| 13 | #include <limits.h> |
| 14 | #include <stddef.h> |
| 15 | |
| 16 | #include "lua.h" |
| 17 | |
| 18 | #include "lapi.h" |
| 19 | #include "lgc.h" |
| 20 | #include "lobject.h" |
| 21 | #include "lstate.h" |
| 22 | #include "ltable.h" |
| 23 | #include "lundump.h" |
| 24 | |
| 25 | |
| 26 | typedef struct { |
| 27 | lua_State *L; |
| 28 | lua_Writer writer; |
| 29 | void *data; |
| 30 | size_t offset; /* current position relative to beginning of dump */ |
| 31 | int strip; |
| 32 | int status; |
| 33 | Table *h; /* table to track saved strings */ |
| 34 | lua_Unsigned nstr; /* counter for counting saved strings */ |
| 35 | } DumpState; |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | ** All high-level dumps go through dumpVector; you can change it to |
| 40 | ** change the endianness of the result |
| 41 | */ |
| 42 | #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) |
| 43 | |
| 44 | #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | ** Dump the block of memory pointed by 'b' with given 'size'. |
| 49 | ** 'b' should not be NULL, except for the last call signaling the end |
| 50 | ** of the dump. |
| 51 | */ |
| 52 | static void dumpBlock (DumpState *D, const void *b, size_t size) { |
| 53 | if (D->status == 0) { /* do not write anything after an error */ |
| 54 | lua_unlock(D->L); |
| 55 | D->status = (*D->writer)(D->L, b, size, D->data); |
| 56 | lua_lock(D->L); |
| 57 | D->offset += size; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /* |
| 63 | ** Dump enough zeros to ensure that current position is a multiple of |
| 64 | ** 'align'. |
| 65 | */ |
| 66 | static void dumpAlign (DumpState *D, unsigned align) { |
| 67 | unsigned padding = align - cast_uint(D->offset % align); |
| 68 | if (padding < align) { /* padding == align means no padding */ |
| 69 | static lua_Integer paddingContent = 0; |
| 70 | lua_assert(align <= sizeof(lua_Integer)); |
| 71 | dumpBlock(D, &paddingContent, padding); |
| 72 | } |
| 73 | lua_assert(D->offset % align == 0); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | #define dumpVar(D,x) dumpVector(D,&x,1) |
| 78 | |
| 79 | |
| 80 | static void dumpByte (DumpState *D, int y) { |
| 81 | lu_byte x = (lu_byte)y; |
| 82 | dumpVar(D, x); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. |
| 88 | ** (The "+6" rounds up the division.) |
| 89 | */ |
| 90 | #define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) |
| 91 | |
| 92 | /* |
| 93 | ** Dumps an unsigned integer using the MSB Varint encoding |
| 94 | */ |
| 95 | static void dumpVarint (DumpState *D, lua_Unsigned x) { |
| 96 | lu_byte buff[DIBS]; |
| 97 | unsigned n = 1; |
| 98 | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ |
| 99 | while ((x >>= 7) != 0) /* fill other bytes in reverse order */ |
| 100 | buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80); |
| 101 | dumpVector(D, buff + DIBS - n, n); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static void dumpSize (DumpState *D, size_t sz) { |
| 106 | dumpVarint(D, cast(lua_Unsigned, sz)); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void dumpInt (DumpState *D, int x) { |
| 111 | lua_assert(x >= 0); |
| 112 | dumpVarint(D, cast_uint(x)); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static void dumpNumber (DumpState *D, lua_Number x) { |
| 117 | dumpVar(D, x); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | ** Signed integers are coded to keep small values small. (Coding -1 as |
| 123 | ** 0xfff...fff would use too many bytes to save a quite common value.) |
| 124 | ** A non-negative x is coded as 2x; a negative x is coded as -2x - 1. |
| 125 | ** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...) |
| 126 | */ |
| 127 | static void dumpInteger (DumpState *D, lua_Integer x) { |
| 128 | lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) |
| 129 | : (2u * ~l_castS2U(x)) + 1; |
| 130 | dumpVarint(D, cx); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | ** Dump a String. First dump its "size": |
| 136 | ** size==0 is followed by an index and means "reuse saved string with |
| 137 | ** that index"; index==0 means NULL. |
| 138 | ** size>=1 is followed by the string contents with real size==size-1 and |
| 139 | ** means that string, which will be saved with the next available index. |
| 140 | ** The real size does not include the ending '\0' (which is not dumped), |
| 141 | ** so adding 1 to it cannot overflow a size_t. |
| 142 | */ |
| 143 | static void dumpString (DumpState *D, TString *ts) { |
| 144 | if (ts == NULL) { |
| 145 | dumpVarint(D, 0); /* will "reuse" NULL */ |
| 146 | dumpVarint(D, 0); /* special index for NULL */ |
| 147 | } |
| 148 | else { |
| 149 | TValue idx; |
| 150 | int tag = luaH_getstr(D->h, ts, &idx); |
| 151 | if (!tagisempty(tag)) { /* string already saved? */ |
| 152 | dumpVarint(D, 0); /* reuse a saved string */ |
| 153 | dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ |
| 154 | } |
| 155 | else { /* must write and save the string */ |
| 156 | TValue key, value; /* to save the string in the hash */ |
| 157 | size_t size; |
| 158 | const char *s = getlstr(ts, size); |
| 159 | dumpSize(D, size + 1); |
| 160 | dumpVector(D, s, size + 1); /* include ending '\0' */ |
| 161 | D->nstr++; /* one more saved string */ |
| 162 | setsvalue(D->L, &key, ts); /* the string is the key */ |
| 163 | setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ |
| 164 | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ |
| 165 | /* integer value does not need barrier */ |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static void dumpCode (DumpState *D, const Proto *f) { |
| 172 | dumpInt(D, f->sizecode); |
| 173 | dumpAlign(D, sizeof(f->code[0])); |
| 174 | lua_assert(f->code != NULL); |
| 175 | dumpVector(D, f->code, cast_uint(f->sizecode)); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static void dumpFunction (DumpState *D, const Proto *f); |
| 180 | |
| 181 | static void dumpConstants (DumpState *D, const Proto *f) { |
| 182 | int i; |
| 183 | int n = f->sizek; |
| 184 | dumpInt(D, n); |
| 185 | for (i = 0; i < n; i++) { |
| 186 | const TValue *o = &f->k[i]; |
| 187 | int tt = ttypetag(o); |
| 188 | dumpByte(D, tt); |
| 189 | switch (tt) { |
| 190 | case LUA_VNUMFLT: |
| 191 | dumpNumber(D, fltvalue(o)); |
| 192 | break; |
| 193 | case LUA_VNUMINT: |
| 194 | dumpInteger(D, ivalue(o)); |
| 195 | break; |
| 196 | case LUA_VSHRSTR: |
| 197 | case LUA_VLNGSTR: |
| 198 | dumpString(D, tsvalue(o)); |
| 199 | break; |
| 200 | default: |
| 201 | lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | static void dumpProtos (DumpState *D, const Proto *f) { |
| 208 | int i; |
| 209 | int n = f->sizep; |
| 210 | dumpInt(D, n); |
| 211 | for (i = 0; i < n; i++) |
| 212 | dumpFunction(D, f->p[i]); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static void dumpUpvalues (DumpState *D, const Proto *f) { |
| 217 | int i, n = f->sizeupvalues; |
| 218 | dumpInt(D, n); |
| 219 | for (i = 0; i < n; i++) { |
| 220 | dumpByte(D, f->upvalues[i].instack); |
| 221 | dumpByte(D, f->upvalues[i].idx); |
| 222 | dumpByte(D, f->upvalues[i].kind); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static void dumpDebug (DumpState *D, const Proto *f) { |
| 228 | int i, n; |
| 229 | n = (D->strip) ? 0 : f->sizelineinfo; |
| 230 | dumpInt(D, n); |
| 231 | if (f->lineinfo != NULL) |
| 232 | dumpVector(D, f->lineinfo, cast_uint(n)); |
| 233 | n = (D->strip) ? 0 : f->sizeabslineinfo; |
| 234 | dumpInt(D, n); |
| 235 | if (n > 0) { |
| 236 | /* 'abslineinfo' is an array of structures of int's */ |
| 237 | dumpAlign(D, sizeof(int)); |
| 238 | dumpVector(D, f->abslineinfo, cast_uint(n)); |
| 239 | } |
| 240 | n = (D->strip) ? 0 : f->sizelocvars; |
| 241 | dumpInt(D, n); |
| 242 | for (i = 0; i < n; i++) { |
| 243 | dumpString(D, f->locvars[i].varname); |
| 244 | dumpInt(D, f->locvars[i].startpc); |
| 245 | dumpInt(D, f->locvars[i].endpc); |
| 246 | } |
| 247 | n = (D->strip) ? 0 : f->sizeupvalues; |
| 248 | dumpInt(D, n); |
| 249 | for (i = 0; i < n; i++) |
| 250 | dumpString(D, f->upvalues[i].name); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | static void dumpFunction (DumpState *D, const Proto *f) { |
| 255 | dumpInt(D, f->linedefined); |
| 256 | dumpInt(D, f->lastlinedefined); |
| 257 | dumpByte(D, f->numparams); |
| 258 | dumpByte(D, f->flag); |
| 259 | dumpByte(D, f->maxstacksize); |
| 260 | dumpCode(D, f); |
| 261 | dumpConstants(D, f); |
| 262 | dumpUpvalues(D, f); |
| 263 | dumpProtos(D, f); |
| 264 | dumpString(D, D->strip ? NULL : f->source); |
| 265 | dumpDebug(D, f); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | #define dumpNumInfo(D, tvar, value) \ |
| 270 | { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); } |
| 271 | |
| 272 | |
| 273 | static void dumpHeader (DumpState *D) { |
| 274 | dumpLiteral(D, LUA_SIGNATURE); |
| 275 | dumpByte(D, LUAC_VERSION); |
| 276 | dumpByte(D, LUAC_FORMAT); |
| 277 | dumpLiteral(D, LUAC_DATA); |
| 278 | dumpNumInfo(D, int, LUAC_INT); |
| 279 | dumpNumInfo(D, Instruction, LUAC_INST); |
| 280 | dumpNumInfo(D, lua_Integer, LUAC_INT); |
| 281 | dumpNumInfo(D, lua_Number, LUAC_NUM); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /* |
| 286 | ** dump Lua function as precompiled chunk |
| 287 | */ |
| 288 | int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data, |
| 289 | int strip) { |
| 290 | DumpState D; |
| 291 | D.h = luaH_new(L); /* aux. table to keep strings already dumped */ |
| 292 | sethvalue2s(L, L->top.p, D.h); /* anchor it */ |
| 293 | L->top.p++; |
| 294 | D.L = L; |
| 295 | D.writer = w; |
| 296 | D.offset = 0; |
| 297 | D.data = data; |
| 298 | D.strip = strip; |
| 299 | D.status = 0; |
| 300 | D.nstr = 0; |
| 301 | dumpHeader(&D); |
| 302 | dumpByte(&D, f->sizeupvalues); |
| 303 | dumpFunction(&D, f); |
| 304 | dumpBlock(&D, NULL, 0); /* signal end of dump */ |
| 305 | return D.status; |
| 306 | } |
| 307 |