diff options
Diffstat (limited to 'ldump.c')
| -rw-r--r-- | ldump.c | 27 |
1 files changed, 18 insertions, 9 deletions
| @@ -31,7 +31,7 @@ typedef struct { | |||
| 31 | int strip; | 31 | int strip; |
| 32 | int status; | 32 | int status; |
| 33 | Table *h; /* table to track saved strings */ | 33 | Table *h; /* table to track saved strings */ |
| 34 | lua_Integer nstr; /* counter for counting saved strings */ | 34 | lua_Unsigned nstr; /* counter for counting saved strings */ |
| 35 | } DumpState; | 35 | } DumpState; |
| 36 | 36 | ||
| 37 | 37 | ||
| @@ -87,12 +87,12 @@ static void dumpByte (DumpState *D, int y) { | |||
| 87 | ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. | 87 | ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. |
| 88 | ** (The "+6" rounds up the division.) | 88 | ** (The "+6" rounds up the division.) |
| 89 | */ | 89 | */ |
| 90 | #define DIBS ((l_numbits(size_t) + 6) / 7) | 90 | #define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) |
| 91 | 91 | ||
| 92 | /* | 92 | /* |
| 93 | ** Dumps an unsigned integer using the MSB Varint encoding | 93 | ** Dumps an unsigned integer using the MSB Varint encoding |
| 94 | */ | 94 | */ |
| 95 | static void dumpVarint (DumpState *D, size_t x) { | 95 | static void dumpVarint (DumpState *D, lua_Unsigned x) { |
| 96 | lu_byte buff[DIBS]; | 96 | lu_byte buff[DIBS]; |
| 97 | unsigned n = 1; | 97 | unsigned n = 1; |
| 98 | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ | 98 | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ |
| @@ -103,12 +103,13 @@ static void dumpVarint (DumpState *D, size_t x) { | |||
| 103 | 103 | ||
| 104 | 104 | ||
| 105 | static void dumpSize (DumpState *D, size_t sz) { | 105 | static void dumpSize (DumpState *D, size_t sz) { |
| 106 | dumpVarint(D, sz); | 106 | dumpVarint(D, cast(lua_Unsigned, sz)); |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | |||
| 109 | static void dumpInt (DumpState *D, int x) { | 110 | static void dumpInt (DumpState *D, int x) { |
| 110 | lua_assert(x >= 0); | 111 | lua_assert(x >= 0); |
| 111 | dumpVarint(D, cast_sizet(x)); | 112 | dumpVarint(D, cast_uint(x)); |
| 112 | } | 113 | } |
| 113 | 114 | ||
| 114 | 115 | ||
| @@ -117,8 +118,16 @@ static void dumpNumber (DumpState *D, lua_Number x) { | |||
| 117 | } | 118 | } |
| 118 | 119 | ||
| 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 | */ | ||
| 120 | static void dumpInteger (DumpState *D, lua_Integer x) { | 127 | static void dumpInteger (DumpState *D, lua_Integer x) { |
| 121 | dumpVar(D, x); | 128 | lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) |
| 129 | : (2u * ~l_castS2U(x)) + 1; | ||
| 130 | dumpVarint(D, cx); | ||
| 122 | } | 131 | } |
| 123 | 132 | ||
| 124 | 133 | ||
| @@ -136,8 +145,8 @@ static void dumpString (DumpState *D, TString *ts) { | |||
| 136 | TValue idx; | 145 | TValue idx; |
| 137 | int tag = luaH_getstr(D->h, ts, &idx); | 146 | int tag = luaH_getstr(D->h, ts, &idx); |
| 138 | if (!tagisempty(tag)) { /* string already saved? */ | 147 | if (!tagisempty(tag)) { /* string already saved? */ |
| 139 | dumpSize(D, 1); /* reuse a saved string */ | 148 | dumpVarint(D, 1); /* reuse a saved string */ |
| 140 | dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ | 149 | dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ |
| 141 | } | 150 | } |
| 142 | else { /* must write and save the string */ | 151 | else { /* must write and save the string */ |
| 143 | TValue key, value; /* to save the string in the hash */ | 152 | TValue key, value; /* to save the string in the hash */ |
| @@ -147,7 +156,7 @@ static void dumpString (DumpState *D, TString *ts) { | |||
| 147 | dumpVector(D, s, size + 1); /* include ending '\0' */ | 156 | dumpVector(D, s, size + 1); /* include ending '\0' */ |
| 148 | D->nstr++; /* one more saved string */ | 157 | D->nstr++; /* one more saved string */ |
| 149 | setsvalue(D->L, &key, ts); /* the string is the key */ | 158 | setsvalue(D->L, &key, ts); /* the string is the key */ |
| 150 | setivalue(&value, D->nstr); /* its index is the value */ | 159 | setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ |
| 151 | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ | 160 | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ |
| 152 | /* integer value does not need barrier */ | 161 | /* integer value does not need barrier */ |
| 153 | } | 162 | } |
