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