Coverage Report

Created: 2025-06-16 06:56

/src/testdir/build/lua-master/source/lauxlib.c
Line
Count
Source (jump to first uncovered line)
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
226k
#define LEVELS1 10  /* size of the first part of the stack */
39
226k
#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
165M
static int findfield (lua_State *L, int objidx, int level) {
48
165M
  if (level == 0 || !lua_istable(L, -1))
49
153M
    return 0;  /* not found */
50
12.2M
  lua_pushnil(L);  /* start 'next' loop */
51
229M
  while (lua_next(L, -2)) {  /* for each pair in table */
52
220M
    if (lua_type(L, -2) == LUA_TSTRING) {  /* ignore non-string keys */
53
165M
      if (lua_rawequal(L, objidx, -1)) {  /* found object? */
54
1.52M
        lua_pop(L, 1);  /* remove value (but keep name) */
55
1.52M
        return 1;
56
1.52M
      }
57
164M
      else if (findfield(L, objidx, level - 1)) {  /* try recursively */
58
        /* stack: lib_name, lib_table, field_name (top) */
59
1.52M
        lua_pushliteral(L, ".");  /* place '.' between the two names */
60
1.52M
        lua_replace(L, -3);  /* (in the slot occupied by table) */
61
1.52M
        lua_concat(L, 3);  /* lib_name.field_name */
62
1.52M
        return 1;
63
1.52M
      }
64
165M
    }
65
217M
    lua_pop(L, 1);  /* remove value */
66
217M
  }
67
9.24M
  return 0;  /* not found */
68
12.2M
}
69
70
71
/*
72
** Search for a name for a function in all loaded modules
73
*/
74
1.79M
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
75
1.79M
  int top = lua_gettop(L);
76
1.79M
  lua_getinfo(L, "f", ar);  /* push function */
77
1.79M
  lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
78
1.79M
  luaL_checkstack(L, 6, "not enough stack");  /* slots for 'findfield' */
79
1.79M
  if (findfield(L, top + 1, 2)) {
80
1.52M
    const char *name = lua_tostring(L, -1);
81
1.52M
    if (strncmp(name, LUA_GNAME ".", 3) == 0) {  /* name start with '_G.'? */
82
1.51M
      lua_pushstring(L, name + 3);  /* push name without prefix */
83
1.51M
      lua_remove(L, -2);  /* remove original name */
84
1.51M
    }
85
1.52M
    lua_copy(L, -1, top + 1);  /* copy name to proper place */
86
1.52M
    lua_settop(L, top + 1);  /* remove table "loaded" and name copy */
87
1.52M
    return 1;
88
1.52M
  }
89
269k
  else {
90
269k
    lua_settop(L, top);  /* remove function and global table */
91
269k
    return 0;
92
269k
  }
93
1.79M
}
94
95
96
1.10M
static void pushfuncname (lua_State *L, lua_Debug *ar) {
97
1.10M
  if (*ar->namewhat != '\0')  /* is there a name from code? */
98
654k
    lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name);  /* use it */
99
445k
  else if (*ar->what == 'm')  /* main? */
100
192k
      lua_pushliteral(L, "main chunk");
101
252k
  else if (pushglobalfuncname(L, ar)) {  /* try a global name */
102
1.37k
    lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
103
1.37k
    lua_remove(L, -2);  /* remove name */
104
1.37k
  }
105
251k
  else if (*ar->what != 'C')  /* for Lua functions, use <file:line> */
106
250k
    lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
107
327
  else  /* nothing left... */
108
327
    lua_pushliteral(L, "?");
109
1.10M
}
110
111
112
192k
static int lastlevel (lua_State *L) {
113
192k
  lua_Debug ar;
114
192k
  int li = 1, le = 1;
115
  /* find an upper bound */
116
681k
  while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
117
  /* do a binary search */
118
627k
  while (li < le) {
119
434k
    int m = (li + le)/2;
120
434k
    if (lua_getstack(L, m, &ar)) li = m + 1;
121
151k
    else le = m;
122
434k
  }
123
192k
  return le - 1;
124
192k
}
125
126
127
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
128
192k
                                const char *msg, int level) {
129
192k
  luaL_Buffer b;
130
192k
  lua_Debug ar;
131
192k
  int last = lastlevel(L1);
132
192k
  int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
133
192k
  luaL_buffinit(L, &b);
134
192k
  if (msg) {
135
192k
    luaL_addstring(&b, msg);
136
192k
    luaL_addchar(&b, '\n');
137
192k
  }
138
192k
  luaL_addstring(&b, "stack traceback:");
139
1.32M
  while (lua_getstack(L1, level++, &ar)) {
140
1.13M
    if (limit2show-- == 0) {  /* too many levels? */
141
33.8k
      int n = last - level - LEVELS2 + 1;  /* number of levels to skip */
142
33.8k
      lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n);
143
33.8k
      luaL_addvalue(&b);  /* add warning about skip */
144
33.8k
      level += n;  /* and skip to last levels */
145
33.8k
    }
146
1.10M
    else {
147
1.10M
      lua_getinfo(L1, "Slnt", &ar);
148
1.10M
      if (ar.currentline <= 0)
149
198k
        lua_pushfstring(L, "\n\t%s: in ", ar.short_src);
150
901k
      else
151
901k
        lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline);
152
1.10M
      luaL_addvalue(&b);
153
1.10M
      pushfuncname(L, &ar);
154
1.10M
      luaL_addvalue(&b);
155
1.10M
      if (ar.istailcall)
156
70.9k
        luaL_addstring(&b, "\n\t(...tail calls...)");
157
1.10M
    }
158
1.13M
  }
159
192k
  luaL_pushresult(&b);
160
192k
}
161
162
/* }====================================================== */
163
164
165
/*
166
** {======================================================
167
** Error-report functions
168
** =======================================================
169
*/
170
171
1.76M
LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
172
1.76M
  lua_Debug ar;
173
1.76M
  const char *argword;
174
1.76M
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
175
0
    return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
176
1.76M
  lua_getinfo(L, "nt", &ar);
177
1.76M
  if (arg <= ar.extraargs)  /* error in an extra argument? */
178
0
    argword =  "extra argument";
179
1.76M
  else {
180
1.76M
    arg -= ar.extraargs;  /* do not count extra arguments */
181
1.76M
    if (strcmp(ar.namewhat, "method") == 0) {  /* colon syntax? */
182
58.4k
      arg--;  /* do not count (extra) self argument */
183
58.4k
      if (arg == 0)  /* error in self argument? */
184
5.20k
        return luaL_error(L, "calling '%s' on bad self (%s)",
185
5.20k
                               ar.name, extramsg);
186
      /* else go through; error in a regular argument */
187
58.4k
    }
188
1.75M
    argword = "argument";
189
1.75M
  }
190
1.75M
  if (ar.name == NULL)
191
1.53M
    ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
192
1.75M
  return luaL_error(L, "bad %s #%d to '%s' (%s)",
193
1.75M
                       argword, arg, ar.name, extramsg);
194
1.76M
}
195
196
197
1.42M
LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
198
1.42M
  const char *msg;
199
1.42M
  const char *typearg;  /* name for the type of the actual argument */
200
1.42M
  if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
201
1
    typearg = lua_tostring(L, -1);  /* use the given type name */
202
1.42M
  else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
203
0
    typearg = "light userdata";  /* special name for messages */
204
1.42M
  else
205
1.42M
    typearg = luaL_typename(L, arg);  /* standard name */
206
1.42M
  msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
207
1.42M
  return luaL_argerror(L, arg, msg);
208
1.42M
}
209
210
211
1.40M
static void tag_error (lua_State *L, int arg, int tag) {
212
1.40M
  luaL_typeerror(L, arg, lua_typename(L, tag));
213
1.40M
}
214
215
216
/*
217
** The use of 'lua_pushfstring' ensures this function does not
218
** need reserved stack space when called.
219
*/
220
2.12M
LUALIB_API void luaL_where (lua_State *L, int level) {
221
2.12M
  lua_Debug ar;
222
2.12M
  if (lua_getstack(L, level, &ar)) {  /* check function at level */
223
2.12M
    lua_getinfo(L, "Sl", &ar);  /* get info about it */
224
2.12M
    if (ar.currentline > 0) {  /* is there info? */
225
541k
      lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
226
541k
      return;
227
541k
    }
228
2.12M
  }
229
1.58M
  lua_pushfstring(L, "");  /* else, no information available... */
230
1.58M
}
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
2.08M
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
239
2.08M
  va_list argp;
240
2.08M
  va_start(argp, fmt);
241
2.08M
  luaL_where(L, 1);
242
2.08M
  lua_pushvfstring(L, fmt, argp);
243
2.08M
  va_end(argp);
244
2.08M
  lua_concat(L, 2);
245
2.08M
  return lua_error(L);
246
2.08M
}
247
248
249
2.74M
LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
250
2.74M
  int en = errno;  /* calls to Lua API may change this value */
251
2.74M
  if (stat) {
252
1.91M
    lua_pushboolean(L, 1);
253
1.91M
    return 1;
254
1.91M
  }
255
831k
  else {
256
831k
    const char *msg;
257
831k
    luaL_pushfail(L);
258
831k
    msg = (en != 0) ? strerror(en) : "(no extra info)";
259
831k
    if (fname)
260
818k
      lua_pushfstring(L, "%s: %s", fname, msg);
261
12.6k
    else
262
12.6k
      lua_pushstring(L, msg);
263
831k
    lua_pushinteger(L, en);
264
831k
    return 3;
265
831k
  }
266
2.74M
}
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
501
   if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
280
501
   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
501
LUALIB_API int luaL_execresult (lua_State *L, int stat) {
292
501
  if (stat != 0 && errno != 0)  /* error with an 'errno'? */
293
0
    return luaL_fileresult(L, 0, NULL);
294
501
  else {
295
501
    const char *what = "exit";  /* type of termination */
296
501
    l_inspectstat(stat, what);  /* interpret result */
297
501
    if (*what == 'e' && stat == 0)  /* successful termination? */
298
131
      lua_pushboolean(L, 1);
299
370
    else
300
370
      luaL_pushfail(L);
301
501
    lua_pushstring(L, what);
302
501
    lua_pushinteger(L, stat);
303
501
    return 3;  /* return true/fail,what,code */
304
501
  }
305
501
}
306
307
/* }====================================================== */
308
309
310
311
/*
312
** {======================================================
313
** Userdata's metatable manipulation
314
** =======================================================
315
*/
316
317
898k
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
318
898k
  if (luaL_getmetatable(L, tname) != LUA_TNIL)  /* name already in use? */
319
850k
    return 0;  /* leave previous value on top, but return 0 */
320
48.1k
  lua_pop(L, 1);
321
48.1k
  lua_createtable(L, 0, 2);  /* create metatable */
322
48.1k
  lua_pushstring(L, tname);
323
48.1k
  lua_setfield(L, -2, "__name");  /* metatable.__name = tname */
324
48.1k
  lua_pushvalue(L, -1);
325
48.1k
  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
326
48.1k
  return 1;
327
898k
}
328
329
330
2.81M
LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
331
2.81M
  luaL_getmetatable(L, tname);
332
2.81M
  lua_setmetatable(L, -2);
333
2.81M
}
334
335
336
8.38M
LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
337
8.38M
  void *p = lua_touserdata(L, ud);
338
8.38M
  if (p != NULL) {  /* value is a userdata? */
339
7.79M
    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */
340
7.79M
      luaL_getmetatable(L, tname);  /* get correct metatable */
341
7.79M
      if (!lua_rawequal(L, -1, -2))  /* not the same? */
342
4.08k
        p = NULL;  /* value is a userdata with wrong metatable */
343
7.79M
      lua_pop(L, 2);  /* remove both metatables */
344
7.79M
      return p;
345
7.79M
    }
346
7.79M
  }
347
591k
  return NULL;  /* value is not a userdata with a metatable */
348
8.38M
}
349
350
351
7.67M
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
352
7.67M
  void *p = luaL_testudata(L, ud, tname);
353
7.67M
  luaL_argexpected(L, p != NULL, ud, tname);
354
7.67M
  return p;
355
7.67M
}
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
1.24M
                                 const char *const lst[]) {
368
1.24M
  const char *name = (def) ? luaL_optstring(L, arg, def) :
369
1.24M
                             luaL_checkstring(L, arg);
370
1.24M
  int i;
371
8.07M
  for (i=0; lst[i]; i++)
372
7.85M
    if (strcmp(lst[i], name) == 0)
373
1.02M
      return i;
374
220k
  return luaL_argerror(L, arg,
375
220k
                       lua_pushfstring(L, "invalid option '%s'", name));
376
1.24M
}
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
12.6M
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
387
12.6M
  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
12.6M
}
394
395
396
8.18M
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
397
8.18M
  if (l_unlikely(lua_type(L, arg) != t))
398
1.00M
    tag_error(L, arg, t);
399
8.18M
}
400
401
402
47.1M
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
403
47.1M
  if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
404
3.47k
    luaL_argerror(L, arg, "value expected");
405
47.1M
}
406
407
408
91.7M
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
409
91.7M
  const char *s = lua_tolstring(L, arg, len);
410
91.7M
  if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
411
91.7M
  return s;
412
91.7M
}
413
414
415
LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
416
25.7M
                                        const char *def, size_t *len) {
417
25.7M
  if (lua_isnoneornil(L, arg)) {
418
23.0M
    if (len)
419
40.1k
      *len = (def ? strlen(def) : 0);
420
23.0M
    return def;
421
23.0M
  }
422
2.68M
  else return luaL_checklstring(L, arg, len);
423
25.7M
}
424
425
426
2.54M
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
427
2.54M
  int isnum;
428
2.54M
  lua_Number d = lua_tonumberx(L, arg, &isnum);
429
2.54M
  if (l_unlikely(!isnum))
430
16.9k
    tag_error(L, arg, LUA_TNUMBER);
431
2.54M
  return d;
432
2.54M
}
433
434
435
109
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
436
109
  return luaL_opt(L, luaL_checknumber, arg, def);
437
109
}
438
439
440
410k
static void interror (lua_State *L, int arg) {
441
410k
  if (lua_isnumber(L, arg))
442
29.8k
    luaL_argerror(L, arg, "number has no integer representation");
443
380k
  else
444
380k
    tag_error(L, arg, LUA_TNUMBER);
445
410k
}
446
447
448
28.8M
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
449
28.8M
  int isnum;
450
28.8M
  lua_Integer d = lua_tointegerx(L, arg, &isnum);
451
28.8M
  if (l_unlikely(!isnum)) {
452
410k
    interror(L, arg);
453
410k
  }
454
28.8M
  return d;
455
28.8M
}
456
457
458
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
459
21.0M
                                                      lua_Integer def) {
460
21.0M
  return luaL_opt(L, luaL_checkinteger, arg, def);
461
21.0M
}
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
4.19M
static void *resizebox (lua_State *L, int idx, size_t newsize) {
485
4.19M
  UBox *box = (UBox *)lua_touserdata(L, idx);
486
4.19M
  if (box->bsize == newsize)  /* not changing size? */
487
1.84M
    return box->box;  /* keep the buffer */
488
2.34M
  else {
489
2.34M
    void *ud;
490
2.34M
    lua_Alloc allocf = lua_getallocf(L, &ud);
491
2.34M
    void *temp = allocf(ud, box->box, box->bsize, newsize);
492
2.34M
    if (l_unlikely(temp == NULL && newsize > 0)) {  /* allocation error? */
493
1.59k
      lua_pushliteral(L, "not enough memory");
494
1.59k
      lua_error(L);  /* raise a memory error */
495
1.59k
    }
496
2.34M
    box->box = temp;
497
2.34M
    box->bsize = newsize;
498
2.34M
    return temp;
499
2.34M
  }
500
4.19M
}
501
502
503
1.72M
static int boxgc (lua_State *L) {
504
1.72M
  resizebox(L, 1, 0);
505
1.72M
  return 0;
506
1.72M
}
507
508
509
static const luaL_Reg boxmt[] = {  /* box metamethods */
510
  {"__gc", boxgc},
511
  {"__close", boxgc},
512
  {NULL, NULL}
513
};
514
515
516
867k
static void newbox (lua_State *L) {
517
867k
  UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0);
518
867k
  box->box = NULL;
519
867k
  box->bsize = 0;
520
867k
  if (luaL_newmetatable(L, "_UBOX*"))  /* creating metatable? */
521
16.7k
    luaL_setfuncs(L, boxmt, 0);  /* set its metamethods */
522
867k
  lua_setmetatable(L, -2);
523
867k
}
524
525
526
/*
527
** check whether buffer is using a userdata on the stack as a temporary
528
** buffer
529
*/
530
12.9M
#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
88.1M
  lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL  \
539
88.1M
                            : 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.60M
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
547
1.60M
  size_t newsize = B->size;
548
1.60M
  if (l_unlikely(sz >= MAX_SIZE - B->n))
549
534
    return cast_sizet(luaL_error(B->L, "resulting string too large"));
550
  /* else  B->n + sz + 1 <= MAX_SIZE */
551
1.60M
  if (newsize <= MAX_SIZE/3 * 2)  /* no overflow? */
552
1.60M
    newsize += (newsize >> 1);  /* new size *= 1.5 */
553
1.60M
  if (newsize < B->n + sz + 1)  /* not big enough? */
554
382k
    newsize = B->n + sz + 1;
555
1.60M
  return newsize;
556
1.60M
}
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
76.8M
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
565
76.8M
  checkbufferlevel(B, boxidx);
566
76.8M
  if (B->size - B->n >= sz)  /* enough space? */
567
75.2M
    return B->b + B->n;
568
1.60M
  else {
569
1.60M
    lua_State *L = B->L;
570
1.60M
    char *newbuff;
571
1.60M
    size_t newsize = newbuffsize(B, sz);
572
    /* create larger buffer */
573
1.60M
    if (buffonstack(B))  /* buffer already has a box? */
574
741k
      newbuff = (char *)resizebox(L, boxidx, newsize);  /* resize it */
575
867k
    else {  /* no box yet */
576
867k
      lua_remove(L, boxidx);  /* remove placeholder */
577
867k
      newbox(L);  /* create a new box */
578
867k
      lua_insert(L, boxidx);  /* move box to its intended position */
579
867k
      lua_toclose(L, boxidx);
580
867k
      newbuff = (char *)resizebox(L, boxidx, newsize);
581
867k
      memcpy(newbuff, B->b, B->n * sizeof(char));  /* copy original content */
582
867k
    }
583
1.60M
    B->b = newbuff;
584
1.60M
    B->size = newsize;
585
1.60M
    return newbuff + B->n;
586
1.60M
  }
587
76.8M
}
588
589
/*
590
** returns a pointer to a free area with at least 'sz' bytes
591
*/
592
3.45M
LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
593
3.45M
  return prepbuffsize(B, sz, -1);
594
3.45M
}
595
596
597
144M
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
598
144M
  if (l > 0) {  /* avoid 'memcpy' when 's' can be NULL */
599
56.4M
    char *b = prepbuffsize(B, l, -1);
600
56.4M
    memcpy(b, s, l * sizeof(char));
601
56.4M
    luaL_addsize(B, l);
602
56.4M
  }
603
144M
}
604
605
606
13.2M
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
607
13.2M
  luaL_addlstring(B, s, strlen(s));
608
13.2M
}
609
610
611
11.3M
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
612
11.3M
  lua_State *L = B->L;
613
11.3M
  checkbufferlevel(B, -1);
614
11.3M
  if (!buffonstack(B))  /* using static buffer? */
615
10.4M
    lua_pushlstring(L, B->b, B->n);  /* save result as regular string */
616
852k
  else {  /* reuse buffer already allocated */
617
852k
    UBox *box = (UBox *)lua_touserdata(L, -1);
618
852k
    void *ud;
619
852k
    lua_Alloc allocf = lua_getallocf(L, &ud);  /* function to free buffer */
620
852k
    size_t len = B->n;  /* final string length */
621
852k
    char *s;
622
852k
    resizebox(L, -1, len + 1);  /* adjust box size to content size */
623
852k
    s = (char*)box->box;  /* final buffer address */
624
852k
    s[len] = '\0';  /* add ending zero */
625
    /* clear box, as Lua will take control of the buffer */
626
852k
    box->bsize = 0;  box->box = NULL;
627
852k
    lua_pushexternalstring(L, s, len, allocf, ud);
628
852k
    lua_closeslot(L, -2);  /* close the box */
629
852k
    lua_gc(L, LUA_GCSTEP, len);
630
852k
  }
631
11.3M
  lua_remove(L, -2);  /* remove box or placeholder from the stack */
632
11.3M
}
633
634
635
4.28M
LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
636
4.28M
  luaL_addsize(B, sz);
637
4.28M
  luaL_pushresult(B);
638
4.28M
}
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
12.5M
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
651
12.5M
  lua_State *L = B->L;
652
12.5M
  size_t len;
653
12.5M
  const char *s = lua_tolstring(L, -1, &len);
654
12.5M
  char *b = prepbuffsize(B, len, -2);
655
12.5M
  memcpy(b, s, len * sizeof(char));
656
12.5M
  luaL_addsize(B, len);
657
12.5M
  lua_pop(L, 1);  /* pop string */
658
12.5M
}
659
660
661
13.5M
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
662
13.5M
  B->L = L;
663
13.5M
  B->b = B->init.b;
664
13.5M
  B->n = 0;
665
13.5M
  B->size = LUAL_BUFFERSIZE;
666
13.5M
  lua_pushlightuserdata(L, (void*)B);  /* push placeholder */
667
13.5M
}
668
669
670
4.28M
LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
671
4.28M
  luaL_buffinit(L, B);
672
4.28M
  return prepbuffsize(B, sz, -1);
673
4.28M
}
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
19
LUALIB_API int luaL_ref (lua_State *L, int t) {
690
19
  int ref;
691
19
  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
18
  t = lua_absindex(L, t);
696
18
  if (lua_rawgeti(L, t, 1) == LUA_TNUMBER)  /* already initialized? */
697
0
    ref = (int)lua_tointeger(L, -1);  /* ref = t[1] */
698
18
  else {  /* first access */
699
18
    lua_assert(!lua_toboolean(L, -1));  /* must be nil or false */
700
18
    ref = 0;  /* list is empty */
701
18
    lua_pushinteger(L, 0);  /* initialize as an empty list */
702
18
    lua_rawseti(L, t, 1);  /* ref = t[1] = 0 */
703
18
  }
704
18
  lua_pop(L, 1);  /* remove element from stack */
705
18
  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
18
  else  /* no free elements */
710
18
    ref = (int)lua_rawlen(L, t) + 1;  /* get a new reference */
711
18
  lua_rawseti(L, t, ref);
712
18
  return ref;
713
18
}
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
62.5k
static const char *getF (lua_State *L, void *ud, size_t *size) {
744
62.5k
  LoadF *lf = (LoadF *)ud;
745
62.5k
  (void)L;  /* not used */
746
62.5k
  if (lf->n > 0) {  /* are there pre-read characters to be read? */
747
399
    *size = lf->n;  /* return them (chars already in buffer) */
748
399
    lf->n = 0;  /* no more pre-read characters */
749
399
  }
750
62.1k
  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
62.1k
    if (feof(lf->f)) return NULL;
755
29.4k
    *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);  /* read block */
756
29.4k
  }
757
29.8k
  return lf->buff;
758
62.5k
}
759
760
761
84.3k
static int errfile (lua_State *L, const char *what, int fnameindex) {
762
84.3k
  int err = errno;
763
84.3k
  const char *filename = lua_tostring(L, fnameindex) + 1;
764
84.3k
  if (err != 0)
765
81.6k
    lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err));
766
2.63k
  else
767
2.63k
    lua_pushfstring(L, "cannot %s %s", what, filename);
768
84.3k
  lua_remove(L, fnameindex);
769
84.3k
  return LUA_ERRFILE;
770
84.3k
}
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
35.6k
static int skipBOM (FILE *f) {
780
35.6k
  int c = getc(f);  /* read first character */
781
35.6k
  if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF)  /* correct BOM? */
782
0
    return getc(f);  /* ignore BOM and return next char */
783
35.6k
  else  /* no (valid) BOM */
784
35.6k
    return c;  /* return first character */
785
35.6k
}
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
35.6k
static int skipcomment (FILE *f, int *cp) {
796
35.6k
  int c = *cp = skipBOM(f);
797
35.6k
  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
35.6k
  else return 0;  /* no comment */
805
35.6k
}
806
807
808
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
809
117k
                                             const char *mode) {
810
117k
  LoadF lf;
811
117k
  int status, readstatus;
812
117k
  int c;
813
117k
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
814
117k
  if (filename == NULL) {
815
32.5k
    lua_pushliteral(L, "=stdin");
816
32.5k
    lf.f = stdin;
817
32.5k
  }
818
84.8k
  else {
819
84.8k
    lua_pushfstring(L, "@%s", filename);
820
84.8k
    errno = 0;
821
84.8k
    lf.f = fopen(filename, "r");
822
84.8k
    if (lf.f == NULL) return errfile(L, "open", fnameindex);
823
84.8k
  }
824
35.6k
  lf.n = 0;
825
35.6k
  if (skipcomment(lf.f, &c))  /* read initial portion */
826
0
    lf.buff[lf.n++] = '\n';  /* add newline to correct line numbers */
827
35.6k
  if (c == LUA_SIGNATURE[0]) {  /* binary file? */
828
1
    lf.n = 0;  /* remove possible newline */
829
1
    if (filename) {  /* "real" file? */
830
1
      errno = 0;
831
1
      lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
832
1
      if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
833
1
      skipcomment(lf.f, &c);  /* re-read initial portion */
834
1
    }
835
1
  }
836
35.6k
  if (c != EOF)
837
399
    lf.buff[lf.n++] = cast_char(c);  /* 'c' is the first character */
838
35.6k
  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
839
35.6k
  readstatus = ferror(lf.f);
840
35.6k
  errno = 0;  /* no useful error number until here */
841
35.6k
  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
842
35.6k
  if (readstatus) {
843
2.63k
    lua_settop(L, fnameindex);  /* ignore results from 'lua_load' */
844
2.63k
    return errfile(L, "read", fnameindex);
845
2.63k
  }
846
33.0k
  lua_remove(L, fnameindex);
847
33.0k
  return status;
848
35.6k
}
849
850
851
typedef struct LoadS {
852
  const char *s;
853
  size_t size;
854
} LoadS;
855
856
857
8.96M
static const char *getS (lua_State *L, void *ud, size_t *size) {
858
8.96M
  LoadS *ls = (LoadS *)ud;
859
8.96M
  (void)L;  /* not used */
860
8.96M
  if (ls->size == 0) return NULL;
861
5.92M
  *size = ls->size;
862
5.92M
  ls->size = 0;
863
5.92M
  return ls->s;
864
8.96M
}
865
866
867
LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
868
6.07M
                                 const char *name, const char *mode) {
869
6.07M
  LoadS ls;
870
6.07M
  ls.s = buff;
871
6.07M
  ls.size = size;
872
6.07M
  return lua_load(L, getS, &ls, name, mode);
873
6.07M
}
874
875
876
34.1k
LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
877
34.1k
  return luaL_loadbuffer(L, s, strlen(s), s);
878
34.1k
}
879
880
/* }====================================================== */
881
882
883
884
5.84M
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
885
5.84M
  if (!lua_getmetatable(L, obj))  /* no metatable? */
886
2.00M
    return LUA_TNIL;
887
3.84M
  else {
888
3.84M
    int tt;
889
3.84M
    lua_pushstring(L, event);
890
3.84M
    tt = lua_rawget(L, -2);
891
3.84M
    if (tt == LUA_TNIL)  /* is metafield nil? */
892
3.83M
      lua_pop(L, 2);  /* remove metatable and metafield */
893
7.71k
    else
894
7.71k
      lua_remove(L, -2);  /* remove only metatable */
895
3.84M
    return tt;  /* return metafield type */
896
3.84M
  }
897
5.84M
}
898
899
900
1.77M
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
901
1.77M
  obj = lua_absindex(L, obj);
902
1.77M
  if (luaL_getmetafield(L, obj, event) == LUA_TNIL)  /* no metafield? */
903
1.76M
    return 0;
904
7.70k
  lua_pushvalue(L, obj);
905
7.70k
  lua_call(L, 1, 1);
906
7.70k
  return 1;
907
1.77M
}
908
909
910
375k
LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
911
375k
  lua_Integer l;
912
375k
  int isnum;
913
375k
  lua_len(L, idx);
914
375k
  l = lua_tointegerx(L, -1, &isnum);
915
375k
  if (l_unlikely(!isnum))
916
0
    luaL_error(L, "object length is not an integer");
917
375k
  lua_pop(L, 1);  /* remove object */
918
375k
  return l;
919
375k
}
920
921
922
1.77M
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
923
1.77M
  idx = lua_absindex(L,idx);
924
1.77M
  if (luaL_callmeta(L, idx, "__tostring")) {  /* metafield? */
925
7.70k
    if (!lua_isstring(L, -1))
926
1
      luaL_error(L, "'__tostring' must return a string");
927
7.70k
  }
928
1.76M
  else {
929
1.76M
    switch (lua_type(L, idx)) {
930
232k
      case LUA_TNUMBER: {
931
232k
        char buff[LUA_N2SBUFFSZ];
932
232k
        lua_numbertocstring(L, idx, buff);
933
232k
        lua_pushstring(L, buff);
934
232k
        break;
935
0
      }
936
724k
      case LUA_TSTRING:
937
724k
        lua_pushvalue(L, idx);
938
724k
        break;
939
99.4k
      case LUA_TBOOLEAN:
940
99.4k
        lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
941
99.4k
        break;
942
276k
      case LUA_TNIL:
943
276k
        lua_pushliteral(L, "nil");
944
276k
        break;
945
435k
      default: {
946
435k
        int tt = luaL_getmetafield(L, idx, "__name");  /* try name */
947
435k
        const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
948
435k
                                                 luaL_typename(L, idx);
949
435k
        lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
950
435k
        if (tt != LUA_TNIL)
951
8
          lua_remove(L, -2);  /* remove '__name' */
952
435k
        break;
953
0
      }
954
1.76M
    }
955
1.76M
  }
956
1.77M
  return lua_tolstring(L, -1, len);
957
1.77M
}
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
453k
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
966
453k
  luaL_checkstack(L, nup, "too many upvalues");
967
5.09M
  for (; l->name != NULL; l++) {  /* fill the table with given functions */
968
4.64M
    if (l->func == NULL)  /* placeholder? */
969
463k
      lua_pushboolean(L, 0);
970
4.18M
    else {
971
4.18M
      int i;
972
4.26M
      for (i = 0; i < nup; i++)  /* copy upvalues to the top */
973
86.9k
        lua_pushvalue(L, -nup);
974
4.18M
      lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
975
4.18M
    }
976
4.64M
    lua_setfield(L, -(nup + 2), l->name);
977
4.64M
  }
978
453k
  lua_pop(L, nup);  /* remove upvalues */
979
453k
}
980
981
982
/*
983
** ensure that stack[idx][fname] has a table and push that table
984
** into the stack
985
*/
986
1.23M
LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
987
1.23M
  if (lua_getfield(L, idx, fname) == LUA_TTABLE)
988
1.14M
    return 1;  /* table already there */
989
88.4k
  else {
990
88.4k
    lua_pop(L, 1);  /* remove previous result */
991
88.4k
    idx = lua_absindex(L, idx);
992
88.4k
    lua_newtable(L);
993
88.4k
    lua_pushvalue(L, -1);  /* copy to be left at top */
994
88.4k
    lua_setfield(L, idx, fname);  /* assign new table to field */
995
88.4k
    return 0;  /* false, because did not find table there */
996
88.4k
  }
997
1.23M
}
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
289k
                               lua_CFunction openf, int glb) {
1008
289k
  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
1009
289k
  lua_getfield(L, -1, modname);  /* LOADED[modname] */
1010
289k
  if (!lua_toboolean(L, -1)) {  /* package not already loaded? */
1011
289k
    lua_pop(L, 1);  /* remove field */
1012
289k
    lua_pushcfunction(L, openf);
1013
289k
    lua_pushstring(L, modname);  /* argument to open function */
1014
289k
    lua_call(L, 1, 1);  /* call 'openf' to open module */
1015
289k
    lua_pushvalue(L, -1);  /* make copy of module (call result) */
1016
289k
    lua_setfield(L, -3, modname);  /* LOADED[modname] = module */
1017
289k
  }
1018
289k
  lua_remove(L, -2);  /* remove LOADED table */
1019
289k
  if (glb) {
1020
289k
    lua_pushvalue(L, -1);  /* copy of module */
1021
289k
    lua_setglobal(L, modname);  /* _G[modname] = module */
1022
289k
  }
1023
289k
}
1024
1025
1026
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
1027
820k
                                     const char *p, const char *r) {
1028
820k
  const char *wild;
1029
820k
  size_t l = strlen(p);
1030
11.2M
  while ((wild = strstr(s, p)) != NULL) {
1031
10.4M
    luaL_addlstring(b, s, ct_diff2sz(wild - s));  /* push prefix */
1032
10.4M
    luaL_addstring(b, r);  /* push replacement in place of pattern */
1033
10.4M
    s = wild + l;  /* continue after 'p' */
1034
10.4M
  }
1035
820k
  luaL_addstring(b, s);  /* push last suffix */
1036
820k
}
1037
1038
1039
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
1040
141k
                                  const char *p, const char *r) {
1041
141k
  luaL_Buffer b;
1042
141k
  luaL_buffinit(L, &b);
1043
141k
  luaL_addgsub(&b, s, p, r);
1044
141k
  luaL_pushresult(&b);
1045
141k
  return lua_tostring(L, -1);
1046
141k
}
1047
1048
1049
689M
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
1050
689M
  (void)ud; (void)osize;  /* not used */
1051
689M
  if (nsize == 0) {
1052
370M
    free(ptr);
1053
370M
    return NULL;
1054
370M
  }
1055
318M
  else
1056
318M
    return realloc(ptr, nsize);
1057
689M
}
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
133k
static int checkcontrol (lua_State *L, const char *message, int tocont) {
1090
133k
  if (tocont || *(message++) != '@')  /* not a control message? */
1091
72.7k
    return 0;
1092
60.9k
  else {
1093
60.9k
    if (strcmp(message, "off") == 0)
1094
1.41k
      lua_setwarnf(L, warnfoff, L);  /* turn warnings off */
1095
59.5k
    else if (strcmp(message, "on") == 0)
1096
22.4k
      lua_setwarnf(L, warnfon, L);   /* turn warnings on */
1097
60.9k
    return 1;  /* it was a control message */
1098
60.9k
  }
1099
133k
}
1100
1101
1102
94.7k
static void warnfoff (void *ud, const char *message, int tocont) {
1103
94.7k
  checkcontrol((lua_State *)ud, message, tocont);
1104
94.7k
}
1105
1106
1107
/*
1108
** Writes the message and handle 'tocont', finishing the message
1109
** if needed and setting the next warn function.
1110
*/
1111
9.39k
static void warnfcont (void *ud, const char *message, int tocont) {
1112
9.39k
  lua_State *L = (lua_State *)ud;
1113
9.39k
  lua_writestringerror("%s", message);  /* write message */
1114
9.39k
  if (tocont)  /* not the last part? */
1115
4.06k
    lua_setwarnf(L, warnfcont, L);  /* to be continued */
1116
5.33k
  else {  /* last part */
1117
5.33k
    lua_writestringerror("%s", "\n");  /* finish message with end-of-line */
1118
5.33k
    lua_setwarnf(L, warnfon, L);  /* next call is a new message */
1119
5.33k
  }
1120
9.39k
}
1121
1122
1123
39.0k
static void warnfon (void *ud, const char *message, int tocont) {
1124
39.0k
  if (checkcontrol((lua_State *)ud, message, tocont))  /* control message? */
1125
33.7k
    return;  /* nothing else to be done */
1126
5.33k
  lua_writestringerror("%s", "Lua warning: ");  /* start a new warning */
1127
5.33k
  warnfcont(ud, message, tocont);  /* finish processing */
1128
5.33k
}
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
455k
#define BUFSEEDB  (sizeof(void*) + sizeof(time_t))
1144
1145
/* Size for the buffer in int's, rounded up */
1146
364k
#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
182k
#define addbuff(b,v)  (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))
1153
1154
1155
91.0k
static unsigned int luai_makeseed (void) {
1156
91.0k
  unsigned int buff[BUFSEED];
1157
91.0k
  unsigned int res;
1158
91.0k
  unsigned int i;
1159
91.0k
  time_t t = time(NULL);
1160
91.0k
  char *b = (char*)buff;
1161
91.0k
  addbuff(b, b);  /* local variable's address */
1162
91.0k
  addbuff(b, t);  /* time */
1163
  /* fill (rare but possible) remain of the buffer with zeros */
1164
91.0k
  memset(b, 0, sizeof(buff) - BUFSEEDB);
1165
91.0k
  res = buff[0];
1166
364k
  for (i = 1; i < BUFSEED; i++)
1167
273k
    res ^= (res >> 3) + (res << 7) + buff[i];
1168
91.0k
  return res;
1169
91.0k
}
1170
1171
#endif
1172
1173
1174
28.9k
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
1175
28.9k
  (void)L;  /* unused */
1176
28.9k
  return luai_makeseed();
1177
28.9k
}
1178
1179
1180
62.0k
LUALIB_API lua_State *luaL_newstate (void) {
1181
62.0k
  lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed());
1182
62.0k
  if (l_likely(L)) {
1183
62.0k
    lua_atpanic(L, &panic);
1184
62.0k
    lua_setwarnf(L, warnfoff, L);  /* default is warnings off */
1185
62.0k
  }
1186
62.0k
  return L;
1187
62.0k
}
1188
1189
1190
260k
LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
1191
260k
  lua_Number v = lua_version(L);
1192
260k
  if (sz != LUAL_NUMSIZES)  /* check numeric types */
1193
0
    luaL_error(L, "core and library have incompatible numeric types");
1194
260k
  else if (v != ver)
1195
0
    luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
1196
0
                  (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);
1197
260k
}
1198