1/*	$NetBSD: linit.c,v 1.10 2023/04/16 20:46:17 nikita Exp $	*/
2
3/*
4** Id: linit.c
5** Initialization of libraries for lua.c and other clients
6** See Copyright Notice in lua.h
7*/
8
9
10#define linit_c
11#define LUA_LIB
12
13/*
14** If you embed Lua in your program and need to open the standard
15** libraries, call luaL_openlibs in your program. If you need a
16** different set of libraries, copy this file to your project and edit
17** it to suit your needs.
18**
19** You can also *preload* libraries, so that a later 'require' can
20** open the library, which is already linked to the application.
21** For that, do the following code:
22**
23**  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
24**  lua_pushcfunction(L, luaopen_modname);
25**  lua_setfield(L, -2, modname);
26**  lua_pop(L, 1);  // remove PRELOAD table
27*/
28
29#include "lprefix.h"
30
31
32#ifndef _KERNEL
33#include <stddef.h>
34#endif /* _KERNEL */
35
36#include "lua.h"
37
38#include "lualib.h"
39#include "lauxlib.h"
40
41
42/*
43** these libs are loaded by lua.c and are readily available to any Lua
44** program
45*/
46static const luaL_Reg loadedlibs[] = {
47  {LUA_GNAME, luaopen_base},
48#ifndef _KERNEL
49  {LUA_LOADLIBNAME, luaopen_package},
50#endif /* _KERNEL */
51  {LUA_COLIBNAME, luaopen_coroutine},
52  {LUA_TABLIBNAME, luaopen_table},
53#ifndef _KERNEL
54  {LUA_IOLIBNAME, luaopen_io},
55  {LUA_OSLIBNAME, luaopen_os},
56#endif /* _KERNEL */
57  {LUA_STRLIBNAME, luaopen_string},
58#ifndef _KERNEL
59  {LUA_MATHLIBNAME, luaopen_math},
60#endif /* _KERNEL */
61  {LUA_UTF8LIBNAME, luaopen_utf8},
62  {LUA_DBLIBNAME, luaopen_debug},
63  {NULL, NULL}
64};
65
66
67LUALIB_API void luaL_openlibs (lua_State *L) {
68  const luaL_Reg *lib;
69  /* "require" functions from 'loadedlibs' and set results to global table */
70  for (lib = loadedlibs; lib->func; lib++) {
71    luaL_requiref(L, lib->name, lib->func, 1);
72    lua_pop(L, 1);  /* remove lib */
73  }
74}
75
76