lrotable.c 4.0 KB

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