Coverage Report

Created: 2025-12-11 06:33

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