Coverage Report

Created: 2024-04-23 06:32

/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
6.31M
int luaO_ceillog2 (unsigned int x) {
36
6.31M
  static const lu_byte log_2[256] = {  /* log_2[i - 1] = ceil(log2(i)) */
37
6.31M
    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
6.31M
    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
6.31M
    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
6.31M
    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
6.31M
    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
6.31M
    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
6.31M
    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
6.31M
    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
6.31M
  };
46
6.31M
  int l = 0;
47
6.31M
  x--;
48
6.71M
  while (x >= 256) { l += 8; x >>= 8; }
49
6.31M
  return l + log_2[x];
50
6.31M
}
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
137k
unsigned int luaO_codeparam (unsigned int p) {
61
137k
  if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u)  /* overflow? */
62
0
    return 0xFF;  /* return maximum value */
63
137k
  else {
64
137k
    p = (cast(l_uint32, p) * 128 + 99) / 100;  /* round up the division */
65
137k
    if (p < 0x10)  /* subnormal number? */
66
0
      return p;  /* exponent bits are already zero; nothing else to do */
67
137k
    else {
68
137k
      int log = luaO_ceillog2(p + 1) - 5;  /* preserve 5 bits */
69
137k
      return ((p >> log) - 0x10) | ((log + 1) << 4);
70
137k
    }
71
137k
  }
72
137k
}
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
2.56M
l_obj luaO_applyparam (unsigned int p, l_obj x) {
85
2.56M
  unsigned int m = p & 0xF;  /* mantissa */
86
2.56M
  int e = (p >> 4);  /* exponent */
87
2.56M
  if (e > 0) {  /* normalized? */
88
2.56M
    e--;  /* correct exponent */
89
2.56M
    m += 0x10;  /* correct mantissa; maximum value is 0x1F */
90
2.56M
  }
91
2.56M
  e -= 7;  /* correct excess-7 */
92
2.56M
  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
2.56M
  else {  /* negative exponent */
99
2.56M
    e = -e;
100
2.56M
    if (x < MAX_LOBJ / 0x1F)  /* multiplication cannot overflow? */
101
2.56M
      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
2.56M
  }
107
2.56M
}
108
109
110
static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
111
1.19M
                                                   lua_Integer v2) {
112
1.19M
  switch (op) {
113
19.1k
    case LUA_OPADD: return intop(+, v1, v2);
114
128k
    case LUA_OPSUB:return intop(-, v1, v2);
115
69.4k
    case LUA_OPMUL:return intop(*, v1, v2);
116
49.8k
    case LUA_OPMOD: return luaV_mod(L, v1, v2);
117
75.2k
    case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
118
4.20k
    case LUA_OPBAND: return intop(&, v1, v2);
119
6.63k
    case LUA_OPBOR: return intop(|, v1, v2);
120
127k
    case LUA_OPBXOR: return intop(^, v1, v2);
121
5.45k
    case LUA_OPSHL: return luaV_shiftl(v1, v2);
122
9.70k
    case LUA_OPSHR: return luaV_shiftr(v1, v2);
123
130k
    case LUA_OPUNM: return intop(-, 0, v1);
124
564k
    case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
125
0
    default: lua_assert(0); return 0;
126
1.19M
  }
127
1.19M
}
128
129
130
static lua_Number numarith (lua_State *L, int op, lua_Number v1,
131
951k
                                                  lua_Number v2) {
132
951k
  switch (op) {
133
13.3k
    case LUA_OPADD: return luai_numadd(L, v1, v2);
134
65.4k
    case LUA_OPSUB: return luai_numsub(L, v1, v2);
135
26.9k
    case LUA_OPMUL: return luai_nummul(L, v1, v2);
136
80.9k
    case LUA_OPDIV: return luai_numdiv(L, v1, v2);
137
588k
    case LUA_OPPOW: return luai_numpow(L, v1, v2);
138
40.8k
    case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
139
39.4k
    case LUA_OPUNM: return luai_numunm(L, v1);
140
95.9k
    case LUA_OPMOD: return luaV_modf(L, v1, v2);
141
0
    default: lua_assert(0); return 0;
142
951k
  }
143
951k
}
144
145
146
int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
147
2.14M
                   TValue *res) {
148
2.14M
  switch (op) {
149
137k
    case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
150
153k
    case LUA_OPSHL: case LUA_OPSHR:
151
717k
    case LUA_OPBNOT: {  /* operate only on integers */
152
717k
      lua_Integer i1; lua_Integer i2;
153
717k
      if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
154
717k
        setivalue(res, intarith(L, op, i1, i2));
155
717k
        return 1;
156
717k
      }
157
0
      else return 0;  /* fail */
158
717k
    }
159
669k
    case LUA_OPDIV: case LUA_OPPOW: {  /* operate only on floats */
160
669k
      lua_Number n1; lua_Number n2;
161
669k
      if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
162
669k
        setfltvalue(res, numarith(L, op, n1, n2));
163
669k
        return 1;
164
669k
      }
165
0
      else return 0;  /* fail */
166
669k
    }
167
755k
    default: {  /* other operations */
168
755k
      lua_Number n1; lua_Number n2;
169
755k
      if (ttisinteger(p1) && ttisinteger(p2)) {
170
473k
        setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
171
473k
        return 1;
172
473k
      }
173
281k
      else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
174
281k
        setfltvalue(res, numarith(L, op, n1, n2));
175
281k
        return 1;
176
281k
      }
177
0
      else return 0;  /* fail */
178
755k
    }
179
2.14M
  }
180
2.14M
}
181
182
183
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
184
241k
                 StkId res) {
185
241k
  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
241k
}
190
191
192
161k
int luaO_hexavalue (int c) {
193
161k
  if (lisdigit(c)) return c - '0';
194
71.9k
  else return (ltolower(c) - 'a') + 10;
195
161k
}
196
197
198
6.80M
static int isneg (const char **s) {
199
6.80M
  if (**s == '-') { (*s)++; return 1; }
200
6.78M
  else if (**s == '+') (*s)++;
201
6.78M
  return 0;
202
6.80M
}
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
4.28k
#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
514k
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
286
514k
  char *endptr;
287
  *result = (mode == 'x') ? lua_strx2number(s, &endptr)  /* try to convert */
288
514k
                          : lua_str2number(s, &endptr);
289
514k
  if (endptr == s) return NULL;  /* nothing recognized? */
290
483k
  while (lisspace(cast_uchar(*endptr))) endptr++;  /* skip trailing spaces */
291
483k
  return (*endptr == '\0') ? endptr : NULL;  /* OK iff no trailing chars */
292
514k
}
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
1.66M
static const char *l_str2d (const char *s, lua_Number *result) {
309
1.66M
  const char *endptr;
310
1.66M
  const char *pmode = strpbrk(s, ".xXnN");  /* look for special chars */
311
1.66M
  int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
312
1.66M
  if (mode == 'n')  /* reject 'inf' and 'nan' */
313
1.15M
    return NULL;
314
510k
  endptr = l_str2dloc(s, result, mode);  /* try to convert */
315
510k
  if (endptr == NULL) {  /* failed? may be a different locale */
316
146k
    char buff[L_MAXLENNUM + 1];
317
146k
    const char *pdot = strchr(s, '.');
318
146k
    if (pdot == NULL || strlen(s) > L_MAXLENNUM)
319
142k
      return NULL;  /* string too long or no dot; fail */
320
4.12k
    strcpy(buff, s);  /* copy string to buffer */
321
4.12k
    buff[pdot - s] = lua_getlocaledecpoint();  /* correct decimal point */
322
4.12k
    endptr = l_str2dloc(buff, result, mode);  /* try again */
323
4.12k
    if (endptr != NULL)
324
0
      endptr = s + (endptr - buff);  /* make relative to 's' */
325
4.12k
  }
326
367k
  return endptr;
327
510k
}
328
329
330
12.2M
#define MAXBY10   cast(lua_Unsigned, LUA_MAXINTEGER / 10)
331
12.6k
#define MAXLASTD  cast_int(LUA_MAXINTEGER % 10)
332
333
6.80M
static const char *l_str2int (const char *s, lua_Integer *result) {
334
6.80M
  lua_Unsigned a = 0;
335
6.80M
  int empty = 1;
336
6.80M
  int neg;
337
6.80M
  while (lisspace(cast_uchar(*s))) s++;  /* skip initial spaces */
338
6.80M
  neg = isneg(&s);
339
6.80M
  if (s[0] == '0' &&
340
6.80M
      (s[1] == 'x' || s[1] == 'X')) {  /* hex? */
341
1.18k
    s += 2;  /* skip '0x' */
342
7.53k
    for (; lisxdigit(cast_uchar(*s)); s++) {
343
7.53k
      a = a * 16 + luaO_hexavalue(*s);
344
7.53k
      empty = 0;
345
7.53k
    }
346
1.18k
  }
347
6.80M
  else {  /* decimal */
348
12.2M
    for (; lisdigit(cast_uchar(*s)); s++) {
349
12.2M
      int d = *s - '0';
350
12.2M
      if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg))  /* overflow? */
351
47.6k
        return NULL;  /* do not accept it (as integer) */
352
12.1M
      a = a * 10 + d;
353
12.1M
      empty = 0;
354
12.1M
    }
355
6.80M
  }
356
6.75M
  while (lisspace(cast_uchar(*s))) s++;  /* skip trailing spaces */
357
6.75M
  if (empty || *s != '\0') return NULL;  /* something wrong in the numeral */
358
5.14M
  else {
359
5.14M
    *result = l_castU2S((neg) ? 0u - a : a);
360
5.14M
    return s;
361
5.14M
  }
362
6.75M
}
363
364
365
6.80M
size_t luaO_str2num (const char *s, TValue *o) {
366
6.80M
  lua_Integer i; lua_Number n;
367
6.80M
  const char *e;
368
6.80M
  if ((e = l_str2int(s, &i)) != NULL) {  /* try as an integer */
369
5.14M
    setivalue(o, i);
370
5.14M
  }
371
1.66M
  else if ((e = l_str2d(s, &n)) != NULL) {  /* else try as a float */
372
363k
    setfltvalue(o, n);
373
363k
  }
374
1.30M
  else
375
1.30M
    return 0;  /* conversion failed */
376
5.50M
  return (e - s) + 1;  /* success; return string size */
377
6.80M
}
378
379
380
50.3k
int luaO_utf8esc (char *buff, unsigned long x) {
381
50.3k
  int n = 1;  /* number of bytes put in buffer (backwards) */
382
50.3k
  lua_assert(x <= 0x7FFFFFFFu);
383
50.3k
  if (x < 0x80)  /* ascii? */
384
39.3k
    buff[UTF8BUFFSZ - 1] = cast_char(x);
385
10.9k
  else {  /* need continuation bytes */
386
10.9k
    unsigned int mfb = 0x3f;  /* maximum that fits in first byte */
387
36.1k
    do {  /* add continuation bytes */
388
36.1k
      buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
389
36.1k
      x >>= 6;  /* remove added bits */
390
36.1k
      mfb >>= 1;  /* now there is one less bit available in first byte */
391
36.1k
    } while (x > mfb);  /* still needs continuation byte? */
392
10.9k
    buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x);  /* add first byte */
393
10.9k
  }
394
50.3k
  return n;
395
50.3k
}
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
70.4M
#define MAXNUMBER2STR 44
407
408
409
/*
410
** Convert a number object to a string, adding it to a buffer
411
*/
412
10.2M
static int tostringbuff (TValue *obj, char *buff) {
413
10.2M
  int len;
414
10.2M
  lua_assert(ttisnumber(obj));
415
10.2M
  if (ttisinteger(obj))
416
7.72M
    len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj));
417
2.49M
  else {
418
2.49M
    len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj));
419
2.49M
    if (buff[strspn(buff, "-0123456789")] == '\0') {  /* looks like an int? */
420
2.38M
      buff[len++] = lua_getlocaledecpoint();
421
2.38M
      buff[len++] = '0';  /* adds '.0' to result */
422
2.38M
    }
423
2.49M
  }
424
10.2M
  return len;
425
10.2M
}
426
427
428
/*
429
** Convert a number object to a Lua string, replacing the value at 'obj'
430
*/
431
7.22M
void luaO_tostring (lua_State *L, TValue *obj) {
432
7.22M
  char buff[MAXNUMBER2STR];
433
7.22M
  int len = tostringbuff(obj, buff);
434
7.22M
  setsvalue(L, obj, luaS_newlstr(L, buff, len));
435
7.22M
}
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
67.4M
#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
8.11M
static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
472
8.11M
  lua_State *L = buff->L;
473
8.11M
  setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
474
8.11M
  L->top.p++;  /* may use one slot from EXTRA_STACK */
475
8.11M
  if (!buff->pushed)  /* no previous string on the stack? */
476
7.23M
    buff->pushed = 1;  /* now there is one */
477
879k
  else  /* join previous string with new one */
478
879k
    luaV_concat(L, 2);
479
8.11M
}
480
481
482
/*
483
** empty the buffer space into the stack
484
*/
485
7.68M
static void clearbuff (BuffFS *buff) {
486
7.68M
  pushstr(buff, buff->space, buff->blen);  /* push buffer contents */
487
7.68M
  buff->blen = 0;  /* space now is empty */
488
7.68M
}
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
35.0M
static char *getbuff (BuffFS *buff, int sz) {
496
35.0M
  lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
497
35.0M
  if (sz > BUFVFS - buff->blen)  /* not enough space? */
498
20.8k
    clearbuff(buff);
499
35.0M
  return buff->space + buff->blen;
500
35.0M
}
501
502
503
35.0M
#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
32.4M
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
511
32.4M
  if (slen <= BUFVFS) {  /* does string fit into buffer? */
512
31.9M
    char *bf = getbuff(buff, cast_int(slen));
513
31.9M
    memcpy(bf, str, slen);  /* add string to buffer */
514
31.9M
    addsize(buff, cast_int(slen));
515
31.9M
  }
516
429k
  else {  /* string larger than buffer */
517
429k
    clearbuff(buff);  /* string comes after buffer's content */
518
429k
    pushstr(buff, str, slen);  /* push string */
519
429k
  }
520
32.4M
}
521
522
523
/*
524
** Add a numeral to the buffer.
525
*/
526
2.99M
static void addnum2buff (BuffFS *buff, TValue *num) {
527
2.99M
  char *numbuff = getbuff(buff, MAXNUMBER2STR);
528
2.99M
  int len = tostringbuff(num, numbuff);  /* format number into 'numbuff' */
529
2.99M
  addsize(buff, len);
530
2.99M
}
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
7.23M
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
538
7.23M
  BuffFS buff;  /* holds last part of the result */
539
7.23M
  const char *e;  /* points to next '%' */
540
7.23M
  buff.pushed = buff.blen = 0;
541
7.23M
  buff.L = L;
542
21.3M
  while ((e = strchr(fmt, '%')) != NULL) {
543
14.1M
    addstr2buff(&buff, fmt, e - fmt);  /* add 'fmt' up to '%' */
544
14.1M
    switch (*(e + 1)) {  /* conversion specifier */
545
10.5M
      case 's': {  /* zero-terminated string */
546
10.5M
        const char *s = va_arg(argp, char *);
547
10.5M
        if (s == NULL) s = "(null)";
548
10.5M
        addstr2buff(&buff, s, strlen(s));
549
10.5M
        break;
550
0
      }
551
476k
      case 'c': {  /* an 'int' as a character */
552
476k
        char c = cast_uchar(va_arg(argp, int));
553
476k
        addstr2buff(&buff, &c, sizeof(char));
554
476k
        break;
555
0
      }
556
2.96M
      case 'd': {  /* an 'int' */
557
2.96M
        TValue num;
558
2.96M
        setivalue(&num, va_arg(argp, int));
559
2.96M
        addnum2buff(&buff, &num);
560
2.96M
        break;
561
0
      }
562
3.22k
      case 'I': {  /* a 'lua_Integer' */
563
3.22k
        TValue num;
564
3.22k
        setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
565
3.22k
        addnum2buff(&buff, &num);
566
3.22k
        break;
567
0
      }
568
29.2k
      case 'f': {  /* a 'lua_Number' */
569
29.2k
        TValue num;
570
29.2k
        setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
571
29.2k
        addnum2buff(&buff, &num);
572
29.2k
        break;
573
0
      }
574
99.5k
      case 'p': {  /* a pointer */
575
99.5k
        const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
576
99.5k
        char *bf = getbuff(&buff, sz);
577
99.5k
        void *p = va_arg(argp, void *);
578
99.5k
        int len = lua_pointer2str(bf, sz, p);
579
99.5k
        addsize(&buff, len);
580
99.5k
        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
3.08k
      case '%': {
589
3.08k
        addstr2buff(&buff, "%", 1);
590
3.08k
        break;
591
0
      }
592
0
      default: {
593
0
        luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
594
0
                         *(e + 1));
595
0
      }
596
14.1M
    }
597
14.1M
    fmt = e + 2;  /* skip '%' and the specifier */
598
14.1M
  }
599
7.23M
  addstr2buff(&buff, fmt, strlen(fmt));  /* rest of 'fmt' */
600
7.23M
  clearbuff(&buff);  /* empty buffer into the stack */
601
7.23M
  lua_assert(buff.pushed == 1);
602
7.23M
  return getstr(tsvalue(s2v(L->top.p - 1)));
603
7.23M
}
604
605
606
4.90M
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
607
4.90M
  const char *msg;
608
4.90M
  va_list argp;
609
4.90M
  va_start(argp, fmt);
610
4.90M
  msg = luaO_pushvfstring(L, fmt, argp);
611
4.90M
  va_end(argp);
612
4.90M
  return msg;
613
4.90M
}
614
615
/* }================================================================== */
616
617
618
#define RETS  "..."
619
#define PRE "[string \""
620
2.13M
#define POS "\"]"
621
622
4.78M
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
623
624
2.42M
void luaO_chunkid (char *out, const char *source, size_t srclen) {
625
2.42M
  size_t bufflen = LUA_IDSIZE;  /* free space in buffer */
626
2.42M
  if (*source == '=') {  /* 'literal' source */
627
287k
    if (srclen <= bufflen)  /* small enough? */
628
286k
      memcpy(out, source + 1, srclen * sizeof(char));
629
493
    else {  /* truncate it */
630
493
      addstr(out, source + 1, bufflen - 1);
631
493
      *out = '\0';
632
493
    }
633
287k
  }
634
2.13M
  else if (*source == '@') {  /* file name */
635
1.96k
    if (srclen <= bufflen)  /* small enough? */
636
913
      memcpy(out, source + 1, srclen * sizeof(char));
637
1.05k
    else {  /* add '...' before rest of name */
638
1.05k
      addstr(out, RETS, LL(RETS));
639
1.05k
      bufflen -= LL(RETS);
640
1.05k
      memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
641
1.05k
    }
642
1.96k
  }
643
2.13M
  else {  /* string; format as [string "source"] */
644
2.13M
    const char *nl = strchr(source, '\n');  /* find first new line (if any) */
645
2.13M
    addstr(out, PRE, LL(PRE));  /* add prefix */
646
2.13M
    bufflen -= LL(PRE RETS POS) + 1;  /* save space for prefix+suffix+'\0' */
647
2.13M
    if (srclen < bufflen && nl == NULL) {  /* small one-line source? */
648
1.62M
      addstr(out, source, srclen);  /* keep it */
649
1.62M
    }
650
512k
    else {
651
512k
      if (nl != NULL) srclen = nl - source;  /* stop at first newline */
652
512k
      if (srclen > bufflen) srclen = bufflen;
653
512k
      addstr(out, source, srclen);
654
512k
      addstr(out, RETS, LL(RETS));
655
512k
    }
656
2.13M
    memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
657
2.13M
  }
658
2.42M
}
659