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