Coverage Report

Created: 2026-03-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/lauxlib.c
Line
Count
Source
1
/*
2
** $Id: lauxlib.c $
3
** Auxiliary functions for building Lua libraries
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lauxlib_c
8
#define LUA_LIB
9
10
#include "lprefix.h"
11
12
13
#include <errno.h>
14
#include <stdarg.h>
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <string.h>
18
19
20
/*
21
** This file uses only the official API of Lua.
22
** Any function declared here could be written as an application function.
23
*/
24
25
#include "lua.h"
26
27
#include "lauxlib.h"
28
#include "llimits.h"
29
30
31
/*
32
** {======================================================
33
** Traceback
34
** =======================================================
35
*/
36
37
38
9.11k
#define LEVELS1 10  /* size of the first part of the stack */
39
9.11k
#define LEVELS2 11  /* size of the second part of the stack */
40
41
42
43
/*
44
** Search for 'objidx' in table at index -1. ('objidx' must be an
45
** absolute index.) Return 1 + string at top if it found a good name.
46
*/
47
59.8M
static int findfield (lua_State *L, int objidx, int level) {
48
59.8M
  if (level == 0 || !lua_istable(L, -1))
49
55.4M
    return 0;  /* not found */
50
4.41M
  lua_pushnil(L);  /* start 'next' loop */
51
78.7M
  while (lua_next(L, -2)) {  /* for each pair in table */
52
75.7M
    if (lua_type(L, -2) == LUA_TSTRING) {  /* ignore non-string keys */
53
59.8M
      if (lua_rawequal(L, objidx, -1)) {  /* found object? */
54
727k
        lua_pop(L, 1);  /* remove value (but keep name) */
55
727k
        return 1;
56
727k
      }
57
59.1M
      else if (findfield(L, objidx, level - 1)) {  /* try recursively */
58
        /* stack: lib_name, lib_table, field_name (top) */
59
727k
        lua_pushliteral(L, ".");  /* place '.' between the two names */
60
727k
        lua_replace(L, -3);  /* (in the slot occupied by table) */
61
727k
        lua_concat(L, 3);  /* lib_name.field_name */
62
727k
        return 1;
63
727k
      }
64
59.8M
    }
65
74.2M
    lua_pop(L, 1);  /* remove value */
66
74.2M
  }
67
2.96M
  return 0;  /* not found */
68
4.41M
}
69
70
71
/*
72
** Search for a name for a function in all loaded modules
73
*/
74
755k
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
75
755k
  int top = lua_gettop(L);
76
755k
  lua_getinfo(L, "f", ar);  /* push function */
77
755k
  lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
78
755k
  luaL_checkstack(L, 6, "not enough stack");  /* slots for 'findfield' */
79
755k
  if (findfield(L, top + 1, 2)) {
80
727k
    const char *name = lua_tostring(L, -1);
81
727k
    if (strncmp(name, LUA_GNAME ".", 3) == 0) {  /* name start with '_G.'? */
82
726k
      lua_pushstring(L, name + 3);  /* push name without prefix */
83
726k
      lua_remove(L, -2);  /* remove original name */
84
726k
    }
85
727k
    lua_copy(L, -1, top + 1);  /* copy name to proper place */
86
727k
    lua_settop(L, top + 1);  /* remove table "loaded" and name copy */
87
727k
    return 1;
88
727k
  }
89
27.5k
  else {
90
27.5k
    lua_settop(L, top);  /* remove function and global table */
91
27.5k
    return 0;
92
27.5k
  }
93
755k
}
94
95
96
91.5k
static void pushfuncname (lua_State *L, lua_Debug *ar) {
97
91.5k
  if (*ar->namewhat != '\0')  /* is there a name from code? */
98
67.7k
    lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name);  /* use it */
99
23.7k
  else if (*ar->what == 'm')  /* main? */
100
4.62k
      lua_pushliteral(L, "main chunk");
101
19.1k
  else if (pushglobalfuncname(L, ar)) {  /* try a global name */
102
35
    lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
103
35
    lua_remove(L, -2);  /* remove name */
104
35
  }
105
19.1k
  else if (*ar->what != 'C')  /* for Lua functions, use <file:line> */
106
19.1k
    lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
107
23
  else  /* nothing left... */
108
23
    lua_pushliteral(L, "?");
109
91.5k
}
110
111
112
4.96k
static int lastlevel (lua_State *L) {
113
4.96k
  lua_Debug ar;
114
4.96k
  int li = 1, le = 1;
115
  /* find an upper bound */
116
37.9k
  while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
117
  /* do a binary search */
118
33.6k
  while (li < le) {
119
28.6k
    int m = (li + le)/2;
120
28.6k
    if (lua_getstack(L, m, &ar)) li = m + 1;
121
15.8k
    else le = m;
122
28.6k
  }
123
4.96k
  return le - 1;
124
4.96k
}
125
126
127
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
128
4.96k
                                const char *msg, int level) {
129
4.96k
  luaL_Buffer b;
130
4.96k
  lua_Debug ar;
131
4.96k
  int last = lastlevel(L1);
132
4.96k
  int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
133
4.96k
  luaL_buffinit(L, &b);
134
4.96k
  if (msg) {
135
4.96k
    luaL_addstring(&b, msg);
136
4.96k
    luaL_addchar(&b, '\n');
137
4.96k
  }
138
4.96k
  luaL_addstring(&b, "stack traceback:");
139
100k
  while (lua_getstack(L1, level++, &ar)) {
140
95.6k
    if (limit2show-- == 0) {  /* too many levels? */
141
4.15k
      int n = last - level - LEVELS2 + 1;  /* number of levels to skip */
142
4.15k
      lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n);
143
4.15k
      luaL_addvalue(&b);  /* add warning about skip */
144
4.15k
      level += n;  /* and skip to last levels */
145
4.15k
    }
146
91.5k
    else {
147
91.5k
      lua_getinfo(L1, "Slnt", &ar);
148
91.5k
      if (ar.currentline <= 0)
149
21.9k
        lua_pushfstring(L, "\n\t%s: in ", ar.short_src);
150
69.5k
      else
151
69.5k
        lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline);
152
91.5k
      luaL_addvalue(&b);
153
91.5k
      pushfuncname(L, &ar);
154
91.5k
      luaL_addvalue(&b);
155
91.5k
      if (ar.istailcall)
156
76
        luaL_addstring(&b, "\n\t(...tail calls...)");
157
91.5k
    }
158
95.6k
  }
159
4.96k
  luaL_pushresult(&b);
160
4.96k
}
161
162
/* }====================================================== */
163
164
165
/*
166
** {======================================================
167
** Error-report functions
168
** =======================================================
169
*/
170
171
924k
LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
172
924k
  lua_Debug ar;
173
924k
  const char *argword;
174
924k
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
175
0
    return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
176
924k
  lua_getinfo(L, "nt", &ar);
177
924k
  if (arg <= ar.extraargs)  /* error in an extra argument? */
178
0
    argword =  "extra argument";
179
924k
  else {
180
924k
    arg -= ar.extraargs;  /* do not count extra arguments */
181
924k
    if (strcmp(ar.namewhat, "method") == 0) {  /* colon syntax? */
182
54.9k
      arg--;  /* do not count (extra) self argument */
183
54.9k
      if (arg == 0)  /* error in self argument? */
184
5.02k
        return luaL_error(L, "calling '%s' on bad self (%s)",
185
5.02k
                               ar.name, extramsg);
186
      /* else go through; error in a regular argument */
187
54.9k
    }
188
919k
    argword = "argument";
189
919k
  }
190
919k
  if (ar.name == NULL)
191
735k
    ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
192
919k
  return luaL_error(L, "bad %s #%d to '%s' (%s)",
193
919k
                       argword, arg, ar.name, extramsg);
194
924k
}
195
196
197
729k
LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
198
729k
  const char *msg;
199
729k
  const char *typearg;  /* name for the type of the actual argument */
200
729k
  if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
201
0
    typearg = lua_tostring(L, -1);  /* use the given type name */
202
729k
  else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
203
0
    typearg = "light userdata";  /* special name for messages */
204
729k
  else
205
729k
    typearg = luaL_typename(L, arg);  /* standard name */
206
729k
  msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
207
729k
  return luaL_argerror(L, arg, msg);
208
729k
}
209
210
211
686k
static void tag_error (lua_State *L, int arg, int tag) {
212
686k
  luaL_typeerror(L, arg, lua_typename(L, tag));
213
686k
}
214
215
216
/*
217
** The use of 'lua_pushfstring' ensures this function does not
218
** need reserved stack space when called.
219
*/
220
1.24M
LUALIB_API void luaL_where (lua_State *L, int level) {
221
1.24M
  lua_Debug ar;
222
1.24M
  if (lua_getstack(L, level, &ar)) {  /* check function at level */
223
1.24M
    lua_getinfo(L, "Sl", &ar);  /* get info about it */
224
1.24M
    if (ar.currentline > 0) {  /* is there info? */
225
464k
      lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
226
464k
      return;
227
464k
    }
228
1.24M
  }
229
782k
  lua_pushfstring(L, "");  /* else, no information available... */
230
782k
}
231
232
233
/*
234
** Again, the use of 'lua_pushvfstring' ensures this function does
235
** not need reserved stack space when called. (At worst, it generates
236
** a memory error instead of the given message.)
237
*/
238
1.19M
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
239
1.19M
  va_list argp;
240
1.19M
  va_start(argp, fmt);
241
1.19M
  luaL_where(L, 1);
242
1.19M
  lua_pushvfstring(L, fmt, argp);
243
1.19M
  va_end(argp);
244
1.19M
  lua_concat(L, 2);
245
1.19M
  return lua_error(L);
246
1.19M
}
247
248
249
391k
LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
250
391k
  int en = errno;  /* calls to Lua API may change this value */
251
391k
  if (stat) {
252
370k
    lua_pushboolean(L, 1);
253
370k
    return 1;
254
370k
  }
255
20.6k
  else {
256
20.6k
    const char *msg;
257
20.6k
    luaL_pushfail(L);
258
20.6k
    msg = (en != 0) ? strerror(en) : "(no extra info)";
259
20.6k
    if (fname)
260
16.6k
      lua_pushfstring(L, "%s: %s", fname, msg);
261
4.04k
    else
262
4.04k
      lua_pushstring(L, msg);
263
20.6k
    lua_pushinteger(L, en);
264
20.6k
    return 3;
265
20.6k
  }
266
391k
}
267
268
269
#if !defined(l_inspectstat) /* { */
270
271
#if defined(LUA_USE_POSIX)
272
273
#include <sys/wait.h>
274
275
/*
276
** use appropriate macros to interpret 'pclose' return status
277
*/
278
#define l_inspectstat(stat,what)  \
279
   if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
280
   else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
281
282
#else
283
284
#define l_inspectstat(stat,what)  /* no op */
285
286
#endif
287
288
#endif        /* } */
289
290
291
0
LUALIB_API int luaL_execresult (lua_State *L, int stat) {
292
0
  if (stat != 0 && errno != 0)  /* error with an 'errno'? */
293
0
    return luaL_fileresult(L, 0, NULL);
294
0
  else {
295
0
    const char *what = "exit";  /* type of termination */
296
0
    l_inspectstat(stat, what);  /* interpret result */
297
0
    if (*what == 'e' && stat == 0)  /* successful termination? */
298
0
      lua_pushboolean(L, 1);
299
0
    else
300
0
      luaL_pushfail(L);
301
0
    lua_pushstring(L, what);
302
0
    lua_pushinteger(L, stat);
303
0
    return 3;  /* return true/fail,what,code */
304
0
  }
305
0
}
306
307
/* }====================================================== */
308
309
310
311
/*
312
** {======================================================
313
** Userdata's metatable manipulation
314
** =======================================================
315
*/
316
317
692k
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
318
692k
  if (luaL_getmetatable(L, tname) != LUA_TNIL)  /* name already in use? */
319
681k
    return 0;  /* leave previous value on top, but return 0 */
320
11.3k
  lua_pop(L, 1);
321
11.3k
  lua_createtable(L, 0, 2);  /* create metatable */
322
11.3k
  lua_pushstring(L, tname);
323
11.3k
  lua_setfield(L, -2, "__name");  /* metatable.__name = tname */
324
11.3k
  lua_pushvalue(L, -1);
325
11.3k
  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
326
11.3k
  return 1;
327
692k
}
328
329
330
397k
LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
331
397k
  luaL_getmetatable(L, tname);
332
397k
  lua_setmetatable(L, -2);
333
397k
}
334
335
336
1.88M
LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
337
1.88M
  void *p = lua_touserdata(L, ud);
338
1.88M
  if (p != NULL) {  /* value is a userdata? */
339
1.87M
    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */
340
1.87M
      luaL_getmetatable(L, tname);  /* get correct metatable */
341
1.87M
      if (!lua_rawequal(L, -1, -2))  /* not the same? */
342
20
        p = NULL;  /* value is a userdata with wrong metatable */
343
1.87M
      lua_pop(L, 2);  /* remove both metatables */
344
1.87M
      return p;
345
1.87M
    }
346
1.87M
  }
347
8.83k
  return NULL;  /* value is not a userdata with a metatable */
348
1.88M
}
349
350
351
1.87M
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
352
1.87M
  void *p = luaL_testudata(L, ud, tname);
353
1.87M
  luaL_argexpected(L, p != NULL, ud, tname);
354
1.87M
  return p;
355
1.87M
}
356
357
/* }====================================================== */
358
359
360
/*
361
** {======================================================
362
** Argument check functions
363
** =======================================================
364
*/
365
366
LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
367
490k
                                 const char *const lst[]) {
368
490k
  const char *name = (def) ? luaL_optstring(L, arg, def) :
369
490k
                             luaL_checkstring(L, arg);
370
490k
  int i;
371
3.09M
  for (i=0; lst[i]; i++)
372
2.99M
    if (strcmp(lst[i], name) == 0)
373
390k
      return i;
374
100k
  return luaL_argerror(L, arg,
375
100k
                       lua_pushfstring(L, "invalid option '%s'", name));
376
490k
}
377
378
379
/*
380
** Ensures the stack has at least 'space' extra slots, raising an error
381
** if it cannot fulfill the request. (The error handling needs a few
382
** extra slots to format the error message. In case of an error without
383
** this extra space, Lua will generate the same 'stack overflow' error,
384
** but without 'msg'.)
385
*/
386
2.31M
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
387
2.31M
  if (l_unlikely(!lua_checkstack(L, space))) {
388
0
    if (msg)
389
0
      luaL_error(L, "stack overflow (%s)", msg);
390
0
    else
391
0
      luaL_error(L, "stack overflow");
392
0
  }
393
2.31M
}
394
395
396
2.91M
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
397
2.91M
  if (l_unlikely(lua_type(L, arg) != t))
398
582k
    tag_error(L, arg, t);
399
2.91M
}
400
401
402
6.32M
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
403
6.32M
  if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
404
277
    luaL_argerror(L, arg, "value expected");
405
6.32M
}
406
407
408
19.7M
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
409
19.7M
  const char *s = lua_tolstring(L, arg, len);
410
19.7M
  if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
411
19.7M
  return s;
412
19.7M
}
413
414
415
LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
416
7.55M
                                        const char *def, size_t *len) {
417
7.55M
  if (lua_isnoneornil(L, arg)) {
418
6.88M
    if (len)
419
4.22k
      *len = (def ? strlen(def) : 0);
420
6.88M
    return def;
421
6.88M
  }
422
675k
  else return luaL_checklstring(L, arg, len);
423
7.55M
}
424
425
426
134k
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
427
134k
  int isnum;
428
134k
  lua_Number d = lua_tonumberx(L, arg, &isnum);
429
134k
  if (l_unlikely(!isnum))
430
21.2k
    tag_error(L, arg, LUA_TNUMBER);
431
134k
  return d;
432
134k
}
433
434
435
8
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
436
8
  return luaL_opt(L, luaL_checknumber, arg, def);
437
8
}
438
439
440
88.7k
static void interror (lua_State *L, int arg) {
441
88.7k
  if (lua_isnumber(L, arg))
442
7.84k
    luaL_argerror(L, arg, "number has no integer representation");
443
80.8k
  else
444
80.8k
    tag_error(L, arg, LUA_TNUMBER);
445
88.7k
}
446
447
448
4.05M
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
449
4.05M
  int isnum;
450
4.05M
  lua_Integer d = lua_tointegerx(L, arg, &isnum);
451
4.05M
  if (l_unlikely(!isnum)) {
452
88.7k
    interror(L, arg);
453
88.7k
  }
454
4.05M
  return d;
455
4.05M
}
456
457
458
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
459
1.44M
                                                      lua_Integer def) {
460
1.44M
  return luaL_opt(L, luaL_checkinteger, arg, def);
461
1.44M
}
462
463
/* }====================================================== */
464
465
466
/*
467
** {======================================================
468
** Generic Buffer manipulation
469
** =======================================================
470
*/
471
472
/* userdata to box arbitrary data */
473
typedef struct UBox {
474
  void *box;
475
  size_t bsize;
476
} UBox;
477
478
479
/* Resize the buffer used by a box. Optimize for the common case of
480
** resizing to the old size. (For instance, __gc will resize the box
481
** to 0 even after it was closed. 'pushresult' may also resize it to a
482
** final size that is equal to the one set when the buffer was created.)
483
*/
484
3.57M
static void *resizebox (lua_State *L, int idx, size_t newsize) {
485
3.57M
  UBox *box = (UBox *)lua_touserdata(L, idx);
486
3.57M
  if (box->bsize == newsize)  /* not changing size? */
487
1.40M
    return box->box;  /* keep the buffer */
488
2.17M
  else {
489
2.17M
    void *ud;
490
2.17M
    lua_Alloc allocf = lua_getallocf(L, &ud);
491
2.17M
    void *temp = allocf(ud, box->box, box->bsize, newsize);
492
2.17M
    if (l_unlikely(temp == NULL && newsize > 0)) {  /* allocation error? */
493
3.17k
      lua_pushliteral(L, "not enough memory");
494
3.17k
      lua_error(L);  /* raise a memory error */
495
3.17k
    }
496
2.17M
    box->box = temp;
497
2.17M
    box->bsize = newsize;
498
2.17M
    return temp;
499
2.17M
  }
500
3.57M
}
501
502
503
1.36M
static int boxgc (lua_State *L) {
504
1.36M
  resizebox(L, 1, 0);
505
1.36M
  return 0;
506
1.36M
}
507
508
509
static const luaL_Reg boxmt[] = {  /* box metamethods */
510
  {"__gc", boxgc},
511
  {"__close", boxgc},
512
  {NULL, NULL}
513
};
514
515
516
684k
static void newbox (lua_State *L) {
517
684k
  UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0);
518
684k
  box->box = NULL;
519
684k
  box->bsize = 0;
520
684k
  if (luaL_newmetatable(L, "_UBOX*"))  /* creating metatable? */
521
2.65k
    luaL_setfuncs(L, boxmt, 0);  /* set its metamethods */
522
684k
  lua_setmetatable(L, -2);
523
684k
}
524
525
526
/*
527
** check whether buffer is using a userdata on the stack as a temporary
528
** buffer
529
*/
530
3.02M
#define buffonstack(B)  ((B)->b != (B)->init.b)
531
532
533
/*
534
** Whenever buffer is accessed, slot 'idx' must either be a box (which
535
** cannot be NULL) or it is a placeholder for the buffer.
536
*/
537
#define checkbufferlevel(B,idx)  \
538
36.2M
  lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL  \
539
36.2M
                            : lua_touserdata(B->L, idx) == (void*)B)
540
541
542
/*
543
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
544
** bytes plus one for a terminating zero.
545
*/
546
1.55M
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
547
1.55M
  size_t newsize = B->size;
548
1.55M
  if (l_unlikely(sz >= MAX_SIZE - B->n))
549
2.16k
    return cast_sizet(luaL_error(B->L, "resulting string too large"));
550
  /* else  B->n + sz + 1 <= MAX_SIZE */
551
1.54M
  if (newsize <= MAX_SIZE/3 * 2)  /* no overflow? */
552
1.54M
    newsize += (newsize >> 1);  /* new size *= 1.5 */
553
1.54M
  if (newsize < B->n + sz + 1)  /* not big enough? */
554
371k
    newsize = B->n + sz + 1;
555
1.54M
  return newsize;
556
1.55M
}
557
558
559
/*
560
** Returns a pointer to a free area with at least 'sz' bytes in buffer
561
** 'B'. 'boxidx' is the relative position in the stack where is the
562
** buffer's box or its placeholder.
563
*/
564
34.7M
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
565
34.7M
  checkbufferlevel(B, boxidx);
566
34.7M
  if (B->size - B->n >= sz)  /* enough space? */
567
33.2M
    return B->b + B->n;
568
1.55M
  else {
569
1.55M
    lua_State *L = B->L;
570
1.55M
    char *newbuff;
571
1.55M
    size_t newsize = newbuffsize(B, sz);
572
    /* create larger buffer */
573
1.55M
    if (buffonstack(B))  /* buffer already has a box? */
574
864k
      newbuff = (char *)resizebox(L, boxidx, newsize);  /* resize it */
575
686k
    else {  /* no box yet */
576
686k
      lua_remove(L, boxidx);  /* remove placeholder */
577
686k
      newbox(L);  /* create a new box */
578
686k
      lua_insert(L, boxidx);  /* move box to its intended position */
579
686k
      lua_toclose(L, boxidx);
580
686k
      newbuff = (char *)resizebox(L, boxidx, newsize);
581
686k
      memcpy(newbuff, B->b, B->n * sizeof(char));  /* copy original content */
582
686k
    }
583
1.55M
    B->b = newbuff;
584
1.55M
    B->size = newsize;
585
1.55M
    return newbuff + B->n;
586
1.55M
  }
587
34.7M
}
588
589
/*
590
** returns a pointer to a free area with at least 'sz' bytes
591
*/
592
676k
LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
593
676k
  return prepbuffsize(B, sz, -1);
594
676k
}
595
596
597
58.3M
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
598
58.3M
  if (l > 0) {  /* avoid 'memcpy' when 's' can be NULL */
599
32.9M
    char *b = prepbuffsize(B, l, -1);
600
32.9M
    memcpy(b, s, l * sizeof(char));
601
32.9M
    luaL_addsize(B, l);
602
32.9M
  }
603
58.3M
}
604
605
606
16.5M
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
607
16.5M
  luaL_addlstring(B, s, strlen(s));
608
16.5M
}
609
610
611
1.47M
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
612
1.47M
  lua_State *L = B->L;
613
1.47M
  checkbufferlevel(B, -1);
614
1.47M
  if (!buffonstack(B))  /* using static buffer? */
615
815k
    lua_pushlstring(L, B->b, B->n);  /* save result as regular string */
616
657k
  else {  /* reuse buffer already allocated */
617
657k
    UBox *box = (UBox *)lua_touserdata(L, -1);
618
657k
    void *ud;
619
657k
    lua_Alloc allocf = lua_getallocf(L, &ud);  /* function to free buffer */
620
657k
    size_t len = B->n;  /* final string length */
621
657k
    char *s;
622
657k
    resizebox(L, -1, len + 1);  /* adjust box size to content size */
623
657k
    s = (char*)box->box;  /* final buffer address */
624
657k
    s[len] = '\0';  /* add ending zero */
625
    /* clear box, as Lua will take control of the buffer */
626
657k
    box->bsize = 0;  box->box = NULL;
627
657k
    lua_pushexternalstring(L, s, len, allocf, ud);
628
657k
    lua_closeslot(L, -2);  /* close the box */
629
657k
    lua_gc(L, LUA_GCSTEP, len);
630
657k
  }
631
1.47M
  lua_remove(L, -2);  /* remove box or placeholder from the stack */
632
1.47M
}
633
634
635
11.2k
LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
636
11.2k
  luaL_addsize(B, sz);
637
11.2k
  luaL_pushresult(B);
638
11.2k
}
639
640
641
/*
642
** 'luaL_addvalue' is the only function in the Buffer system where the
643
** box (if existent) is not on the top of the stack. So, instead of
644
** calling 'luaL_addlstring', it replicates the code using -2 as the
645
** last argument to 'prepbuffsize', signaling that the box is (or will
646
** be) below the string being added to the buffer. (Box creation can
647
** trigger an emergency GC, so we should not remove the string from the
648
** stack before we have the space guaranteed.)
649
*/
650
1.10M
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
651
1.10M
  lua_State *L = B->L;
652
1.10M
  size_t len;
653
1.10M
  const char *s = lua_tolstring(L, -1, &len);
654
1.10M
  char *b = prepbuffsize(B, len, -2);
655
1.10M
  memcpy(b, s, len * sizeof(char));
656
1.10M
  luaL_addsize(B, len);
657
1.10M
  lua_pop(L, 1);  /* pop string */
658
1.10M
}
659
660
661
2.02M
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
662
2.02M
  B->L = L;
663
2.02M
  B->b = B->init.b;
664
2.02M
  B->n = 0;
665
2.02M
  B->size = LUAL_BUFFERSIZE;
666
2.02M
  lua_pushlightuserdata(L, (void*)B);  /* push placeholder */
667
2.02M
}
668
669
670
11.3k
LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
671
11.3k
  luaL_buffinit(L, B);
672
11.3k
  return prepbuffsize(B, sz, -1);
673
11.3k
}
674
675
/* }====================================================== */
676
677
678
/*
679
** {======================================================
680
** Reference system
681
** =======================================================
682
*/
683
684
/*
685
** The previously freed references form a linked list: t[1] is the index
686
** of a first free index, t[t[1]] is the index of the second element,
687
** etc. A zero signals the end of the list.
688
*/
689
12
LUALIB_API int luaL_ref (lua_State *L, int t) {
690
12
  int ref;
691
12
  if (lua_isnil(L, -1)) {
692
1
    lua_pop(L, 1);  /* remove from stack */
693
1
    return LUA_REFNIL;  /* 'nil' has a unique fixed reference */
694
1
  }
695
11
  t = lua_absindex(L, t);
696
11
  if (lua_rawgeti(L, t, 1) == LUA_TNUMBER)  /* already initialized? */
697
0
    ref = (int)lua_tointeger(L, -1);  /* ref = t[1] */
698
11
  else {  /* first access */
699
11
    lua_assert(!lua_toboolean(L, -1));  /* must be nil or false */
700
11
    ref = 0;  /* list is empty */
701
11
    lua_pushinteger(L, 0);  /* initialize as an empty list */
702
11
    lua_rawseti(L, t, 1);  /* ref = t[1] = 0 */
703
11
  }
704
11
  lua_pop(L, 1);  /* remove element from stack */
705
11
  if (ref != 0) {  /* any free element? */
706
0
    lua_rawgeti(L, t, ref);  /* remove it from list */
707
0
    lua_rawseti(L, t, 1);  /* (t[1] = t[ref]) */
708
0
  }
709
11
  else  /* no free elements */
710
11
    ref = (int)lua_rawlen(L, t) + 1;  /* get a new reference */
711
11
  lua_rawseti(L, t, ref);
712
11
  return ref;
713
11
}
714
715
716
0
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
717
0
  if (ref >= 0) {
718
0
    t = lua_absindex(L, t);
719
0
    lua_rawgeti(L, t, 1);
720
0
    lua_assert(lua_isinteger(L, -1));
721
0
    lua_rawseti(L, t, ref);  /* t[ref] = t[1] */
722
0
    lua_pushinteger(L, ref);
723
0
    lua_rawseti(L, t, 1);  /* t[1] = ref */
724
0
  }
725
0
}
726
727
/* }====================================================== */
728
729
730
/*
731
** {======================================================
732
** Load functions
733
** =======================================================
734
*/
735
736
typedef struct LoadF {
737
  unsigned n;  /* number of pre-read characters */
738
  FILE *f;  /* file being read */
739
  char buff[BUFSIZ];  /* area for reading file */
740
} LoadF;
741
742
743
53.3k
static const char *getF (lua_State *L, void *ud, size_t *size) {
744
53.3k
  LoadF *lf = (LoadF *)ud;
745
53.3k
  UNUSED(L);
746
53.3k
  if (lf->n > 0) {  /* are there pre-read characters to be read? */
747
279
    *size = lf->n;  /* return them (chars already in buffer) */
748
279
    lf->n = 0;  /* no more pre-read characters */
749
279
  }
750
53.0k
  else {  /* read a block from file */
751
    /* 'fread' can return > 0 *and* set the EOF flag. If next call to
752
       'getF' called 'fread', it might still wait for user input.
753
       The next check avoids this problem. */
754
53.0k
    if (feof(lf->f)) return NULL;
755
18.5k
    *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);  /* read block */
756
18.5k
  }
757
18.7k
  return lf->buff;
758
53.3k
}
759
760
761
10.9k
static int errfile (lua_State *L, const char *what, int fnameindex) {
762
10.9k
  int err = errno;
763
10.9k
  const char *filename = lua_tostring(L, fnameindex) + 1;
764
10.9k
  if (err != 0)
765
2.77k
    lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err));
766
8.13k
  else
767
8.13k
    lua_pushfstring(L, "cannot %s %s", what, filename);
768
10.9k
  lua_remove(L, fnameindex);
769
10.9k
  return LUA_ERRFILE;
770
10.9k
}
771
772
773
/*
774
** Skip an optional BOM at the start of a stream. If there is an
775
** incomplete BOM (the first character is correct but the rest is
776
** not), returns the first character anyway to force an error
777
** (as no chunk can start with 0xEF).
778
*/
779
42.9k
static int skipBOM (FILE *f) {
780
42.9k
  int c = getc(f);  /* read first character */
781
42.9k
  if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF)  /* correct BOM? */
782
0
    return getc(f);  /* ignore BOM and return next char */
783
42.9k
  else  /* no (valid) BOM */
784
42.9k
    return c;  /* return first character */
785
42.9k
}
786
787
788
/*
789
** reads the first character of file 'f' and skips an optional BOM mark
790
** in its beginning plus its first line if it starts with '#'. Returns
791
** true if it skipped the first line.  In any case, '*cp' has the
792
** first "valid" character of the file (after the optional BOM and
793
** a first-line comment).
794
*/
795
42.9k
static int skipcomment (FILE *f, int *cp) {
796
42.9k
  int c = *cp = skipBOM(f);
797
42.9k
  if (c == '#') {  /* first line is a comment (Unix exec. file)? */
798
0
    do {  /* skip first line */
799
0
      c = getc(f);
800
0
    } while (c != EOF && c != '\n');
801
0
    *cp = getc(f);  /* next character after comment, if present */
802
0
    return 1;  /* there was a comment */
803
0
  }
804
42.9k
  else return 0;  /* no comment */
805
42.9k
}
806
807
808
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
809
45.7k
                                             const char *mode) {
810
45.7k
  LoadF lf;
811
45.7k
  int status, readstatus;
812
45.7k
  int c;
813
45.7k
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
814
45.7k
  if (filename == NULL) {
815
34.4k
    lua_pushliteral(L, "=stdin");
816
34.4k
    lf.f = stdin;
817
34.4k
  }
818
11.2k
  else {
819
11.2k
    lua_pushfstring(L, "@%s", filename);
820
11.2k
    errno = 0;
821
11.2k
    lf.f = fopen(filename, "r");
822
11.2k
    if (lf.f == NULL) return errfile(L, "open", fnameindex);
823
11.2k
  }
824
42.9k
  lf.n = 0;
825
42.9k
  if (skipcomment(lf.f, &c))  /* read initial portion */
826
0
    lf.buff[lf.n++] = '\n';  /* add newline to correct line numbers */
827
42.9k
  if (c == LUA_SIGNATURE[0]) {  /* binary file? */
828
0
    lf.n = 0;  /* remove possible newline */
829
0
    if (filename) {  /* "real" file? */
830
0
      errno = 0;
831
0
      lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
832
0
      if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
833
0
      skipcomment(lf.f, &c);  /* re-read initial portion */
834
0
    }
835
0
  }
836
42.9k
  if (c != EOF)
837
279
    lf.buff[lf.n++] = cast_char(c);  /* 'c' is the first character */
838
42.9k
  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
839
42.9k
  readstatus = ferror(lf.f);
840
42.9k
  errno = 0;  /* no useful error number until here */
841
42.9k
  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
842
42.9k
  if (readstatus) {
843
8.13k
    lua_settop(L, fnameindex);  /* ignore results from 'lua_load' */
844
8.13k
    return errfile(L, "read", fnameindex);
845
8.13k
  }
846
34.7k
  lua_remove(L, fnameindex);
847
34.7k
  return status;
848
42.9k
}
849
850
851
typedef struct LoadS {
852
  const char *s;
853
  size_t size;
854
} LoadS;
855
856
857
1.81M
static const char *getS (lua_State *L, void *ud, size_t *size) {
858
1.81M
  LoadS *ls = (LoadS *)ud;
859
1.81M
  UNUSED(L);
860
1.81M
  if (ls->size == 0) return NULL;
861
1.35M
  *size = ls->size;
862
1.35M
  ls->size = 0;
863
1.35M
  return ls->s;
864
1.81M
}
865
866
867
LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
868
1.39M
                                 const char *name, const char *mode) {
869
1.39M
  LoadS ls;
870
1.39M
  ls.s = buff;
871
1.39M
  ls.size = size;
872
1.39M
  return lua_load(L, getS, &ls, name, mode);
873
1.39M
}
874
875
876
15.5k
LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
877
15.5k
  return luaL_loadbuffer(L, s, strlen(s), s);
878
15.5k
}
879
880
/* }====================================================== */
881
882
883
884
3.62M
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
885
3.62M
  if (!lua_getmetatable(L, obj))  /* no metatable? */
886
581k
    return LUA_TNIL;
887
3.04M
  else {
888
3.04M
    int tt;
889
3.04M
    lua_pushstring(L, event);
890
3.04M
    tt = lua_rawget(L, -2);
891
3.04M
    if (tt == LUA_TNIL)  /* is metafield nil? */
892
3.04M
      lua_pop(L, 2);  /* remove metatable and metafield */
893
1.67k
    else
894
1.67k
      lua_remove(L, -2);  /* remove only metatable */
895
3.04M
    return tt;  /* return metafield type */
896
3.04M
  }
897
3.62M
}
898
899
900
1.67M
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
901
1.67M
  obj = lua_absindex(L, obj);
902
1.67M
  if (luaL_getmetafield(L, obj, event) == LUA_TNIL)  /* no metafield? */
903
1.67M
    return 0;
904
466
  lua_pushvalue(L, obj);
905
466
  lua_call(L, 1, 1);
906
466
  return 1;
907
1.67M
}
908
909
910
50.7k
LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
911
50.7k
  lua_Integer l;
912
50.7k
  int isnum;
913
50.7k
  lua_len(L, idx);
914
50.7k
  l = lua_tointegerx(L, -1, &isnum);
915
50.7k
  if (l_unlikely(!isnum))
916
308
    luaL_error(L, "object length is not an integer");
917
50.7k
  lua_pop(L, 1);  /* remove object */
918
50.7k
  return l;
919
50.7k
}
920
921
922
1.67M
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
923
1.67M
  idx = lua_absindex(L,idx);
924
1.67M
  if (luaL_callmeta(L, idx, "__tostring")) {  /* metafield? */
925
456
    if (!lua_isstring(L, -1))
926
344
      luaL_error(L, "'__tostring' must return a string");
927
456
  }
928
1.67M
  else {
929
1.67M
    switch (lua_type(L, idx)) {
930
556k
      case LUA_TNUMBER: {
931
556k
        char buff[LUA_N2SBUFFSZ];
932
556k
        lua_numbertocstring(L, idx, buff);
933
556k
        lua_pushstring(L, buff);
934
556k
        break;
935
0
      }
936
290k
      case LUA_TSTRING:
937
290k
        lua_pushvalue(L, idx);
938
290k
        break;
939
92.8k
      case LUA_TBOOLEAN:
940
92.8k
        lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
941
92.8k
        break;
942
602k
      case LUA_TNIL:
943
602k
        lua_pushliteral(L, "nil");
944
602k
        break;
945
131k
      default: {
946
131k
        int tt = luaL_getmetafield(L, idx, "__name");  /* try name */
947
131k
        const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
948
131k
                                                 luaL_typename(L, idx);
949
131k
        lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
950
131k
        if (tt != LUA_TNIL)
951
8
          lua_remove(L, -2);  /* remove '__name' */
952
131k
        break;
953
0
      }
954
1.67M
    }
955
1.67M
  }
956
1.67M
  return lua_tolstring(L, -1, len);
957
1.67M
}
958
959
960
/*
961
** set functions from list 'l' into table at top - 'nup'; each
962
** function gets the 'nup' elements at the top as upvalues.
963
** Returns with only the table at the stack.
964
*/
965
99.3k
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
966
99.3k
  luaL_checkstack(L, nup, "too many upvalues");
967
1.11M
  for (; l->name != NULL; l++) {  /* fill the table with given functions */
968
1.02M
    if (l->func == NULL)  /* placeholder? */
969
100k
      lua_pushboolean(L, 0);
970
919k
    else {
971
919k
      int i;
972
938k
      for (i = 0; i < nup; i++)  /* copy upvalues to the top */
973
18.8k
        lua_pushvalue(L, -nup);
974
919k
      lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
975
919k
    }
976
1.02M
    lua_setfield(L, -(nup + 2), l->name);
977
1.02M
  }
978
99.3k
  lua_pop(L, nup);  /* remove upvalues */
979
99.3k
}
980
981
982
/*
983
** ensure that stack[idx][fname] has a table and push that table
984
** into the stack
985
*/
986
266k
LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
987
266k
  if (lua_getfield(L, idx, fname) == LUA_TTABLE)
988
246k
    return 1;  /* table already there */
989
19.5k
  else {
990
19.5k
    lua_pop(L, 1);  /* remove previous result */
991
19.5k
    idx = lua_absindex(L, idx);
992
19.5k
    lua_newtable(L);
993
19.5k
    lua_pushvalue(L, -1);  /* copy to be left at top */
994
19.5k
    lua_setfield(L, idx, fname);  /* assign new table to field */
995
19.5k
    return 0;  /* false, because did not find table there */
996
19.5k
  }
997
266k
}
998
999
1000
/*
1001
** Stripped-down 'require': After checking "loaded" table, calls 'openf'
1002
** to open a module, registers the result in 'package.loaded' table and,
1003
** if 'glb' is true, also registers the result in the global table.
1004
** Leaves resulting module on the top.
1005
*/
1006
LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
1007
62.9k
                               lua_CFunction openf, int glb) {
1008
62.9k
  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
1009
62.9k
  lua_getfield(L, -1, modname);  /* LOADED[modname] */
1010
62.9k
  if (!lua_toboolean(L, -1)) {  /* package not already loaded? */
1011
62.9k
    lua_pop(L, 1);  /* remove field */
1012
62.9k
    lua_pushcfunction(L, openf);
1013
62.9k
    lua_pushstring(L, modname);  /* argument to open function */
1014
62.9k
    lua_call(L, 1, 1);  /* call 'openf' to open module */
1015
62.9k
    lua_pushvalue(L, -1);  /* make copy of module (call result) */
1016
62.9k
    lua_setfield(L, -3, modname);  /* LOADED[modname] = module */
1017
62.9k
  }
1018
62.9k
  lua_remove(L, -2);  /* remove LOADED table */
1019
62.9k
  if (glb) {
1020
62.9k
    lua_pushvalue(L, -1);  /* copy of module */
1021
62.9k
    lua_setglobal(L, modname);  /* _G[modname] = module */
1022
62.9k
  }
1023
62.9k
}
1024
1025
1026
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
1027
908k
                                     const char *p, const char *r) {
1028
908k
  const char *wild;
1029
908k
  size_t l = strlen(p);
1030
12.3M
  while ((wild = strstr(s, p)) != NULL) {
1031
11.4M
    luaL_addlstring(b, s, ct_diff2sz(wild - s));  /* push prefix */
1032
11.4M
    luaL_addstring(b, r);  /* push replacement in place of pattern */
1033
11.4M
    s = wild + l;  /* continue after 'p' */
1034
11.4M
  }
1035
908k
  luaL_addstring(b, s);  /* push last suffix */
1036
908k
}
1037
1038
1039
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
1040
123k
                                  const char *p, const char *r) {
1041
123k
  luaL_Buffer b;
1042
123k
  luaL_buffinit(L, &b);
1043
123k
  luaL_addgsub(&b, s, p, r);
1044
123k
  luaL_pushresult(&b);
1045
123k
  return lua_tostring(L, -1);
1046
123k
}
1047
1048
1049
208M
void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
1050
208M
  UNUSED(ud); UNUSED(osize);
1051
208M
  if (nsize == 0) {
1052
111M
    free(ptr);
1053
111M
    return NULL;
1054
111M
  }
1055
96.9M
  else
1056
96.9M
    return realloc(ptr, nsize);
1057
208M
}
1058
1059
1060
/*
1061
** Standard panic function just prints an error message. The test
1062
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
1063
*/
1064
0
static int panic (lua_State *L) {
1065
0
  const char *msg = (lua_type(L, -1) == LUA_TSTRING)
1066
0
                  ? lua_tostring(L, -1)
1067
0
                  : "error object is not a string";
1068
0
  lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
1069
0
                        msg);
1070
0
  return 0;  /* return to Lua to abort */
1071
0
}
1072
1073
1074
/*
1075
** Warning functions:
1076
** warnfoff: warning system is off
1077
** warnfon: ready to start a new message
1078
** warnfcont: previous message is to be continued
1079
*/
1080
static void warnfoff (void *ud, const char *message, int tocont);
1081
static void warnfon (void *ud, const char *message, int tocont);
1082
static void warnfcont (void *ud, const char *message, int tocont);
1083
1084
1085
/*
1086
** Check whether message is a control message. If so, execute the
1087
** control or ignore it if unknown.
1088
*/
1089
79.0k
static int checkcontrol (lua_State *L, const char *message, int tocont) {
1090
79.0k
  if (tocont || *(message++) != '@')  /* not a control message? */
1091
39.3k
    return 0;
1092
39.7k
  else {
1093
39.7k
    if (strcmp(message, "off") == 0)
1094
306
      lua_setwarnf(L, warnfoff, L);  /* turn warnings off */
1095
39.4k
    else if (strcmp(message, "on") == 0)
1096
3.43k
      lua_setwarnf(L, warnfon, L);   /* turn warnings on */
1097
39.7k
    return 1;  /* it was a control message */
1098
39.7k
  }
1099
79.0k
}
1100
1101
1102
1.06k
static void warnfoff (void *ud, const char *message, int tocont) {
1103
1.06k
  checkcontrol((lua_State *)ud, message, tocont);
1104
1.06k
}
1105
1106
1107
/*
1108
** Writes the message and handle 'tocont', finishing the message
1109
** if needed and setting the next warn function.
1110
*/
1111
41.7k
static void warnfcont (void *ud, const char *message, int tocont) {
1112
41.7k
  lua_State *L = (lua_State *)ud;
1113
41.7k
  lua_writestringerror("%s", message);  /* write message */
1114
41.7k
  if (tocont)  /* not the last part? */
1115
3.17k
    lua_setwarnf(L, warnfcont, L);  /* to be continued */
1116
38.5k
  else {  /* last part */
1117
38.5k
    lua_writestringerror("%s", "\n");  /* finish message with end-of-line */
1118
38.5k
    lua_setwarnf(L, warnfon, L);  /* next call is a new message */
1119
38.5k
  }
1120
41.7k
}
1121
1122
1123
78.0k
static void warnfon (void *ud, const char *message, int tocont) {
1124
78.0k
  if (checkcontrol((lua_State *)ud, message, tocont))  /* control message? */
1125
39.4k
    return;  /* nothing else to be done */
1126
38.5k
  lua_writestringerror("%s", "Lua warning: ");  /* start a new warning */
1127
38.5k
  warnfcont(ud, message, tocont);  /* finish processing */
1128
38.5k
}
1129
1130
1131
1132
/*
1133
** A function to compute an unsigned int with some level of
1134
** randomness. Rely on Address Space Layout Randomization (if present)
1135
** and the current time.
1136
*/
1137
#if !defined(luai_makeseed)
1138
1139
#include <time.h>
1140
1141
1142
/* Size for the buffer, in bytes */
1143
231k
#define BUFSEEDB  (sizeof(void*) + sizeof(time_t))
1144
1145
/* Size for the buffer in int's, rounded up */
1146
185k
#define BUFSEED   ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))
1147
1148
/*
1149
** Copy the contents of variable 'v' into the buffer pointed by 'b'.
1150
** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.)
1151
*/
1152
92.5k
#define addbuff(b,v)  (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))
1153
1154
1155
46.2k
static unsigned int luai_makeseed (void) {
1156
46.2k
  unsigned int buff[BUFSEED];
1157
46.2k
  unsigned int res;
1158
46.2k
  unsigned int i;
1159
46.2k
  time_t t = time(NULL);
1160
46.2k
  char *b = (char*)buff;
1161
46.2k
  addbuff(b, b);  /* local variable's address */
1162
46.2k
  addbuff(b, t);  /* time */
1163
  /* fill (rare but possible) remain of the buffer with zeros */
1164
46.2k
  memset(b, 0, sizeof(buff) - BUFSEEDB);
1165
46.2k
  res = buff[0];
1166
185k
  for (i = 1; i < BUFSEED; i++)
1167
138k
    res ^= (res >> 3) + (res << 7) + buff[i];
1168
46.2k
  return res;
1169
46.2k
}
1170
1171
#endif
1172
1173
1174
46.2k
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
1175
46.2k
  UNUSED(L);
1176
46.2k
  return luai_makeseed();
1177
46.2k
}
1178
1179
1180
/*
1181
** Use the name with parentheses so that headers can redefine it
1182
** as a macro.
1183
*/
1184
40.0k
LUALIB_API lua_State *(luaL_newstate) (void) {
1185
40.0k
  lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL));
1186
40.0k
  if (l_likely(L)) {
1187
40.0k
    lua_atpanic(L, &panic);
1188
40.0k
    lua_setwarnf(L, warnfon, L);
1189
40.0k
  }
1190
40.0k
  return L;
1191
40.0k
}
1192
1193
1194
56.6k
LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
1195
56.6k
  lua_Number v = lua_version(L);
1196
56.6k
  if (sz != LUAL_NUMSIZES)  /* check numeric types */
1197
0
    luaL_error(L, "core and library have incompatible numeric types");
1198
56.6k
  else if (v != ver)
1199
0
    luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
1200
0
                  (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);
1201
56.6k
}
1202