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