lib_init.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. ** Library initialization.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major parts taken verbatim from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lib_init_c
  9. #define LUA_LIB
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "lualib.h"
  13. #include "lj_arch.h"
  14. static const luaL_Reg lj_lib_load[] = {
  15. { "", luaopen_base },
  16. { LUA_LOADLIBNAME, luaopen_package },
  17. { LUA_TABLIBNAME, luaopen_table },
  18. { LUA_IOLIBNAME, luaopen_io },
  19. { LUA_OSLIBNAME, luaopen_os },
  20. { LUA_STRLIBNAME, luaopen_string },
  21. { LUA_MATHLIBNAME, luaopen_math },
  22. { LUA_DBLIBNAME, luaopen_debug },
  23. { LUA_BITLIBNAME, luaopen_bit },
  24. { LUA_JITLIBNAME, luaopen_jit },
  25. { NULL, NULL }
  26. };
  27. static const luaL_Reg lj_lib_preload[] = {
  28. #if LJ_HASFFI
  29. { LUA_FFILIBNAME, luaopen_ffi },
  30. #endif
  31. { NULL, NULL }
  32. };
  33. LUALIB_API void luaL_openlibs(lua_State *L)
  34. {
  35. const luaL_Reg *lib;
  36. for (lib = lj_lib_load; lib->func; lib++) {
  37. lua_pushcfunction(L, lib->func);
  38. lua_pushstring(L, lib->name);
  39. lua_call(L, 1, 0);
  40. }
  41. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD",
  42. sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1);
  43. for (lib = lj_lib_preload; lib->func; lib++) {
  44. lua_pushcfunction(L, lib->func);
  45. lua_setfield(L, -2, lib->name);
  46. }
  47. lua_pop(L, 1);
  48. }