linit.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
  3. ** Initialization of libraries for lua.c
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define linit_c
  7. #define LUA_LIB
  8. /*
  9. ** NodeMCU uses RO segment based static ROTable declarations for all library
  10. ** tables, including the index of library tables itself (the ROM table). These
  11. ** tables can be moved from RAM to flash ROM on the ESPs.
  12. **
  13. ** On the ESPs targets, we can marshal the table entries through linker-based
  14. ** PSECTs to enable the library initiation tables to be bound during the link
  15. ** process rather than being statically declared here. This simplifies the
  16. ** addition of new modules and configuring builds with a subset of the total
  17. ** modules available.
  18. **
  19. ** Such a linker-based approach is not practical for cross compiler builds that
  20. ** must link on a range of platforms, and where we don't have control of PSECT
  21. ** placement. However unlike the target builds, the luac.cross builds only
  22. ** use a small and fixed list of libraries and so in this case all of libraries
  23. ** are defined here, avoiding the need for linker magic on host builds.
  24. **
  25. ** Note that a separate ROTable is defined in lbaselib.c on luac.cross builds
  26. ** for the base functions. (These use linker based entries on target builds)
  27. ** and there is a metatable index cascade from _G to this base function table
  28. ** to the master rotables table. In the target build, the linker marshals the
  29. ** table, hence the LROT_BREAK() macros which don't 0 terminate the lists and
  30. ** skip generating the ROtable header.
  31. */
  32. #include "lua.h"
  33. #include "lualib.h"
  34. #include "lauxlib.h"
  35. #include "lstate.h"
  36. #include "lnodemcu.h"
  37. extern LROT_TABLE(strlib);
  38. extern LROT_TABLE(tab_funcs);
  39. extern LROT_TABLE(dblib);
  40. extern LROT_TABLE(co_funcs);
  41. extern LROT_TABLE(math);
  42. #define LROT_ROM_ENTRIES \
  43. LROT_TABENTRY( string, strlib ) \
  44. LROT_TABENTRY( table, tab_funcs ) \
  45. LROT_TABENTRY( debug, dblib) \
  46. LROT_TABENTRY( coroutine, co_funcs ) \
  47. LROT_TABENTRY( math, math ) \
  48. LROT_TABENTRY( ROM, rotables )
  49. #define LROT_LIB_ENTRIES \
  50. LROT_FUNCENTRY( _G, luaopen_base ) /* This MUST be called first */ \
  51. LROT_FUNCENTRY( package, luaopen_package ) \
  52. LROT_FUNCENTRY( string, luaopen_string ) \
  53. LROT_FUNCENTRY( debug, luaopen_debug )
  54. #if defined(LUA_CROSS_COMPILER)
  55. extern LROT_TABLE(base_func);
  56. LROT_BEGIN(rotables_meta, NULL, LROT_MASK_INDEX)
  57. LROT_TABENTRY( __index, base_func)
  58. LROT_END(rotables_meta, NULL, LROT_MASK_INDEX)
  59. extern LROT_TABLE(oslib);
  60. extern LROT_TABLE(iolib);
  61. LROT_BEGIN(rotables, LROT_TABLEREF(rotables_meta), 0)
  62. LROT_ROM_ENTRIES
  63. LROT_TABENTRY( os, oslib )
  64. LROT_TABENTRY( io, iolib )
  65. LROT_END(rotables, LROT_TABLEREF(rotables_meta), 0)
  66. LROT_BEGIN(lua_libs, NULL, 0)
  67. LROT_LIB_ENTRIES
  68. LROT_FUNCENTRY( io, luaopen_io )
  69. LROT_END(lua_libs, NULL, 0)
  70. #else
  71. extern const ROTable_entry lua_libs_base[];
  72. extern const ROTable_entry lua_rotable_base[];
  73. ROTable rotables_ROTable;
  74. LROT_ENTRIES_IN_SECTION(rotables, rotable)
  75. LROT_ROM_ENTRIES
  76. LROT_BREAK(rotables)
  77. LROT_ENTRIES_IN_SECTION(lua_libs, libs)
  78. LROT_LIB_ENTRIES
  79. LROT_BREAK(lua_libs)
  80. #endif
  81. void luaL_openlibs (lua_State *L) {
  82. #ifdef LUA_CROSS_COMPILER
  83. const ROTable_entry *p = LROT_TABLEREF(lua_libs)->entry;
  84. #else
  85. const ROTable_entry *p = lua_libs_base;
  86. lua_createrotable(L, LROT_TABLEREF(rotables), lua_rotable_base, NULL);
  87. #endif
  88. while (p->key) {
  89. if (ttislightfunction(&p->value) && fvalue(&p->value)) {
  90. lua_pushcfunction(L, fvalue(&p->value));
  91. lua_pushstring(L, p->key);
  92. lua_call(L, 1, 0); // call luaopen_XXX(libname)
  93. }
  94. p++;
  95. }
  96. }