Coverage Report

Created: 2024-04-23 06:32

/src/testdir/build/lua-master/source/linit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: linit.c $
3
** Initialization of libraries for lua.c and other clients
4
** See Copyright Notice in lua.h
5
*/
6
7
8
#define linit_c
9
#define LUA_LIB
10
11
12
#include "lprefix.h"
13
14
15
#include <stddef.h>
16
17
#include "lua.h"
18
19
#include "lualib.h"
20
#include "lauxlib.h"
21
22
23
/*
24
** Standard Libraries. (Must be listed in the same ORDER of their
25
** respective constants LUA_<libname>K.)
26
*/
27
static const luaL_Reg stdlibs[] = {
28
  {LUA_GNAME, luaopen_base},
29
  {LUA_LOADLIBNAME, luaopen_package},
30
  {LUA_COLIBNAME, luaopen_coroutine},
31
  {LUA_DBLIBNAME, luaopen_debug},
32
  {LUA_IOLIBNAME, luaopen_io},
33
  {LUA_MATHLIBNAME, luaopen_math},
34
  {LUA_OSLIBNAME, luaopen_os},
35
  {LUA_STRLIBNAME, luaopen_string},
36
  {LUA_TABLIBNAME, luaopen_table},
37
  {LUA_UTF8LIBNAME, luaopen_utf8},
38
  {NULL, NULL}
39
};
40
41
42
/*
43
** require and preload selected standard libraries
44
*/
45
4.53k
LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
46
4.53k
  int mask;
47
4.53k
  const luaL_Reg *lib;
48
4.53k
  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
49
49.9k
  for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
50
45.3k
    if (load & mask) {  /* selected? */
51
45.3k
      luaL_requiref(L, lib->name, lib->func, 1);  /* require library */
52
45.3k
      lua_pop(L, 1);  /* remove result from the stack */
53
45.3k
    }
54
0
    else if (preload & mask) {  /* selected? */
55
0
      lua_pushcfunction(L, lib->func);
56
0
      lua_setfield(L, -2, lib->name);  /* add library to PRELOAD table */
57
0
    }
58
45.3k
  }
59
4.53k
  lua_assert((mask >> 1) == LUA_UTF8LIBK);
60
4.53k
  lua_pop(L, 1);  /* remove PRELOAD table */
61
4.53k
}
62