Coverage Report

Created: 2023-09-15 06:20

/src/testdir/build/lua-master/source/lbaselib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: lbaselib.c $
3
** Basic library
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lbaselib_c
8
#define LUA_LIB
9
10
#include "lprefix.h"
11
12
13
#include <ctype.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "lua.h"
19
20
#include "lauxlib.h"
21
#include "lualib.h"
22
23
24
3.35k
static int luaB_print (lua_State *L) {
25
3.35k
  int n = lua_gettop(L);  /* number of arguments */
26
3.35k
  int i;
27
7.91k
  for (i = 1; i <= n; i++) {  /* for each argument */
28
4.55k
    size_t l;
29
4.55k
    const char *s = luaL_tolstring(L, i, &l);  /* convert it to string */
30
4.55k
    if (i > 1)  /* not the first element? */
31
1.19k
      lua_writestring("\t", 1);  /* add a tab before it */
32
4.55k
    lua_writestring(s, l);  /* print it */
33
4.55k
    lua_pop(L, 1);  /* pop result */
34
4.55k
  }
35
3.35k
  lua_writeline();
36
3.35k
  return 0;
37
3.35k
}
38
39
40
/*
41
** Creates a warning with all given arguments.
42
** Check first for errors; otherwise an error may interrupt
43
** the composition of a warning, leaving it unfinished.
44
*/
45
8.01k
static int luaB_warn (lua_State *L) {
46
8.01k
  int n = lua_gettop(L);  /* number of arguments */
47
8.01k
  int i;
48
8.01k
  luaL_checkstring(L, 1);  /* at least one argument */
49
8.01k
  for (i = 2; i <= n; i++)
50
0
    luaL_checkstring(L, i);  /* make sure all arguments are strings */
51
8.01k
  for (i = 1; i < n; i++)  /* compose warning */
52
0
    lua_warning(L, lua_tostring(L, i), 1);
53
8.01k
  lua_warning(L, lua_tostring(L, n), 0);  /* close warning */
54
8.01k
  return 0;
55
8.01k
}
56
57
58
0
#define SPACECHARS  " \f\n\r\t\v"
59
60
0
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
61
0
  lua_Unsigned n = 0;
62
0
  int neg = 0;
63
0
  s += strspn(s, SPACECHARS);  /* skip initial spaces */
64
0
  if (*s == '-') { s++; neg = 1; }  /* handle sign */
65
0
  else if (*s == '+') s++;
66
0
  if (!isalnum((unsigned char)*s))  /* no digit? */
67
0
    return NULL;
68
0
  do {
69
0
    int digit = (isdigit((unsigned char)*s)) ? *s - '0'
70
0
                   : (toupper((unsigned char)*s) - 'A') + 10;
71
0
    if (digit >= base) return NULL;  /* invalid numeral */
72
0
    n = n * base + digit;
73
0
    s++;
74
0
  } while (isalnum((unsigned char)*s));
75
0
  s += strspn(s, SPACECHARS);  /* skip trailing spaces */
76
0
  *pn = (lua_Integer)((neg) ? (0u - n) : n);
77
0
  return s;
78
0
}
79
80
81
2.49k
static int luaB_tonumber (lua_State *L) {
82
2.49k
  if (lua_isnoneornil(L, 2)) {  /* standard conversion? */
83
2.49k
    if (lua_type(L, 1) == LUA_TNUMBER) {  /* already a number? */
84
0
      lua_settop(L, 1);  /* yes; return it */
85
0
      return 1;
86
0
    }
87
2.49k
    else {
88
2.49k
      size_t l;
89
2.49k
      const char *s = lua_tolstring(L, 1, &l);
90
2.49k
      if (s != NULL && lua_stringtonumber(L, s) == l + 1)
91
669
        return 1;  /* successful conversion to number */
92
      /* else not a number */
93
1.82k
      luaL_checkany(L, 1);  /* (but there must be some parameter) */
94
1.82k
    }
95
2.49k
  }
96
0
  else {
97
0
    size_t l;
98
0
    const char *s;
99
0
    lua_Integer n = 0;  /* to avoid warnings */
100
0
    lua_Integer base = luaL_checkinteger(L, 2);
101
0
    luaL_checktype(L, 1, LUA_TSTRING);  /* no numbers as strings */
102
0
    s = lua_tolstring(L, 1, &l);
103
0
    luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
104
0
    if (b_str2int(s, (int)base, &n) == s + l) {
105
0
      lua_pushinteger(L, n);
106
0
      return 1;
107
0
    }  /* else not a number */
108
0
  }  /* else not a number */
109
1.82k
  luaL_pushfail(L);  /* not a number */
110
1.82k
  return 1;
111
2.49k
}
112
113
114
0
static int luaB_error (lua_State *L) {
115
0
  int level = (int)luaL_optinteger(L, 2, 1);
116
0
  lua_settop(L, 1);
117
0
  if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
118
0
    luaL_where(L, level);   /* add extra information */
119
0
    lua_pushvalue(L, 1);
120
0
    lua_concat(L, 2);
121
0
  }
122
0
  return lua_error(L);
123
0
}
124
125
126
0
static int luaB_getmetatable (lua_State *L) {
127
0
  luaL_checkany(L, 1);
128
0
  if (!lua_getmetatable(L, 1)) {
129
0
    lua_pushnil(L);
130
0
    return 1;  /* no metatable */
131
0
  }
132
0
  luaL_getmetafield(L, 1, "__metatable");
133
0
  return 1;  /* returns either __metatable field (if present) or metatable */
134
0
}
135
136
137
34
static int luaB_setmetatable (lua_State *L) {
138
34
  int t = lua_type(L, 2);
139
34
  luaL_checktype(L, 1, LUA_TTABLE);
140
34
  luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
141
34
  if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL))
142
0
    return luaL_error(L, "cannot change a protected metatable");
143
34
  lua_settop(L, 2);
144
34
  lua_setmetatable(L, 1);
145
34
  return 1;
146
34
}
147
148
149
0
static int luaB_rawequal (lua_State *L) {
150
0
  luaL_checkany(L, 1);
151
0
  luaL_checkany(L, 2);
152
0
  lua_pushboolean(L, lua_rawequal(L, 1, 2));
153
0
  return 1;
154
0
}
155
156
157
250k
static int luaB_rawlen (lua_State *L) {
158
250k
  int t = lua_type(L, 1);
159
250k
  luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
160
250k
                      "table or string");
161
250k
  lua_pushinteger(L, lua_rawlen(L, 1));
162
250k
  return 1;
163
250k
}
164
165
166
0
static int luaB_rawget (lua_State *L) {
167
0
  luaL_checktype(L, 1, LUA_TTABLE);
168
0
  luaL_checkany(L, 2);
169
0
  lua_settop(L, 2);
170
0
  lua_rawget(L, 1);
171
0
  return 1;
172
0
}
173
174
0
static int luaB_rawset (lua_State *L) {
175
0
  luaL_checktype(L, 1, LUA_TTABLE);
176
0
  luaL_checkany(L, 2);
177
0
  luaL_checkany(L, 3);
178
0
  lua_settop(L, 3);
179
0
  lua_rawset(L, 1);
180
0
  return 1;
181
0
}
182
183
184
1.27k
static int pushmode (lua_State *L, int oldmode) {
185
1.27k
  if (oldmode == -1)
186
0
    luaL_pushfail(L);  /* invalid call to 'lua_gc' */
187
1.27k
  else
188
1.27k
    lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
189
1.27k
                                             : "generational");
190
1.27k
  return 1;
191
1.27k
}
192
193
194
/*
195
** check whether call to 'lua_gc' was valid (not inside a finalizer)
196
*/
197
6.95k
#define checkvalres(res) { if (res == -1) break; }
198
199
8.24k
static int luaB_collectgarbage (lua_State *L) {
200
8.24k
  static const char *const opts[] = {"stop", "restart", "collect",
201
8.24k
    "count", "step", "setpause", "setstepmul",
202
8.24k
    "isrunning", "generational", "incremental", NULL};
203
8.24k
  static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
204
8.24k
    LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
205
8.24k
    LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
206
8.24k
  int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
207
8.24k
  switch (o) {
208
307
    case LUA_GCCOUNT: {
209
307
      int k = lua_gc(L, o);
210
307
      int b = lua_gc(L, LUA_GCCOUNTB);
211
307
      checkvalres(k);
212
307
      lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
213
307
      return 1;
214
307
    }
215
2.71k
    case LUA_GCSTEP: {
216
2.71k
      int step = (int)luaL_optinteger(L, 2, 0);
217
2.71k
      int res = lua_gc(L, o, step);
218
2.71k
      checkvalres(res);
219
2.71k
      lua_pushboolean(L, res);
220
2.71k
      return 1;
221
2.71k
    }
222
936
    case LUA_GCSETPAUSE:
223
1.03k
    case LUA_GCSETSTEPMUL: {
224
1.03k
      int p = (int)luaL_optinteger(L, 2, 0);
225
1.03k
      int previous = lua_gc(L, o, p);
226
1.03k
      checkvalres(previous);
227
1.03k
      lua_pushinteger(L, previous);
228
1.03k
      return 1;
229
1.03k
    }
230
386
    case LUA_GCISRUNNING: {
231
386
      int res = lua_gc(L, o);
232
386
      checkvalres(res);
233
386
      lua_pushboolean(L, res);
234
386
      return 1;
235
386
    }
236
1.27k
    case LUA_GCGEN: {
237
1.27k
      int minormul = (int)luaL_optinteger(L, 2, 0);
238
1.27k
      int majormul = (int)luaL_optinteger(L, 3, 0);
239
1.27k
      return pushmode(L, lua_gc(L, o, minormul, majormul));
240
386
    }
241
1
    case LUA_GCINC: {
242
1
      int pause = (int)luaL_optinteger(L, 2, 0);
243
1
      int stepmul = (int)luaL_optinteger(L, 3, 0);
244
1
      int stepsize = (int)luaL_optinteger(L, 4, 0);
245
1
      return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
246
386
    }
247
2.51k
    default: {
248
2.51k
      int res = lua_gc(L, o);
249
2.51k
      checkvalres(res);
250
2.51k
      lua_pushinteger(L, res);
251
2.51k
      return 1;
252
2.51k
    }
253
8.24k
  }
254
0
  luaL_pushfail(L);  /* invalid call (inside a finalizer) */
255
0
  return 1;
256
8.24k
}
257
258
259
2.66k
static int luaB_type (lua_State *L) {
260
2.66k
  int t = lua_type(L, 1);
261
2.66k
  luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
262
2.66k
  lua_pushstring(L, lua_typename(L, t));
263
2.66k
  return 1;
264
2.66k
}
265
266
267
65
static int luaB_next (lua_State *L) {
268
65
  luaL_checktype(L, 1, LUA_TTABLE);
269
65
  lua_settop(L, 2);  /* create a 2nd argument if there isn't one */
270
65
  if (lua_next(L, 1))
271
0
    return 2;
272
65
  else {
273
65
    lua_pushnil(L);
274
65
    return 1;
275
65
  }
276
65
}
277
278
279
0
static int pairscont (lua_State *L, int status, lua_KContext k) {
280
0
  (void)L; (void)status; (void)k;  /* unused */
281
0
  return 3;
282
0
}
283
284
484
static int luaB_pairs (lua_State *L) {
285
484
  luaL_checkany(L, 1);
286
484
  if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) {  /* no metamethod? */
287
484
    lua_pushcfunction(L, luaB_next);  /* will return generator, */
288
484
    lua_pushvalue(L, 1);  /* state, */
289
484
    lua_pushnil(L);  /* and initial value */
290
484
  }
291
0
  else {
292
0
    lua_pushvalue(L, 1);  /* argument 'self' to metamethod */
293
0
    lua_callk(L, 1, 3, 0, pairscont);  /* get 3 values from metamethod */
294
0
  }
295
484
  return 3;
296
484
}
297
298
299
/*
300
** Traversal function for 'ipairs'
301
*/
302
13
static int ipairsaux (lua_State *L) {
303
13
  lua_Integer i = luaL_checkinteger(L, 2);
304
13
  i = luaL_intop(+, i, 1);
305
13
  lua_pushinteger(L, i);
306
13
  return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
307
13
}
308
309
310
/*
311
** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
312
** (The given "table" may not be a table.)
313
*/
314
1.99k
static int luaB_ipairs (lua_State *L) {
315
1.99k
  luaL_checkany(L, 1);
316
1.99k
  lua_pushcfunction(L, ipairsaux);  /* iteration function */
317
1.99k
  lua_pushvalue(L, 1);  /* state */
318
1.99k
  lua_pushinteger(L, 0);  /* initial value */
319
1.99k
  return 3;
320
1.99k
}
321
322
323
194k
static int load_aux (lua_State *L, int status, int envidx) {
324
194k
  if (l_likely(status == LUA_OK)) {
325
23.1k
    if (envidx != 0) {  /* 'env' parameter? */
326
0
      lua_pushvalue(L, envidx);  /* environment for loaded function */
327
0
      if (!lua_setupvalue(L, -2, 1))  /* set it as 1st upvalue */
328
0
        lua_pop(L, 1);  /* remove 'env' if not used by previous call */
329
0
    }
330
23.1k
    return 1;
331
23.1k
  }
332
171k
  else {  /* error (message is on top of the stack) */
333
171k
    luaL_pushfail(L);
334
171k
    lua_insert(L, -2);  /* put before error message */
335
171k
    return 2;  /* return fail plus error message */
336
171k
  }
337
194k
}
338
339
340
2.25k
static int luaB_loadfile (lua_State *L) {
341
2.25k
  const char *fname = luaL_optstring(L, 1, NULL);
342
2.25k
  const char *mode = luaL_optstring(L, 2, NULL);
343
2.25k
  int env = (!lua_isnone(L, 3) ? 3 : 0);  /* 'env' index or 0 if no 'env' */
344
2.25k
  int status = luaL_loadfilex(L, fname, mode);
345
2.25k
  return load_aux(L, status, env);
346
2.25k
}
347
348
349
/*
350
** {======================================================
351
** Generic Read function
352
** =======================================================
353
*/
354
355
356
/*
357
** reserved slot, above all arguments, to hold a copy of the returned
358
** string to avoid it being collected while parsed. 'load' has four
359
** optional arguments (chunk, source name, mode, and environment).
360
*/
361
25.1k
#define RESERVEDSLOT  5
362
363
364
/*
365
** Reader for generic 'load' function: 'lua_load' uses the
366
** stack for internal stuff, so the reader cannot change the
367
** stack top. Instead, it keeps its resulting string in a
368
** reserved slot inside the stack.
369
*/
370
9.37k
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
371
9.37k
  (void)(ud);  /* not used */
372
9.37k
  luaL_checkstack(L, 2, "too many nested functions");
373
9.37k
  lua_pushvalue(L, 1);  /* get function */
374
9.37k
  lua_call(L, 0, 1);  /* call it */
375
9.37k
  if (lua_isnil(L, -1)) {
376
142
    lua_pop(L, 1);  /* pop result */
377
142
    *size = 0;
378
142
    return NULL;
379
142
  }
380
9.23k
  else if (l_unlikely(!lua_isstring(L, -1)))
381
0
    luaL_error(L, "reader function must return a string");
382
9.23k
  lua_replace(L, RESERVEDSLOT);  /* save string in reserved slot */
383
9.23k
  return lua_tolstring(L, RESERVEDSLOT, size);
384
9.37k
}
385
386
387
198k
static int luaB_load (lua_State *L) {
388
198k
  int status;
389
198k
  size_t l;
390
198k
  const char *s = lua_tolstring(L, 1, &l);
391
198k
  const char *mode = luaL_optstring(L, 3, "bt");
392
198k
  int env = (!lua_isnone(L, 4) ? 4 : 0);  /* 'env' index or 0 if no 'env' */
393
198k
  if (s != NULL) {  /* loading a string? */
394
182k
    const char *chunkname = luaL_optstring(L, 2, s);
395
182k
    status = luaL_loadbufferx(L, s, l, chunkname, mode);
396
182k
  }
397
15.9k
  else {  /* loading from a reader function */
398
15.9k
    const char *chunkname = luaL_optstring(L, 2, "=(load)");
399
15.9k
    luaL_checktype(L, 1, LUA_TFUNCTION);
400
15.9k
    lua_settop(L, RESERVEDSLOT);  /* create reserved slot */
401
15.9k
    status = lua_load(L, generic_reader, NULL, chunkname, mode);
402
15.9k
  }
403
198k
  return load_aux(L, status, env);
404
198k
}
405
406
/* }====================================================== */
407
408
409
28
static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
410
28
  (void)d1;  (void)d2;  /* only to match 'lua_Kfunction' prototype */
411
28
  return lua_gettop(L) - 1;
412
28
}
413
414
415
28
static int luaB_dofile (lua_State *L) {
416
28
  const char *fname = luaL_optstring(L, 1, NULL);
417
28
  lua_settop(L, 1);
418
28
  if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
419
0
    return lua_error(L);
420
28
  lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
421
28
  return dofilecont(L, 0, 0);
422
28
}
423
424
425
615
static int luaB_assert (lua_State *L) {
426
615
  if (l_likely(lua_toboolean(L, 1)))  /* condition is true? */
427
615
    return lua_gettop(L);  /* return all arguments */
428
0
  else {  /* error */
429
0
    luaL_checkany(L, 1);  /* there must be a condition */
430
0
    lua_remove(L, 1);  /* remove it */
431
0
    lua_pushliteral(L, "assertion failed!");  /* default message */
432
0
    lua_settop(L, 1);  /* leave only message (default if no other one) */
433
0
    return luaB_error(L);  /* call 'error' */
434
0
  }
435
615
}
436
437
438
506
static int luaB_select (lua_State *L) {
439
506
  int n = lua_gettop(L);
440
506
  if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
441
0
    lua_pushinteger(L, n-1);
442
0
    return 1;
443
0
  }
444
506
  else {
445
506
    lua_Integer i = luaL_checkinteger(L, 1);
446
506
    if (i < 0) i = n + i;
447
506
    else if (i > n) i = n;
448
506
    luaL_argcheck(L, 1 <= i, 1, "index out of range");
449
506
    return n - (int)i;
450
506
  }
451
506
}
452
453
454
/*
455
** Continuation function for 'pcall' and 'xpcall'. Both functions
456
** already pushed a 'true' before doing the call, so in case of success
457
** 'finishpcall' only has to return everything in the stack minus
458
** 'extra' values (where 'extra' is exactly the number of items to be
459
** ignored).
460
*/
461
5.50k
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
462
5.50k
  if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) {  /* error? */
463
5.50k
    lua_pushboolean(L, 0);  /* first result (false) */
464
5.50k
    lua_pushvalue(L, -2);  /* error message */
465
5.50k
    return 2;  /* return false, msg */
466
5.50k
  }
467
0
  else
468
0
    return lua_gettop(L) - (int)extra;  /* return all results */
469
5.50k
}
470
471
472
5.50k
static int luaB_pcall (lua_State *L) {
473
5.50k
  int status;
474
5.50k
  luaL_checkany(L, 1);
475
5.50k
  lua_pushboolean(L, 1);  /* first result if no errors */
476
5.50k
  lua_insert(L, 1);  /* put it in place */
477
5.50k
  status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
478
5.50k
  return finishpcall(L, status, 0);
479
5.50k
}
480
481
482
/*
483
** Do a protected call with error handling. After 'lua_rotate', the
484
** stack will have <f, err, true, f, [args...]>; so, the function passes
485
** 2 to 'finishpcall' to skip the 2 first values when returning results.
486
*/
487
0
static int luaB_xpcall (lua_State *L) {
488
0
  int status;
489
0
  int n = lua_gettop(L);
490
0
  luaL_checktype(L, 2, LUA_TFUNCTION);  /* check error function */
491
0
  lua_pushboolean(L, 1);  /* first result */
492
0
  lua_pushvalue(L, 1);  /* function */
493
0
  lua_rotate(L, 3, 2);  /* move them below function's arguments */
494
0
  status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
495
0
  return finishpcall(L, status, 2);
496
0
}
497
498
499
8.13k
static int luaB_tostring (lua_State *L) {
500
8.13k
  luaL_checkany(L, 1);
501
8.13k
  luaL_tolstring(L, 1, NULL);
502
8.13k
  return 1;
503
8.13k
}
504
505
506
static const luaL_Reg base_funcs[] = {
507
  {"assert", luaB_assert},
508
  {"collectgarbage", luaB_collectgarbage},
509
  {"dofile", luaB_dofile},
510
  {"error", luaB_error},
511
  {"getmetatable", luaB_getmetatable},
512
  {"ipairs", luaB_ipairs},
513
  {"loadfile", luaB_loadfile},
514
  {"load", luaB_load},
515
  {"next", luaB_next},
516
  {"pairs", luaB_pairs},
517
  {"pcall", luaB_pcall},
518
  {"print", luaB_print},
519
  {"warn", luaB_warn},
520
  {"rawequal", luaB_rawequal},
521
  {"rawlen", luaB_rawlen},
522
  {"rawget", luaB_rawget},
523
  {"rawset", luaB_rawset},
524
  {"select", luaB_select},
525
  {"setmetatable", luaB_setmetatable},
526
  {"tonumber", luaB_tonumber},
527
  {"tostring", luaB_tostring},
528
  {"type", luaB_type},
529
  {"xpcall", luaB_xpcall},
530
  /* placeholders */
531
  {LUA_GNAME, NULL},
532
  {"_VERSION", NULL},
533
  {NULL, NULL}
534
};
535
536
537
3.20k
LUAMOD_API int luaopen_base (lua_State *L) {
538
  /* open lib into global table */
539
3.20k
  lua_pushglobaltable(L);
540
3.20k
  luaL_setfuncs(L, base_funcs, 0);
541
  /* set global _G */
542
3.20k
  lua_pushvalue(L, -1);
543
3.20k
  lua_setfield(L, -2, LUA_GNAME);
544
  /* set global _VERSION */
545
3.20k
  lua_pushliteral(L, LUA_VERSION);
546
3.20k
  lua_setfield(L, -2, "_VERSION");
547
3.20k
  return 1;
548
3.20k
}
549