linit.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $
  3. ** Initialization of libraries for lua.c and other clients
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define linit_c
  7. #define LUA_LIB
  8. #define LUA_CORE
  9. /*
  10. ** NodeMCU uses RO segment based static ROTable declarations for library
  11. ** tables including the index of library tables itself (the ROM table).
  12. ** These tables are moved from RAM to flash ROM on the ESPs.
  13. **
  14. ** In the case of ESP firmware builds, explicit control of the loader
  15. ** directives "linker magic" allows the marshalling of table entries for
  16. ** the master ROM and library initialisation vectors through linker-based
  17. ** PSECTs so that the corresponding tables can be bound during the link
  18. ** process rather than being statically declared here. This avoids the
  19. ** need to reconfigure this linit.c file to reflect the subset of the total
  20. ** modules selected for a given build. This same mechanism is used to
  21. ** include the lbaselib.c functions into the master ROM table.
  22. **
  23. ** In contrast the host-based luac.cross builds must link on a range of
  24. ** platforms where we don't have control of PSECT placement. However these
  25. ** only use a small fixed list of libraries, which can be defined in this
  26. ** linit.c. This avoids the need for linker magic on host builds and
  27. ** simplifies building luac.cross across a range of host toolchains. One
  28. ** compilation in this case is that the lbaselib.c functions must be compiled
  29. ** into a separate external ROTable which is cascaded into the ROM resolution
  30. ** using its metatable __index hook.
  31. */
  32. #include "lprefix.h"
  33. #include <stddef.h>
  34. #include "lua.h"
  35. #include "lualib.h"
  36. #include "lauxlib.h"
  37. #include "lstate.h"
  38. #include "lnodemcu.h"
  39. extern LROT_TABLE(strlib);
  40. extern LROT_TABLE(tab_funcs);
  41. extern LROT_TABLE(dblib);
  42. extern LROT_TABLE(co_funcs);
  43. extern LROT_TABLE(mathlib);
  44. extern LROT_TABLE(utf8);
  45. #define LROT_ROM_ENTRIES \
  46. LROT_TABENTRY( string, strlib ) \
  47. LROT_TABENTRY( table, tab_funcs ) \
  48. LROT_TABENTRY( debug, dblib) \
  49. LROT_TABENTRY( coroutine, co_funcs ) \
  50. LROT_TABENTRY( math, mathlib ) \
  51. LROT_TABENTRY( utf8, utf8 ) \
  52. LROT_TABENTRY( ROM, rotables )
  53. #define LROT_LIB_ENTRIES \
  54. LROT_FUNCENTRY( _G, luaopen_base ) \
  55. LROT_FUNCENTRY( package, luaopen_package ) \
  56. LROT_FUNCENTRY( string, luaopen_string ) \
  57. LROT_FUNCENTRY( nodemcu, luaN_init )
  58. /*
  59. * Note that this nodemcu entry isn't a normal library initialisaiton but
  60. * instead is a hook to allow the loading of a new LFS. This load process
  61. * needs base and string to be initialised but not the untrustworthy
  62. * modules and so is slotted in here.
  63. */
  64. #if defined(LUA_CROSS_COMPILER)
  65. /* _G __index -> rotables __index -> base_func */
  66. extern LROT_TABLE(rotables_meta);
  67. extern LROT_TABLE(base_func);
  68. LROT_BEGIN(rotables_meta, NULL, LROT_MASK_INDEX)
  69. LROT_TABENTRY( __index, base_func)
  70. LROT_END(rotables_meta, NULL, LROT_MASK_INDEX)
  71. LROT_BEGIN(rotables, LROT_TABLEREF(rotables_meta), 0)
  72. LROT_TABENTRY( _G, base_func)
  73. LROT_ROM_ENTRIES
  74. LROT_END(rotables, LROT_TABLEREF(rotables_meta), 0)
  75. LROT_BEGIN(lua_libs, NULL, 0)
  76. LROT_LIB_ENTRIES
  77. LROT_FUNCENTRY( io, luaopen_io )
  78. LROT_FUNCENTRY( os, luaopen_os )
  79. LROT_END(lua_libs, NULL, 0)
  80. #else /* LUA_USE_ESP */
  81. /* _G __index -> rotables __index (rotables includes base_func) */
  82. extern const ROTable_entry lua_libs_base[];
  83. extern const ROTable_entry lua_rotable_base[];
  84. ROTable rotables_ROTable; /* NOT const in this case */
  85. LROT_ENTRIES_IN_SECTION(rotables, rotable)
  86. LROT_ROM_ENTRIES
  87. LROT_BREAK(rotables)
  88. LROT_ENTRIES_IN_SECTION(lua_libs, libs)
  89. LROT_LIB_ENTRIES
  90. LROT_BREAK(lua_libs)
  91. #endif
  92. void luaL_openlibs (lua_State *L) {
  93. #ifdef LUA_CROSS_COMPILER
  94. const ROTable_entry *p = LROT_TABLEREF(lua_libs)->entry;
  95. #else
  96. const ROTable_entry *p = lua_libs_base;
  97. lua_createrotable(L, LROT_TABLEREF(rotables), lua_rotable_base, NULL);
  98. #endif
  99. /* Now do lua opens */
  100. for ( ; p->key; p++) {
  101. if (ttislcf(&p->value) && fvalue(&p->value))
  102. luaL_requiref(L, p->key, fvalue(&p->value), 1);
  103. }
  104. }