/src/testdir/build/lua-master/source/lobject.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lobject.c $ |
3 | | ** Some generic functions over Lua objects |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lobject_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <float.h> |
14 | | #include <locale.h> |
15 | | #include <math.h> |
16 | | #include <stdarg.h> |
17 | | #include <stdio.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include "lua.h" |
22 | | |
23 | | #include "lctype.h" |
24 | | #include "ldebug.h" |
25 | | #include "ldo.h" |
26 | | #include "lmem.h" |
27 | | #include "lobject.h" |
28 | | #include "lstate.h" |
29 | | #include "lstring.h" |
30 | | #include "lvm.h" |
31 | | |
32 | | |
33 | | /* |
34 | | ** Computes ceil(log2(x)), which is the smallest integer n such that |
35 | | ** x <= (1 << n). |
36 | | */ |
37 | 15.0k | lu_byte luaO_ceillog2 (unsigned int x) { |
38 | 15.0k | static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ |
39 | 15.0k | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
40 | 15.0k | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
41 | 15.0k | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
42 | 15.0k | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
43 | 15.0k | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
44 | 15.0k | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
45 | 15.0k | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
46 | 15.0k | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 |
47 | 15.0k | }; |
48 | 15.0k | int l = 0; |
49 | 15.0k | x--; |
50 | 20.1k | while (x >= 256) { l += 8; x >>= 8; } |
51 | 15.0k | return cast_byte(l + log_2[x]); |
52 | 15.0k | } |
53 | | |
54 | | /* |
55 | | ** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx). |
56 | | ** The exponent is represented using excess-7. Mimicking IEEE 754, the |
57 | | ** representation normalizes the number when possible, assuming an extra |
58 | | ** 1 before the mantissa (xxxx) and adding one to the exponent (eeee) |
59 | | ** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if |
60 | | ** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers). |
61 | | */ |
62 | 2.64k | lu_byte luaO_codeparam (unsigned int p) { |
63 | 2.64k | if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */ |
64 | 0 | return 0xFF; /* return maximum value */ |
65 | 2.64k | else { |
66 | 2.64k | p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */ |
67 | 2.64k | if (p < 0x10) { /* subnormal number? */ |
68 | | /* exponent bits are already zero; nothing else to do */ |
69 | 0 | return cast_byte(p); |
70 | 0 | } |
71 | 2.64k | else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */ |
72 | | /* preserve 5 bits in 'p' */ |
73 | 2.64k | unsigned log = luaO_ceillog2(p + 1) - 5u; |
74 | 2.64k | return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4)); |
75 | 2.64k | } |
76 | 2.64k | } |
77 | 2.64k | } |
78 | | |
79 | | |
80 | | /* |
81 | | ** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly, |
82 | | ** we have to multiply 'x' by the mantissa and then shift accordingly to |
83 | | ** the exponent. If the exponent is positive, both the multiplication |
84 | | ** and the shift increase 'x', so we have to care only about overflows. |
85 | | ** For negative exponents, however, multiplying before the shift keeps |
86 | | ** more significant bits, as long as the multiplication does not |
87 | | ** overflow, so we check which order is best. |
88 | | */ |
89 | 6.38k | l_mem luaO_applyparam (lu_byte p, l_mem x) { |
90 | 6.38k | unsigned int m = p & 0xF; /* mantissa */ |
91 | 6.38k | int e = (p >> 4); /* exponent */ |
92 | 6.38k | if (e > 0) { /* normalized? */ |
93 | 6.38k | e--; /* correct exponent */ |
94 | 6.38k | m += 0x10; /* correct mantissa; maximum value is 0x1F */ |
95 | 6.38k | } |
96 | 6.38k | e -= 7; /* correct excess-7 */ |
97 | 6.38k | if (e >= 0) { |
98 | 2.62k | if (x < (MAX_LMEM / 0x1F) >> e) /* no overflow? */ |
99 | 2.62k | return (x * m) << e; /* order doesn't matter here */ |
100 | 0 | else /* real overflow */ |
101 | 0 | return MAX_LMEM; |
102 | 2.62k | } |
103 | 3.76k | else { /* negative exponent */ |
104 | 3.76k | e = -e; |
105 | 3.76k | if (x < MAX_LMEM / 0x1F) /* multiplication cannot overflow? */ |
106 | 3.76k | return (x * m) >> e; /* multiplying first gives more precision */ |
107 | 0 | else if ((x >> e) < MAX_LMEM / 0x1F) /* cannot overflow after shift? */ |
108 | 0 | return (x >> e) * m; |
109 | 0 | else /* real overflow */ |
110 | 0 | return MAX_LMEM; |
111 | 3.76k | } |
112 | 6.38k | } |
113 | | |
114 | | |
115 | | static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, |
116 | 46.2k | lua_Integer v2) { |
117 | 46.2k | switch (op) { |
118 | 33 | case LUA_OPADD: return intop(+, v1, v2); |
119 | 2.74k | case LUA_OPSUB:return intop(-, v1, v2); |
120 | 1.18k | case LUA_OPMUL:return intop(*, v1, v2); |
121 | 7.73k | case LUA_OPMOD: return luaV_mod(L, v1, v2); |
122 | 3 | case LUA_OPIDIV: return luaV_idiv(L, v1, v2); |
123 | 38 | case LUA_OPBAND: return intop(&, v1, v2); |
124 | 0 | case LUA_OPBOR: return intop(|, v1, v2); |
125 | 2.10k | case LUA_OPBXOR: return intop(^, v1, v2); |
126 | 747 | case LUA_OPSHL: return luaV_shiftl(v1, v2); |
127 | 260 | case LUA_OPSHR: return luaV_shiftr(v1, v2); |
128 | 6.23k | case LUA_OPUNM: return intop(-, 0, v1); |
129 | 25.1k | case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); |
130 | 0 | default: lua_assert(0); return 0; |
131 | 46.2k | } |
132 | 46.2k | } |
133 | | |
134 | | |
135 | | static lua_Number numarith (lua_State *L, int op, lua_Number v1, |
136 | 119k | lua_Number v2) { |
137 | 119k | switch (op) { |
138 | 8 | case LUA_OPADD: return luai_numadd(L, v1, v2); |
139 | 882 | case LUA_OPSUB: return luai_numsub(L, v1, v2); |
140 | 1.97k | case LUA_OPMUL: return luai_nummul(L, v1, v2); |
141 | 3.68k | case LUA_OPDIV: return luai_numdiv(L, v1, v2); |
142 | 102k | case LUA_OPPOW: return luai_numpow(L, v1, v2); |
143 | 614 | case LUA_OPIDIV: return luai_numidiv(L, v1, v2); |
144 | 1.58k | case LUA_OPUNM: return luai_numunm(L, v1); |
145 | 7.79k | case LUA_OPMOD: return luaV_modf(L, v1, v2); |
146 | 0 | default: lua_assert(0); return 0; |
147 | 119k | } |
148 | 119k | } |
149 | | |
150 | | |
151 | | int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, |
152 | 165k | TValue *res) { |
153 | 165k | switch (op) { |
154 | 2.14k | case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: |
155 | 3.15k | case LUA_OPSHL: case LUA_OPSHR: |
156 | 28.2k | case LUA_OPBNOT: { /* operate only on integers */ |
157 | 28.2k | lua_Integer i1; lua_Integer i2; |
158 | 28.2k | if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) { |
159 | 28.2k | setivalue(res, intarith(L, op, i1, i2)); |
160 | 28.2k | return 1; |
161 | 28.2k | } |
162 | 0 | else return 0; /* fail */ |
163 | 28.2k | } |
164 | 106k | case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ |
165 | 106k | lua_Number n1; lua_Number n2; |
166 | 106k | if (tonumberns(p1, n1) && tonumberns(p2, n2)) { |
167 | 106k | setfltvalue(res, numarith(L, op, n1, n2)); |
168 | 106k | return 1; |
169 | 106k | } |
170 | 0 | else return 0; /* fail */ |
171 | 106k | } |
172 | 30.8k | default: { /* other operations */ |
173 | 30.8k | lua_Number n1; lua_Number n2; |
174 | 30.8k | if (ttisinteger(p1) && ttisinteger(p2)) { |
175 | 17.9k | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); |
176 | 17.9k | return 1; |
177 | 17.9k | } |
178 | 12.8k | else if (tonumberns(p1, n1) && tonumberns(p2, n2)) { |
179 | 12.8k | setfltvalue(res, numarith(L, op, n1, n2)); |
180 | 12.8k | return 1; |
181 | 12.8k | } |
182 | 0 | else return 0; /* fail */ |
183 | 30.8k | } |
184 | 165k | } |
185 | 165k | } |
186 | | |
187 | | |
188 | | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, |
189 | 0 | StkId res) { |
190 | 0 | if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { |
191 | | /* could not perform raw operation; try metamethod */ |
192 | 0 | luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | | |
197 | 5.79k | lu_byte luaO_hexavalue (int c) { |
198 | 5.79k | lua_assert(lisxdigit(c)); |
199 | 5.79k | if (lisdigit(c)) return cast_byte(c - '0'); |
200 | 5.79k | else return cast_byte((ltolower(c) - 'a') + 10); |
201 | 5.79k | } |
202 | | |
203 | | |
204 | 997k | static int isneg (const char **s) { |
205 | 997k | if (**s == '-') { (*s)++; return 1; } |
206 | 997k | else if (**s == '+') (*s)++; |
207 | 997k | return 0; |
208 | 997k | } |
209 | | |
210 | | |
211 | | |
212 | | /* |
213 | | ** {================================================================== |
214 | | ** Lua's implementation for 'lua_strx2number' |
215 | | ** =================================================================== |
216 | | */ |
217 | | |
218 | | #if !defined(lua_strx2number) |
219 | | |
220 | | /* maximum number of significant digits to read (to avoid overflows |
221 | | even with single floats) */ |
222 | | #define MAXSIGDIG 30 |
223 | | |
224 | | /* |
225 | | ** convert a hexadecimal numeric string to a number, following |
226 | | ** C99 specification for 'strtod' |
227 | | */ |
228 | | static lua_Number lua_strx2number (const char *s, char **endptr) { |
229 | | int dot = lua_getlocaledecpoint(); |
230 | | lua_Number r = l_mathop(0.0); /* result (accumulator) */ |
231 | | int sigdig = 0; /* number of significant digits */ |
232 | | int nosigdig = 0; /* number of non-significant digits */ |
233 | | int e = 0; /* exponent correction */ |
234 | | int neg; /* 1 if number is negative */ |
235 | | int hasdot = 0; /* true after seen a dot */ |
236 | | *endptr = cast_charp(s); /* nothing is valid yet */ |
237 | | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ |
238 | | neg = isneg(&s); /* check sign */ |
239 | | if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ |
240 | | return l_mathop(0.0); /* invalid format (no '0x') */ |
241 | | for (s += 2; ; s++) { /* skip '0x' and read numeral */ |
242 | | if (*s == dot) { |
243 | | if (hasdot) break; /* second dot? stop loop */ |
244 | | else hasdot = 1; |
245 | | } |
246 | | else if (lisxdigit(cast_uchar(*s))) { |
247 | | if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ |
248 | | nosigdig++; |
249 | | else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ |
250 | | r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); |
251 | | else e++; /* too many digits; ignore, but still count for exponent */ |
252 | | if (hasdot) e--; /* decimal digit? correct exponent */ |
253 | | } |
254 | | else break; /* neither a dot nor a digit */ |
255 | | } |
256 | | if (nosigdig + sigdig == 0) /* no digits? */ |
257 | | return l_mathop(0.0); /* invalid format */ |
258 | | *endptr = cast_charp(s); /* valid up to here */ |
259 | | e *= 4; /* each digit multiplies/divides value by 2^4 */ |
260 | | if (*s == 'p' || *s == 'P') { /* exponent part? */ |
261 | | int exp1 = 0; /* exponent value */ |
262 | | int neg1; /* exponent sign */ |
263 | | s++; /* skip 'p' */ |
264 | | neg1 = isneg(&s); /* sign */ |
265 | | if (!lisdigit(cast_uchar(*s))) |
266 | | return l_mathop(0.0); /* invalid; must have at least one digit */ |
267 | | while (lisdigit(cast_uchar(*s))) /* read exponent */ |
268 | | exp1 = exp1 * 10 + *(s++) - '0'; |
269 | | if (neg1) exp1 = -exp1; |
270 | | e += exp1; |
271 | | *endptr = cast_charp(s); /* valid up to here */ |
272 | | } |
273 | | if (neg) r = -r; |
274 | | return l_mathop(ldexp)(r, e); |
275 | | } |
276 | | |
277 | | #endif |
278 | | /* }====================================================== */ |
279 | | |
280 | | |
281 | | /* maximum length of a numeral to be converted to a number */ |
282 | | #if !defined (L_MAXLENNUM) |
283 | 5 | #define L_MAXLENNUM 200 |
284 | | #endif |
285 | | |
286 | | /* |
287 | | ** Convert string 's' to a Lua number (put in 'result'). Return NULL on |
288 | | ** fail or the address of the ending '\0' on success. ('mode' == 'x') |
289 | | ** means a hexadecimal numeral. |
290 | | */ |
291 | 13.2k | static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { |
292 | 13.2k | char *endptr; |
293 | 13.2k | *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ |
294 | 13.2k | : lua_str2number(s, &endptr); |
295 | 13.2k | if (endptr == s) return NULL; /* nothing recognized? */ |
296 | 13.2k | while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ |
297 | 13.2k | return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ |
298 | 13.2k | } |
299 | | |
300 | | |
301 | | /* |
302 | | ** Convert string 's' to a Lua number (put in 'result') handling the |
303 | | ** current locale. |
304 | | ** This function accepts both the current locale or a dot as the radix |
305 | | ** mark. If the conversion fails, it may mean number has a dot but |
306 | | ** locale accepts something else. In that case, the code copies 's' |
307 | | ** to a buffer (because 's' is read-only), changes the dot to the |
308 | | ** current locale radix mark, and tries to convert again. |
309 | | ** The variable 'mode' checks for special characters in the string: |
310 | | ** - 'n' means 'inf' or 'nan' (which should be rejected) |
311 | | ** - 'x' means a hexadecimal numeral |
312 | | ** - '.' just optimizes the search for the common case (no special chars) |
313 | | */ |
314 | 13.2k | static const char *l_str2d (const char *s, lua_Number *result) { |
315 | 13.2k | const char *endptr; |
316 | 13.2k | const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ |
317 | 13.2k | int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; |
318 | 13.2k | if (mode == 'n') /* reject 'inf' and 'nan' */ |
319 | 1 | return NULL; |
320 | 13.2k | endptr = l_str2dloc(s, result, mode); /* try to convert */ |
321 | 13.2k | if (endptr == NULL) { /* failed? may be a different locale */ |
322 | 16 | char buff[L_MAXLENNUM + 1]; |
323 | 16 | const char *pdot = strchr(s, '.'); |
324 | 16 | if (pdot == NULL || strlen(s) > L_MAXLENNUM) |
325 | 14 | return NULL; /* string too long or no dot; fail */ |
326 | 2 | strcpy(buff, s); /* copy string to buffer */ |
327 | 2 | buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ |
328 | 2 | endptr = l_str2dloc(buff, result, mode); /* try again */ |
329 | 2 | if (endptr != NULL) |
330 | 0 | endptr = s + (endptr - buff); /* make relative to 's' */ |
331 | 2 | } |
332 | 13.2k | return endptr; |
333 | 13.2k | } |
334 | | |
335 | | |
336 | 4.35M | #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) |
337 | 554 | #define MAXLASTD cast_int(LUA_MAXINTEGER % 10) |
338 | | |
339 | 997k | static const char *l_str2int (const char *s, lua_Integer *result) { |
340 | 997k | lua_Unsigned a = 0; |
341 | 997k | int empty = 1; |
342 | 997k | int neg; |
343 | 997k | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ |
344 | 997k | neg = isneg(&s); |
345 | 997k | if (s[0] == '0' && |
346 | 997k | (s[1] == 'x' || s[1] == 'X')) { /* hex? */ |
347 | 19 | s += 2; /* skip '0x' */ |
348 | 19 | for (; lisxdigit(cast_uchar(*s)); s++) { |
349 | 19 | a = a * 16 + luaO_hexavalue(*s); |
350 | 19 | empty = 0; |
351 | 19 | } |
352 | 19 | } |
353 | 997k | else { /* decimal */ |
354 | 4.34M | for (; lisdigit(cast_uchar(*s)); s++) { |
355 | 4.34M | int d = *s - '0'; |
356 | 4.34M | if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ |
357 | 8.03k | return NULL; /* do not accept it (as integer) */ |
358 | 4.33M | a = a * 10 + cast_uint(d); |
359 | 4.33M | empty = 0; |
360 | 4.33M | } |
361 | 997k | } |
362 | 989k | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ |
363 | 989k | if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ |
364 | 984k | else { |
365 | 984k | *result = l_castU2S((neg) ? 0u - a : a); |
366 | 984k | return s; |
367 | 984k | } |
368 | 989k | } |
369 | | |
370 | | |
371 | 997k | size_t luaO_str2num (const char *s, TValue *o) { |
372 | 997k | lua_Integer i; lua_Number n; |
373 | 997k | const char *e; |
374 | 997k | if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ |
375 | 984k | setivalue(o, i); |
376 | 984k | } |
377 | 13.2k | else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ |
378 | 13.2k | setfltvalue(o, n); |
379 | 13.2k | } |
380 | 17 | else |
381 | 17 | return 0; /* conversion failed */ |
382 | 997k | return ct_diff2sz(e - s) + 1; /* success; return string size */ |
383 | 997k | } |
384 | | |
385 | | |
386 | 2.46k | int luaO_utf8esc (char *buff, l_uint32 x) { |
387 | 2.46k | int n = 1; /* number of bytes put in buffer (backwards) */ |
388 | 2.46k | lua_assert(x <= 0x7FFFFFFFu); |
389 | 2.46k | if (x < 0x80) /* ASCII? */ |
390 | 1.92k | buff[UTF8BUFFSZ - 1] = cast_char(x); |
391 | 546 | else { /* need continuation bytes */ |
392 | 546 | unsigned int mfb = 0x3f; /* maximum that fits in first byte */ |
393 | 1.02k | do { /* add continuation bytes */ |
394 | 1.02k | buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f)); |
395 | 1.02k | x >>= 6; /* remove added bits */ |
396 | 1.02k | mfb >>= 1; /* now there is one less bit available in first byte */ |
397 | 1.02k | } while (x > mfb); /* still needs continuation byte? */ |
398 | 546 | buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */ |
399 | 546 | } |
400 | 2.46k | return n; |
401 | 2.46k | } |
402 | | |
403 | | |
404 | | /* |
405 | | ** The size of the buffer for the conversion of a number to a string |
406 | | ** 'LUA_N2SBUFFSZ' must be enough to accommodate both LUA_INTEGER_FMT |
407 | | ** and LUA_NUMBER_FMT. For a long long int, this is 19 digits plus a |
408 | | ** sign and a final '\0', adding to 21. For a long double, it can go to |
409 | | ** a sign, the dot, an exponent letter, an exponent sign, 4 exponent |
410 | | ** digits, the final '\0', plus the significant digits, which are |
411 | | ** approximately the *_DIG attribute. |
412 | | */ |
413 | | #if LUA_N2SBUFFSZ < (20 + l_floatatt(DIG)) |
414 | | #error "invalid value for LUA_N2SBUFFSZ" |
415 | | #endif |
416 | | |
417 | | |
418 | | /* |
419 | | ** Convert a float to a string, adding it to a buffer. First try with |
420 | | ** a not too large number of digits, to avoid noise (for instance, |
421 | | ** 1.1 going to "1.1000000000000001"). If that lose precision, so |
422 | | ** that reading the result back gives a different number, then do the |
423 | | ** conversion again with extra precision. Moreover, if the numeral looks |
424 | | ** like an integer (without a decimal point or an exponent), add ".0" to |
425 | | ** its end. |
426 | | */ |
427 | 0 | static int tostringbuffFloat (lua_Number n, char *buff) { |
428 | | /* first conversion */ |
429 | 0 | int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT, |
430 | 0 | (LUAI_UACNUMBER)n); |
431 | 0 | lua_Number check = lua_str2number(buff, NULL); /* read it back */ |
432 | 0 | if (check != n) { /* not enough precision? */ |
433 | | /* convert again with more precision */ |
434 | 0 | len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N, |
435 | 0 | (LUAI_UACNUMBER)n); |
436 | 0 | } |
437 | | /* looks like an integer? */ |
438 | 0 | if (buff[strspn(buff, "-0123456789")] == '\0') { |
439 | 0 | buff[len++] = lua_getlocaledecpoint(); |
440 | 0 | buff[len++] = '0'; /* adds '.0' to result */ |
441 | 0 | } |
442 | 0 | return len; |
443 | 0 | } |
444 | | |
445 | | |
446 | | /* |
447 | | ** Convert a number object to a string, adding it to a buffer. |
448 | | */ |
449 | 668 | unsigned luaO_tostringbuff (const TValue *obj, char *buff) { |
450 | 668 | int len; |
451 | 668 | lua_assert(ttisnumber(obj)); |
452 | 668 | if (ttisinteger(obj)) |
453 | 668 | len = lua_integer2str(buff, LUA_N2SBUFFSZ, ivalue(obj)); |
454 | 0 | else |
455 | 0 | len = tostringbuffFloat(fltvalue(obj), buff); |
456 | 668 | lua_assert(len < LUA_N2SBUFFSZ); |
457 | 668 | return cast_uint(len); |
458 | 668 | } |
459 | | |
460 | | |
461 | | /* |
462 | | ** Convert a number object to a Lua string, replacing the value at 'obj' |
463 | | */ |
464 | 17 | void luaO_tostring (lua_State *L, TValue *obj) { |
465 | 17 | char buff[LUA_N2SBUFFSZ]; |
466 | 17 | unsigned len = luaO_tostringbuff(obj, buff); |
467 | 17 | setsvalue(L, obj, luaS_newlstr(L, buff, len)); |
468 | 17 | } |
469 | | |
470 | | |
471 | | |
472 | | |
473 | | /* |
474 | | ** {================================================================== |
475 | | ** 'luaO_pushvfstring' |
476 | | ** =================================================================== |
477 | | */ |
478 | | |
479 | | /* |
480 | | ** Size for buffer space used by 'luaO_pushvfstring'. It should be |
481 | | ** (LUA_IDSIZE + LUA_N2SBUFFSZ) + a minimal space for basic messages, |
482 | | ** so that 'luaG_addinfo' can work directly on the static buffer. |
483 | | */ |
484 | | #define BUFVFS cast_uint(LUA_IDSIZE + LUA_N2SBUFFSZ + 95) |
485 | | |
486 | | /* |
487 | | ** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while |
488 | | ** building result (memory error [1] or buffer overflow [2]). |
489 | | */ |
490 | | typedef struct BuffFS { |
491 | | lua_State *L; |
492 | | char *b; |
493 | | size_t buffsize; |
494 | | size_t blen; /* length of string in 'buff' */ |
495 | | int err; |
496 | | char space[BUFVFS]; /* initial buffer */ |
497 | | } BuffFS; |
498 | | |
499 | | |
500 | 1.52k | static void initbuff (lua_State *L, BuffFS *buff) { |
501 | 1.52k | buff->L = L; |
502 | 1.52k | buff->b = buff->space; |
503 | 1.52k | buff->buffsize = sizeof(buff->space); |
504 | 1.52k | buff->blen = 0; |
505 | 1.52k | buff->err = 0; |
506 | 1.52k | } |
507 | | |
508 | | |
509 | | /* |
510 | | ** Push final result from 'luaO_pushvfstring'. This function may raise |
511 | | ** errors explicitly or through memory errors, so it must run protected. |
512 | | */ |
513 | 1.52k | static void pushbuff (lua_State *L, void *ud) { |
514 | 1.52k | BuffFS *buff = cast(BuffFS*, ud); |
515 | 1.52k | switch (buff->err) { |
516 | 0 | case 1: /* memory error */ |
517 | 0 | luaD_throw(L, LUA_ERRMEM); |
518 | 0 | break; |
519 | 0 | case 2: /* length overflow: Add "..." at the end of result */ |
520 | 0 | if (buff->buffsize - buff->blen < 3) |
521 | 0 | strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */ |
522 | 0 | else { /* there is enough space left for the "..." */ |
523 | 0 | strcpy(buff->b + buff->blen, "..."); |
524 | 0 | buff->blen += 3; |
525 | 0 | } |
526 | | /* FALLTHROUGH */ |
527 | 1.52k | default: { /* no errors, but it can raise one creating the new string */ |
528 | 1.52k | TString *ts = luaS_newlstr(L, buff->b, buff->blen); |
529 | 1.52k | setsvalue2s(L, L->top.p, ts); |
530 | 1.52k | L->top.p++; |
531 | 1.52k | } |
532 | 1.52k | } |
533 | 1.52k | } |
534 | | |
535 | | |
536 | 1.52k | static const char *clearbuff (BuffFS *buff) { |
537 | 1.52k | lua_State *L = buff->L; |
538 | 1.52k | const char *res; |
539 | 1.52k | if (luaD_rawrunprotected(L, pushbuff, buff) != LUA_OK) /* errors? */ |
540 | 0 | res = NULL; /* error message is on the top of the stack */ |
541 | 1.52k | else |
542 | 1.52k | res = getstr(tsvalue(s2v(L->top.p - 1))); |
543 | 1.52k | if (buff->b != buff->space) /* using dynamic buffer? */ |
544 | 204 | luaM_freearray(L, buff->b, buff->buffsize); /* free it */ |
545 | 1.52k | return res; |
546 | 1.52k | } |
547 | | |
548 | | |
549 | 7.94k | static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { |
550 | 7.94k | size_t left = buff->buffsize - buff->blen; /* space left in the buffer */ |
551 | 7.94k | if (buff->err) /* do nothing else after an error */ |
552 | 0 | return; |
553 | 7.94k | if (slen > left) { /* new string doesn't fit into current buffer? */ |
554 | 204 | if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */ |
555 | 0 | memcpy(buff->b + buff->blen, str, left); /* copy what it can */ |
556 | 0 | buff->blen = buff->buffsize; |
557 | 0 | buff->err = 2; /* doesn't add anything else */ |
558 | 0 | return; |
559 | 0 | } |
560 | 204 | else { |
561 | 204 | size_t newsize = buff->buffsize + slen; /* limited to MAX_SIZE/2 */ |
562 | 204 | char *newb = |
563 | 204 | (buff->b == buff->space) /* still using static space? */ |
564 | 204 | ? luaM_reallocvector(buff->L, NULL, 0, newsize, char) |
565 | 204 | : luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize, |
566 | 204 | char); |
567 | 204 | if (newb == NULL) { /* allocation error? */ |
568 | 0 | buff->err = 1; /* signal a memory error */ |
569 | 0 | return; |
570 | 0 | } |
571 | 204 | if (buff->b == buff->space) /* new buffer (not reallocated)? */ |
572 | 204 | memcpy(newb, buff->b, buff->blen); /* copy previous content */ |
573 | 204 | buff->b = newb; /* set new (larger) buffer... */ |
574 | 204 | buff->buffsize = newsize; /* ...and its new size */ |
575 | 204 | } |
576 | 204 | } |
577 | 7.94k | memcpy(buff->b + buff->blen, str, slen); /* copy new content */ |
578 | 7.94k | buff->blen += slen; |
579 | 7.94k | } |
580 | | |
581 | | |
582 | | /* |
583 | | ** Add a numeral to the buffer. |
584 | | */ |
585 | 651 | static void addnum2buff (BuffFS *buff, TValue *num) { |
586 | 651 | char numbuff[LUA_N2SBUFFSZ]; |
587 | 651 | unsigned len = luaO_tostringbuff(num, numbuff); |
588 | 651 | addstr2buff(buff, numbuff, len); |
589 | 651 | } |
590 | | |
591 | | |
592 | | /* |
593 | | ** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' |
594 | | conventional formats, plus Lua-specific '%I' and '%U' |
595 | | */ |
596 | 1.52k | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { |
597 | 1.52k | BuffFS buff; /* holds last part of the result */ |
598 | 1.52k | const char *e; /* points to next '%' */ |
599 | 1.52k | initbuff(L, &buff); |
600 | 4.73k | while ((e = strchr(fmt, '%')) != NULL) { |
601 | 3.21k | addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */ |
602 | 3.21k | switch (*(e + 1)) { /* conversion specifier */ |
603 | 2.37k | case 's': { /* zero-terminated string */ |
604 | 2.37k | const char *s = va_arg(argp, char *); |
605 | 2.37k | if (s == NULL) s = "(null)"; |
606 | 2.37k | addstr2buff(&buff, s, strlen(s)); |
607 | 2.37k | break; |
608 | 0 | } |
609 | 187 | case 'c': { /* an 'int' as a character */ |
610 | 187 | char c = cast_char(va_arg(argp, int)); |
611 | 187 | addstr2buff(&buff, &c, sizeof(char)); |
612 | 187 | break; |
613 | 0 | } |
614 | 651 | case 'd': { /* an 'int' */ |
615 | 651 | TValue num; |
616 | 651 | setivalue(&num, va_arg(argp, int)); |
617 | 651 | addnum2buff(&buff, &num); |
618 | 651 | break; |
619 | 0 | } |
620 | 0 | case 'I': { /* a 'lua_Integer' */ |
621 | 0 | TValue num; |
622 | 0 | setivalue(&num, cast_Integer(va_arg(argp, l_uacInt))); |
623 | 0 | addnum2buff(&buff, &num); |
624 | 0 | break; |
625 | 0 | } |
626 | 0 | case 'f': { /* a 'lua_Number' */ |
627 | 0 | TValue num; |
628 | 0 | setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); |
629 | 0 | addnum2buff(&buff, &num); |
630 | 0 | break; |
631 | 0 | } |
632 | 0 | case 'p': { /* a pointer */ |
633 | 0 | char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */ |
634 | 0 | void *p = va_arg(argp, void *); |
635 | 0 | int len = lua_pointer2str(bf, LUA_N2SBUFFSZ, p); |
636 | 0 | addstr2buff(&buff, bf, cast_uint(len)); |
637 | 0 | break; |
638 | 0 | } |
639 | 0 | case 'U': { /* an 'unsigned long' as a UTF-8 sequence */ |
640 | 0 | char bf[UTF8BUFFSZ]; |
641 | 0 | unsigned long arg = va_arg(argp, unsigned long); |
642 | 0 | int len = luaO_utf8esc(bf, cast(l_uint32, arg)); |
643 | 0 | addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len)); |
644 | 0 | break; |
645 | 0 | } |
646 | 0 | case '%': { |
647 | 0 | addstr2buff(&buff, "%", 1); |
648 | 0 | break; |
649 | 0 | } |
650 | 0 | default: { |
651 | 0 | addstr2buff(&buff, e, 2); /* keep unknown format in the result */ |
652 | 0 | break; |
653 | 0 | } |
654 | 3.21k | } |
655 | 3.21k | fmt = e + 2; /* skip '%' and the specifier */ |
656 | 3.21k | } |
657 | 1.52k | addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ |
658 | 1.52k | return clearbuff(&buff); /* empty buffer into a new string */ |
659 | 1.52k | } |
660 | | |
661 | | |
662 | 1.28k | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { |
663 | 1.28k | const char *msg; |
664 | 1.28k | va_list argp; |
665 | 1.28k | va_start(argp, fmt); |
666 | 1.28k | msg = luaO_pushvfstring(L, fmt, argp); |
667 | 1.28k | va_end(argp); |
668 | 1.28k | if (msg == NULL) /* error? */ |
669 | 0 | luaD_throw(L, LUA_ERRMEM); |
670 | 1.28k | return msg; |
671 | 1.28k | } |
672 | | |
673 | | /* }================================================================== */ |
674 | | |
675 | | |
676 | | #define RETS "..." |
677 | | #define PRE "[string \"" |
678 | 556 | #define POS "\"]" |
679 | | |
680 | 1.11k | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) |
681 | | |
682 | 556 | void luaO_chunkid (char *out, const char *source, size_t srclen) { |
683 | 556 | size_t bufflen = LUA_IDSIZE; /* free space in buffer */ |
684 | 556 | if (*source == '=') { /* 'literal' source */ |
685 | 0 | if (srclen <= bufflen) /* small enough? */ |
686 | 0 | memcpy(out, source + 1, srclen * sizeof(char)); |
687 | 0 | else { /* truncate it */ |
688 | 0 | addstr(out, source + 1, bufflen - 1); |
689 | 0 | *out = '\0'; |
690 | 0 | } |
691 | 0 | } |
692 | 556 | else if (*source == '@') { /* file name */ |
693 | 0 | if (srclen <= bufflen) /* small enough? */ |
694 | 0 | memcpy(out, source + 1, srclen * sizeof(char)); |
695 | 0 | else { /* add '...' before rest of name */ |
696 | 0 | addstr(out, RETS, LL(RETS)); |
697 | 0 | bufflen -= LL(RETS); |
698 | 0 | memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); |
699 | 0 | } |
700 | 0 | } |
701 | 556 | else { /* string; format as [string "source"] */ |
702 | 556 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ |
703 | 556 | addstr(out, PRE, LL(PRE)); /* add prefix */ |
704 | 556 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ |
705 | 556 | if (srclen < bufflen && nl == NULL) { /* small one-line source? */ |
706 | 556 | addstr(out, source, srclen); /* keep it */ |
707 | 556 | } |
708 | 0 | else { |
709 | 0 | if (nl != NULL) |
710 | 0 | srclen = ct_diff2sz(nl - source); /* stop at first newline */ |
711 | 0 | if (srclen > bufflen) srclen = bufflen; |
712 | 0 | addstr(out, source, srclen); |
713 | 0 | addstr(out, RETS, LL(RETS)); |
714 | 0 | } |
715 | 556 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); |
716 | 556 | } |
717 | 556 | } |
718 | | |