?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  3. // Copyright 2007, Nathan Reed
  4. // SPDX-License-Identifier: MIT
  5.  
  6. #pragma once
  7.  
  8. #include <cassert>
  9.  
  10. namespace luabridge {
  11.  
  12. // These are for Lua versions prior to 5.2.0.
  13. //
  14. #if LUA_VERSION_NUM < 502
  15. inline int lua_absindex(lua_State* L, int idx)
  16. {
  17.     if (idx > LUA_REGISTRYINDEX && idx < 0)
  18.         return lua_gettop(L) + idx + 1;
  19.     else
  20.         return idx;
  21. }
  22.  
  23. inline void lua_rawgetp(lua_State* L, int idx, void const* p)
  24. {
  25.     idx = lua_absindex(L, idx);
  26.     lua_pushlightuserdata(L, const_cast<void*>(p));
  27.     lua_rawget(L, idx);
  28. }
  29.  
  30. inline void lua_rawsetp(lua_State* L, int idx, void const* p)
  31. {
  32.     idx = lua_absindex(L, idx);
  33.     lua_pushlightuserdata(L, const_cast<void*>(p));
  34.     // put key behind value
  35.     lua_insert(L, -2);
  36.     lua_rawset(L, idx);
  37. }
  38.  
  39. #define LUA_OPEQ 1
  40. #define LUA_OPLT 2
  41. #define LUA_OPLE 3
  42.  
  43. inline int lua_compare(lua_State* L, int idx1, int idx2, int op)
  44. {
  45.     switch (op)
  46.     {
  47.     case LUA_OPEQ:
  48.         return lua_equal(L, idx1, idx2);
  49.         break;
  50.  
  51.     case LUA_OPLT:
  52.         return lua_lessthan(L, idx1, idx2);
  53.         break;
  54.  
  55.     case LUA_OPLE:
  56.         return lua_equal(L, idx1, idx2) || lua_lessthan(L, idx1, idx2);
  57.         break;
  58.  
  59.     default:
  60.         return 0;
  61.     };
  62. }
  63.  
  64. inline int get_length(lua_State* L, int idx)
  65. {
  66.     return int(lua_objlen(L, idx));
  67. }
  68.  
  69. #else
  70. inline int get_length(lua_State* L, int idx)
  71. {
  72.     lua_len(L, idx);
  73.     int len = int(luaL_checknumber(L, -1));
  74.     lua_pop(L, 1);
  75.     return len;
  76. }
  77.  
  78. #endif
  79.  
  80. #ifndef LUA_OK
  81. #define LUABRIDGE_LUA_OK 0
  82. #else
  83. #define LUABRIDGE_LUA_OK LUA_OK
  84. #endif
  85.  
  86. /** Get a table value, bypassing metamethods.
  87.  */
  88. inline void rawgetfield(lua_State* L, int index, char const* key)
  89. {
  90.     assert(lua_istable(L, index));
  91.     index = lua_absindex(L, index);
  92.     lua_pushstring(L, key);
  93.     lua_rawget(L, index);
  94. }
  95.  
  96. /** Set a table value, bypassing metamethods.
  97.  */
  98. inline void rawsetfield(lua_State* L, int index, char const* key)
  99. {
  100.     assert(lua_istable(L, index));
  101.     index = lua_absindex(L, index);
  102.     lua_pushstring(L, key);
  103.     lua_insert(L, -2);
  104.     lua_rawset(L, index);
  105. }
  106.  
  107. /** Returns true if the value is a full userdata (not light).
  108.  */
  109. inline bool isfulluserdata(lua_State* L, int index)
  110. {
  111.     return lua_isuserdata(L, index) && !lua_islightuserdata(L, index);
  112. }
  113.  
  114. /** Test lua_State objects for global equality.
  115.  
  116.     This can determine if two different lua_State objects really point
  117.     to the same global state, such as when using coroutines.
  118.  
  119.     @note This is used for assertions.
  120. */
  121. inline bool equalstates(lua_State* L1, lua_State* L2)
  122. {
  123.     return lua_topointer(L1, LUA_REGISTRYINDEX) == lua_topointer(L2, LUA_REGISTRYINDEX);
  124. }
  125.  
  126. } // namespace luabridge
  127.