lrotable.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Read-only tables for Lua */
  2. #ifndef lrotable_h
  3. #define lrotable_h
  4. #include "lua.h"
  5. #include "luaconf.h"
  6. #include "lobject.h"
  7. #include "llimits.h"
  8. /* Macros one can use to define rotable entries */
  9. #define LRO_FUNCVAL(v) {{.p = v}, LUA_TLIGHTFUNCTION}
  10. #define LRO_LUDATA(v) {{.p = v}, LUA_TLIGHTUSERDATA}
  11. #define LRO_NUMVAL(v) {{.n = v}, LUA_TNUMBER}
  12. #define LRO_ROVAL(v) {{.p = (void*)v}, LUA_TROTABLE}
  13. #define LRO_NILVAL {{.p = NULL}, LUA_TNIL}
  14. #ifdef LUA_CROSS_COMPILER
  15. #define LRO_STRKEY(k) {LUA_TSTRING, {.strkey = k}}
  16. #else
  17. #define LRO_STRKEY(k) {LUA_TSTRING, {.strkey = (STORE_ATTR char *) k}}
  18. #endif
  19. #define LRO_NUMKEY(k) {LUA_TNUMBER, {.numkey = k}}
  20. #define LRO_NILKEY {LUA_TNIL, {.strkey=NULL}}
  21. /* Maximum length of a rotable name and of a string key*/
  22. #define LUA_MAX_ROTABLE_NAME 32
  23. /* Type of a numeric key in a rotable */
  24. typedef int luaR_numkey;
  25. /* The next structure defines the type of a key */
  26. typedef struct {
  27. int type;
  28. union {
  29. const char* strkey;
  30. luaR_numkey numkey;
  31. } id;
  32. } luaR_key;
  33. /* An entry in the read only table */
  34. typedef struct luaR_entry {
  35. const luaR_key key;
  36. const TValue value;
  37. } luaR_entry;
  38. /*
  39. * The current ROTable implmentation is a vector of luaR_entry terminated by a
  40. * nil record. The convention is to use ROtable * to refer to the entire vector
  41. * as a logical ROTable.
  42. */
  43. typedef const struct luaR_entry ROTable;
  44. const TValue* luaR_findentry(ROTable *tab, TString *key, unsigned *ppos);
  45. const TValue* luaR_findentryN(ROTable *tab, luaR_numkey numkey, unsigned *ppos);
  46. void luaR_next(lua_State *L, ROTable *tab, TValue *key, TValue *val);
  47. void* luaR_getmeta(ROTable *tab);
  48. int luaR_isrotable(void *p);
  49. /*
  50. * Set inRO check depending on platform. Note that this implementation needs
  51. * to work on both the host (luac.cross) and ESP targets. The luac.cross
  52. * VM is used for the -e option, and is primarily used to be able to debug
  53. * VM changes on the more developer-friendly hot gdb environment.
  54. */
  55. #if defined(LUA_CROSS_COMPILER)
  56. #if defined(_MSC_VER)
  57. //msvc build uses these dummy vars to locate the beginning and ending addresses of the RO data
  58. extern cons char _ro_start[], _ro_end[];
  59. #define IN_RODATA_AREA(p) (((const char*)(p)) >= _ro_start && ((const char *)(p)) <= _ro_end)
  60. #else /* one of the POSIX variants */
  61. #if defined(__CYGWIN__)
  62. #define _RODATA_END __end__
  63. #elif defined(__MINGW32__)
  64. #define _RODATA_END end
  65. #else
  66. #define _RODATA_END _edata
  67. #endif
  68. extern const char _RODATA_END[];
  69. #define IN_RODATA_AREA(p) (((const char *)(p)) < _RODATA_END)
  70. #endif /* defined(_MSC_VER) */
  71. #else /* xtensa tool chain for ESP target */
  72. extern const char _irom0_text_start[];
  73. extern const char _irom0_text_end[];
  74. #define IN_RODATA_AREA(p) (((const char *)(p)) >= _irom0_text_start && ((const char *)(p)) <= _irom0_text_end)
  75. #endif /* defined(LUA_CROSS_COMPILER) */
  76. /* Return 1 if the given pointer is a rotable */
  77. #define luaR_isrotable(p) IN_RODATA_AREA(p)
  78. #endif