lrotable.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Read-only tables for Lua */
  2. #define LUAC_CROSS_FILE
  3. #include "lua.h"
  4. #include C_HEADER_STRING
  5. #include "lrotable.h"
  6. #include "lauxlib.h"
  7. #include "lstring.h"
  8. #include "lobject.h"
  9. #include "lapi.h"
  10. /* Local defines */
  11. #define LUAR_FINDFUNCTION 0
  12. #define LUAR_FINDVALUE 1
  13. /* Externally defined read-only table array */
  14. extern const luaR_table lua_rotable[];
  15. /* Find a global "read only table" in the constant lua_rotable array */
  16. void* luaR_findglobal(const char *name, unsigned len) {
  17. #ifndef LUA_CROSS_COMPILER
  18. unsigned i;
  19. if (c_strlen(name) > LUA_MAX_ROTABLE_NAME)
  20. return NULL;
  21. for (i=0; lua_rotable[i].name; i ++)
  22. if (*lua_rotable[i].name != '\0' && c_strlen(lua_rotable[i].name) == len && !c_strncmp(lua_rotable[i].name, name, len)) {
  23. return (void*)(lua_rotable[i].pentries);
  24. }
  25. #endif
  26. return NULL;
  27. }
  28. /* Find an entry in a rotable and return it */
  29. static const TValue* luaR_auxfind(const luaR_entry *pentry, const char *strkey, luaR_numkey numkey, unsigned *ppos) {
  30. const TValue *res = NULL;
  31. unsigned i = 0;
  32. if (pentry == NULL)
  33. return NULL;
  34. while(pentry->key.type != LUA_TNIL) {
  35. if ((strkey && (pentry->key.type == LUA_TSTRING) && (!c_strcmp(pentry->key.id.strkey, strkey))) ||
  36. (!strkey && (pentry->key.type == LUA_TNUMBER) && ((luaR_numkey)pentry->key.id.numkey == numkey))) {
  37. res = &pentry->value;
  38. break;
  39. }
  40. i ++; pentry ++;
  41. }
  42. if (res && ppos)
  43. *ppos = i;
  44. return res;
  45. }
  46. int luaR_findfunction(lua_State *L, const luaR_entry *ptable) {
  47. const TValue *res = NULL;
  48. const char *key = luaL_checkstring(L, 2);
  49. res = luaR_auxfind(ptable, key, 0, NULL);
  50. if (res && ttislightfunction(res)) {
  51. luaA_pushobject(L, res);
  52. return 1;
  53. }
  54. else
  55. return 0;
  56. }
  57. /* Find an entry in a rotable and return its type
  58. If "strkey" is not NULL, the function will look for a string key,
  59. otherwise it will look for a number key */
  60. const TValue* luaR_findentry(void *data, const char *strkey, luaR_numkey numkey, unsigned *ppos) {
  61. return luaR_auxfind((const luaR_entry*)data, strkey, numkey, ppos);
  62. }
  63. /* Find the metatable of a given table */
  64. void* luaR_getmeta(void *data) {
  65. #ifdef LUA_META_ROTABLES
  66. const TValue *res = luaR_auxfind((const luaR_entry*)data, "__metatable", 0, NULL);
  67. return res && ttisrotable(res) ? rvalue(res) : NULL;
  68. #else
  69. return NULL;
  70. #endif
  71. }
  72. static void luaR_next_helper(lua_State *L, const luaR_entry *pentries, int pos, TValue *key, TValue *val) {
  73. setnilvalue(key);
  74. setnilvalue(val);
  75. if (pentries[pos].key.type != LUA_TNIL) {
  76. /* Found an entry */
  77. if (pentries[pos].key.type == LUA_TSTRING)
  78. setsvalue(L, key, luaS_newro(L, pentries[pos].key.id.strkey))
  79. else
  80. setnvalue(key, (lua_Number)pentries[pos].key.id.numkey)
  81. setobj2s(L, val, &pentries[pos].value);
  82. }
  83. }
  84. /* next (used for iteration) */
  85. void luaR_next(lua_State *L, void *data, TValue *key, TValue *val) {
  86. const luaR_entry* pentries = (const luaR_entry*)data;
  87. char strkey[LUA_MAX_ROTABLE_NAME + 1], *pstrkey = NULL;
  88. luaR_numkey numkey = 0;
  89. unsigned keypos;
  90. /* Special case: if key is nil, return the first element of the rotable */
  91. if (ttisnil(key))
  92. luaR_next_helper(L, pentries, 0, key, val);
  93. else if (ttisstring(key) || ttisnumber(key)) {
  94. /* Find the previoud key again */
  95. if (ttisstring(key)) {
  96. luaR_getcstr(strkey, rawtsvalue(key), LUA_MAX_ROTABLE_NAME);
  97. pstrkey = strkey;
  98. } else
  99. numkey = (luaR_numkey)nvalue(key);
  100. luaR_findentry(data, pstrkey, numkey, &keypos);
  101. /* Advance to next key */
  102. keypos ++;
  103. luaR_next_helper(L, pentries, keypos, key, val);
  104. }
  105. }
  106. /* Convert a Lua string to a C string */
  107. void luaR_getcstr(char *dest, const TString *src, size_t maxsize) {
  108. if (src->tsv.len+1 > maxsize)
  109. dest[0] = '\0';
  110. else {
  111. c_memcpy(dest, getstr(src), src->tsv.len);
  112. dest[src->tsv.len] = '\0';
  113. }
  114. }
  115. /* Return 1 if the given pointer is a rotable */
  116. #ifdef LUA_META_ROTABLES
  117. #include "compiler.h"
  118. int luaR_isrotable(void *p) {
  119. return RODATA_START_ADDRESS <= (char*)p && (char*)p <= RODATA_END_ADDRESS;
  120. }
  121. #endif