diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-03-13 09:16:51 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-03-13 09:16:51 -0300 |
| commit | cc2b66c85687b095e68304c010b59851ca4093e1 (patch) | |
| tree | e85314ebbc0a27d4ece319c0313cebf77a62a96b | |
| parent | 65b07dd53d7938a60112fc4473f5cad3473e3534 (diff) | |
| download | lua-cc2b66c85687b095e68304c010b59851ca4093e1.tar.gz lua-cc2b66c85687b095e68304c010b59851ca4093e1.tar.bz2 lua-cc2b66c85687b095e68304c010b59851ca4093e1.zip | |
Removed type 'varint_t'
size_t should be big enough to count the number of strings in a dump.
(And, by definition, it is big enough to count the length of each
string.)
| -rw-r--r-- | ldump.c | 22 | ||||
| -rw-r--r-- | lundump.c | 21 | ||||
| -rw-r--r-- | lundump.h | 12 |
3 files changed, 23 insertions, 32 deletions
| @@ -30,7 +30,7 @@ typedef struct { | |||
| 30 | int strip; | 30 | int strip; |
| 31 | int status; | 31 | int status; |
| 32 | Table *h; /* table to track saved strings */ | 32 | Table *h; /* table to track saved strings */ |
| 33 | lua_Unsigned nstr; /* counter to number saved strings */ | 33 | lua_Integer nstr; /* counter for counting saved strings */ |
| 34 | } DumpState; | 34 | } DumpState; |
| 35 | 35 | ||
| 36 | 36 | ||
| @@ -86,12 +86,12 @@ static void dumpByte (DumpState *D, int y) { | |||
| 86 | ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. | 86 | ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. |
| 87 | ** (The "+6" rounds up the division.) | 87 | ** (The "+6" rounds up the division.) |
| 88 | */ | 88 | */ |
| 89 | #define DIBS ((sizeof(varint_t) * CHAR_BIT + 6) / 7) | 89 | #define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) |
| 90 | 90 | ||
| 91 | /* | 91 | /* |
| 92 | ** Dumps an unsigned integer using the MSB Varint encoding | 92 | ** Dumps an unsigned integer using the MSB Varint encoding |
| 93 | */ | 93 | */ |
| 94 | static void dumpVarint (DumpState *D, varint_t x) { | 94 | static void dumpVarint (DumpState *D, size_t x) { |
| 95 | lu_byte buff[DIBS]; | 95 | lu_byte buff[DIBS]; |
| 96 | int n = 1; | 96 | int n = 1; |
| 97 | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ | 97 | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ |
| @@ -101,9 +101,13 @@ static void dumpVarint (DumpState *D, varint_t x) { | |||
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | static void dumpSize (DumpState *D, size_t sz) { | ||
| 105 | dumpVarint(D, sz); | ||
| 106 | } | ||
| 107 | |||
| 104 | static void dumpInt (DumpState *D, int x) { | 108 | static void dumpInt (DumpState *D, int x) { |
| 105 | lua_assert(x >= 0); | 109 | lua_assert(x >= 0); |
| 106 | dumpVarint(D, x); | 110 | dumpVarint(D, cast(size_t, x)); |
| 107 | } | 111 | } |
| 108 | 112 | ||
| 109 | 113 | ||
| @@ -126,22 +130,22 @@ static void dumpInteger (DumpState *D, lua_Integer x) { | |||
| 126 | */ | 130 | */ |
| 127 | static void dumpString (DumpState *D, TString *ts) { | 131 | static void dumpString (DumpState *D, TString *ts) { |
| 128 | if (ts == NULL) | 132 | if (ts == NULL) |
| 129 | dumpVarint(D, 0); | 133 | dumpSize(D, 0); |
| 130 | else { | 134 | else { |
| 131 | TValue idx; | 135 | TValue idx; |
| 132 | if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ | 136 | if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ |
| 133 | dumpVarint(D, 1); /* reuse a saved string */ | 137 | dumpSize(D, 1); /* reuse a saved string */ |
| 134 | dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ | 138 | dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ |
| 135 | } | 139 | } |
| 136 | else { /* must write and save the string */ | 140 | else { /* must write and save the string */ |
| 137 | TValue key, value; /* to save the string in the hash */ | 141 | TValue key, value; /* to save the string in the hash */ |
| 138 | size_t size; | 142 | size_t size; |
| 139 | const char *s = getlstr(ts, size); | 143 | const char *s = getlstr(ts, size); |
| 140 | dumpVarint(D, size + 2); | 144 | dumpSize(D, size + 2); |
| 141 | dumpVector(D, s, size + 1); /* include ending '\0' */ | 145 | dumpVector(D, s, size + 1); /* include ending '\0' */ |
| 142 | D->nstr++; /* one more saved string */ | 146 | D->nstr++; /* one more saved string */ |
| 143 | setsvalue(D->L, &key, ts); /* the string is the key */ | 147 | setsvalue(D->L, &key, ts); /* the string is the key */ |
| 144 | setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ | 148 | setivalue(&value, D->nstr); /* its index is the value */ |
| 145 | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ | 149 | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ |
| 146 | /* integer value does not need barrier */ | 150 | /* integer value does not need barrier */ |
| 147 | } | 151 | } |
| @@ -36,8 +36,8 @@ typedef struct { | |||
| 36 | ZIO *Z; | 36 | ZIO *Z; |
| 37 | const char *name; | 37 | const char *name; |
| 38 | Table *h; /* list for string reuse */ | 38 | Table *h; /* list for string reuse */ |
| 39 | size_t offset; /* current position relative to beginning of dump */ | 39 | lu_mem offset; /* current position relative to beginning of dump */ |
| 40 | lua_Unsigned nstr; /* number of strings in the list */ | 40 | lua_Integer nstr; /* number of strings in the list */ |
| 41 | lu_byte fixed; /* dump is fixed in memory */ | 41 | lu_byte fixed; /* dump is fixed in memory */ |
| 42 | } LoadState; | 42 | } LoadState; |
| 43 | 43 | ||
| @@ -94,13 +94,13 @@ static lu_byte loadByte (LoadState *S) { | |||
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | 96 | ||
| 97 | static varint_t loadVarint (LoadState *S, varint_t limit) { | 97 | static size_t loadVarint (LoadState *S, size_t limit) { |
| 98 | varint_t x = 0; | 98 | size_t x = 0; |
| 99 | int b; | 99 | int b; |
| 100 | limit >>= 7; | 100 | limit >>= 7; |
| 101 | do { | 101 | do { |
| 102 | b = loadByte(S); | 102 | b = loadByte(S); |
| 103 | if (x >= limit) | 103 | if (x > limit) |
| 104 | error(S, "integer overflow"); | 104 | error(S, "integer overflow"); |
| 105 | x = (x << 7) | (b & 0x7f); | 105 | x = (x << 7) | (b & 0x7f); |
| 106 | } while ((b & 0x80) != 0); | 106 | } while ((b & 0x80) != 0); |
| @@ -109,12 +109,12 @@ static varint_t loadVarint (LoadState *S, varint_t limit) { | |||
| 109 | 109 | ||
| 110 | 110 | ||
| 111 | static size_t loadSize (LoadState *S) { | 111 | static size_t loadSize (LoadState *S) { |
| 112 | return cast_sizet(loadVarint(S, MAX_SIZET)); | 112 | return loadVarint(S, MAX_SIZET); |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | static int loadInt (LoadState *S) { | 116 | static int loadInt (LoadState *S) { |
| 117 | return cast_int(loadVarint(S, INT_MAX)); | 117 | return cast_int(loadVarint(S, cast_sizet(INT_MAX))); |
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | 120 | ||
| @@ -148,10 +148,9 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { | |||
| 148 | return; | 148 | return; |
| 149 | } | 149 | } |
| 150 | else if (size == 1) { /* previously saved string? */ | 150 | else if (size == 1) { /* previously saved string? */ |
| 151 | /* get its index */ | 151 | lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ |
| 152 | lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED)); | ||
| 153 | TValue stv; | 152 | TValue stv; |
| 154 | luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ | 153 | luaH_getint(S->h, idx, &stv); /* get its value */ |
| 155 | *sl = ts = tsvalue(&stv); | 154 | *sl = ts = tsvalue(&stv); |
| 156 | luaC_objbarrier(L, p, ts); | 155 | luaC_objbarrier(L, p, ts); |
| 157 | return; /* do not save it again */ | 156 | return; /* do not save it again */ |
| @@ -175,7 +174,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { | |||
| 175 | /* add string to list of saved strings */ | 174 | /* add string to list of saved strings */ |
| 176 | S->nstr++; | 175 | S->nstr++; |
| 177 | setsvalue(L, &sv, ts); | 176 | setsvalue(L, &sv, ts); |
| 178 | luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); | 177 | luaH_setint(L, S->h, S->nstr, &sv); |
| 179 | luaC_objbarrierback(L, obj2gco(S->h), ts); | 178 | luaC_objbarrierback(L, obj2gco(S->h), ts); |
| 180 | } | 179 | } |
| 181 | 180 | ||
| @@ -28,18 +28,6 @@ | |||
| 28 | #define LUAC_FORMAT 0 /* this is the official format */ | 28 | #define LUAC_FORMAT 0 /* this is the official format */ |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | /* | ||
| 32 | ** Type to handle MSB Varint encoding: Try to get the largest unsigned | ||
| 33 | ** integer available. (It was enough to be the largest between size_t and | ||
| 34 | ** lua_Integer, but the C89 preprocessor knows nothing about size_t.) | ||
| 35 | */ | ||
| 36 | #if !defined(LUA_USE_C89) && defined(LLONG_MAX) | ||
| 37 | typedef unsigned long long varint_t; | ||
| 38 | #else | ||
| 39 | typedef unsigned long varint_t; | ||
| 40 | #endif | ||
| 41 | |||
| 42 | |||
| 43 | /* load one chunk; from lundump.c */ | 31 | /* load one chunk; from lundump.c */ |
| 44 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, | 32 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, |
| 45 | int fixed); | 33 | int fixed); |
