You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

upb.h 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Shared definitions for upb Lua modules.
  29. */
  30. #ifndef UPB_LUA_UPB_H_
  31. #define UPB_LUA_UPB_H_
  32. #include "lauxlib.h"
  33. #include "upb/def.h"
  34. #include "upb/msg.h"
  35. #include "upb/reflection.h"
  36. /* Lua changes its API in incompatible ways in every minor release.
  37. * This is some shim code to paper over the differences. */
  38. #if LUA_VERSION_NUM == 501
  39. #define lua_rawlen lua_objlen
  40. #define lua_setuservalue(L, idx) lua_setfenv(L, idx)
  41. #define lua_getuservalue(L, idx) lua_getfenv(L, idx)
  42. #define lupb_setfuncs(L, l) luaL_register(L, NULL, l)
  43. #elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504
  44. #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0)
  45. #else
  46. #error Only Lua 5.1-5.4 are supported
  47. #endif
  48. /* Create a new userdata with the given type and |n| uservals, which are popped
  49. * from the stack to initialize the userdata. */
  50. void* lupb_newuserdata(lua_State* L, size_t size, int n, const char* type);
  51. #if LUA_VERSION_NUM < 504
  52. /* Polyfills for this Lua 5.4 function. Pushes userval |n| for the userdata at
  53. * |index|. */
  54. int lua_setiuservalue(lua_State* L, int index, int n);
  55. int lua_getiuservalue(lua_State* L, int index, int n);
  56. #endif
  57. /* Registers a type with the given name, methods, and metamethods. */
  58. void lupb_register_type(lua_State* L, const char* name, const luaL_Reg* m, const luaL_Reg* mm);
  59. /* Checks the given upb_Status and throws a Lua error if it is not ok. */
  60. void lupb_checkstatus(lua_State* L, upb_Status* s);
  61. int luaopen_lupb(lua_State* L);
  62. /* C <-> Lua value conversions. ***********************************************/
  63. /* Custom check/push functions. Unlike the Lua equivalents, they are pinned to
  64. * specific C types (instead of lua_Number, etc), and do not allow any implicit
  65. * conversion or data loss. */
  66. int64_t lupb_checkint64(lua_State* L, int narg);
  67. int32_t lupb_checkint32(lua_State* L, int narg);
  68. uint64_t lupb_checkuint64(lua_State* L, int narg);
  69. uint32_t lupb_checkuint32(lua_State* L, int narg);
  70. double lupb_checkdouble(lua_State* L, int narg);
  71. float lupb_checkfloat(lua_State* L, int narg);
  72. bool lupb_checkbool(lua_State* L, int narg);
  73. const char* lupb_checkstring(lua_State* L, int narg, size_t* len);
  74. const char* lupb_checkname(lua_State* L, int narg);
  75. void lupb_pushint64(lua_State* L, int64_t val);
  76. void lupb_pushint32(lua_State* L, int32_t val);
  77. void lupb_pushuint64(lua_State* L, uint64_t val);
  78. void lupb_pushuint32(lua_State* L, uint32_t val);
  79. /** From def.c. ***************************************************************/
  80. const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg);
  81. const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg);
  82. const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg);
  83. upb_DefPool* lupb_DefPool_check(lua_State* L, int narg);
  84. void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f);
  85. void lupb_def_registertypes(lua_State* L);
  86. /** From msg.c. ***************************************************************/
  87. void lupb_pushmsgval(lua_State* L, int container, upb_CType type, upb_MessageValue val);
  88. int lupb_MessageDef_call(lua_State* L);
  89. upb_Arena* lupb_Arena_pushnew(lua_State* L);
  90. void lupb_msg_registertypes(lua_State* L);
  91. #define lupb_assert(L, predicate) \
  92. if (!(predicate)) \
  93. luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__);
  94. #define LUPB_UNUSED(var) (void)var
  95. #if defined(__GNUC__) || defined(__clang__)
  96. #define LUPB_UNREACHABLE() \
  97. do \
  98. { \
  99. assert(0); \
  100. __builtin_unreachable(); \
  101. } while (0)
  102. #else
  103. #define LUPB_UNREACHABLE() \
  104. do \
  105. { \
  106. assert(0); \
  107. } while (0)
  108. #endif
  109. #endif /* UPB_LUA_UPB_H_ */