diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-06-27 11:21:12 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-06-27 11:21:12 -0300 |
commit | 124bfd20817d4624d2b69a4dc41182485912b821 (patch) | |
tree | 313f1bfaa63f108b900ed620fb1d4f4e83036c0f | |
parent | b42430fd3a6200eaaf4020be90c4d47f7e251b67 (diff) | |
download | lua-124bfd20817d4624d2b69a4dc41182485912b821.tar.gz lua-124bfd20817d4624d2b69a4dc41182485912b821.tar.bz2 lua-124bfd20817d4624d2b69a4dc41182485912b821.zip |
dumping ints and size_ts compacted
-rw-r--r-- | ldump.c | 32 | ||||
-rw-r--r-- | lundump.c | 26 |
2 files changed, 38 insertions, 20 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp roberto $ | 2 | ** $Id: ldump.c,v 2.38 2017/06/27 11:35:31 roberto Exp roberto $ |
3 | ** save precompiled Lua chunks | 3 | ** save precompiled Lua chunks |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -55,8 +55,23 @@ static void DumpByte (int y, DumpState *D) { | |||
55 | } | 55 | } |
56 | 56 | ||
57 | 57 | ||
58 | /* DumpInt Buff Size */ | ||
59 | #define DIBS ((sizeof(size_t) * 8 / 7) + 1) | ||
60 | |||
61 | static void DumpSize (size_t x, DumpState *D) { | ||
62 | lu_byte buff[DIBS]; | ||
63 | int n = 0; | ||
64 | do { | ||
65 | buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ | ||
66 | x >>= 7; | ||
67 | } while (x != 0); | ||
68 | buff[DIBS - 1] |= 0x80; /* mark last byte */ | ||
69 | DumpVector(buff + DIBS - n, n, D); | ||
70 | } | ||
71 | |||
72 | |||
58 | static void DumpInt (int x, DumpState *D) { | 73 | static void DumpInt (int x, DumpState *D) { |
59 | DumpVar(x, D); | 74 | DumpSize(x, D); |
60 | } | 75 | } |
61 | 76 | ||
62 | 77 | ||
@@ -72,17 +87,12 @@ static void DumpInteger (lua_Integer x, DumpState *D) { | |||
72 | 87 | ||
73 | static void DumpString (const TString *s, DumpState *D) { | 88 | static void DumpString (const TString *s, DumpState *D) { |
74 | if (s == NULL) | 89 | if (s == NULL) |
75 | DumpByte(0, D); | 90 | DumpSize(0, D); |
76 | else { | 91 | else { |
77 | size_t size = tsslen(s) + 1; /* include trailing '\0' */ | 92 | size_t size = tsslen(s); |
78 | const char *str = getstr(s); | 93 | const char *str = getstr(s); |
79 | if (size < 0xFF) | 94 | DumpSize(size + 1, D); |
80 | DumpByte(cast_int(size), D); | 95 | DumpVector(str, size, D); |
81 | else { | ||
82 | DumpByte(0xFF, D); | ||
83 | DumpVar(size, D); | ||
84 | } | ||
85 | DumpVector(str, size - 1, D); /* no need to save '\0' */ | ||
86 | } | 96 | } |
87 | } | 97 | } |
88 | 98 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp roberto $ | 2 | ** $Id: lundump.c,v 2.45 2017/06/27 11:35:31 roberto Exp roberto $ |
3 | ** load precompiled Lua chunks | 3 | ** load precompiled Lua chunks |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -58,16 +58,26 @@ static void LoadBlock (LoadState *S, void *b, size_t size) { | |||
58 | 58 | ||
59 | 59 | ||
60 | static lu_byte LoadByte (LoadState *S) { | 60 | static lu_byte LoadByte (LoadState *S) { |
61 | lu_byte x; | 61 | int b = zgetc(S->Z); |
62 | LoadVar(S, x); | 62 | if (b == EOZ) |
63 | error(S, "truncated"); | ||
64 | return cast_byte(b); | ||
65 | } | ||
66 | |||
67 | |||
68 | static size_t LoadSize (LoadState *S) { | ||
69 | size_t x = 0; | ||
70 | int b; | ||
71 | do { | ||
72 | b = LoadByte(S); | ||
73 | x = (x << 7) | (b & 0x7f); | ||
74 | } while ((b & 0x80) == 0); | ||
63 | return x; | 75 | return x; |
64 | } | 76 | } |
65 | 77 | ||
66 | 78 | ||
67 | static int LoadInt (LoadState *S) { | 79 | static int LoadInt (LoadState *S) { |
68 | int x; | 80 | return cast_int(LoadSize(S)); |
69 | LoadVar(S, x); | ||
70 | return x; | ||
71 | } | 81 | } |
72 | 82 | ||
73 | 83 | ||
@@ -86,9 +96,7 @@ static lua_Integer LoadInteger (LoadState *S) { | |||
86 | 96 | ||
87 | 97 | ||
88 | static TString *LoadString (LoadState *S) { | 98 | static TString *LoadString (LoadState *S) { |
89 | size_t size = LoadByte(S); | 99 | size_t size = LoadSize(S); |
90 | if (size == 0xFF) | ||
91 | LoadVar(S, size); | ||
92 | if (size == 0) | 100 | if (size == 0) |
93 | return NULL; | 101 | return NULL; |
94 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ | 102 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ |