1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 /* 9 * Shared definitions for upb Lua modules. 10 */ 11 12 #ifndef UPB_LUA_UPB_H_ 13 #define UPB_LUA_UPB_H_ 14 15 #include "lauxlib.h" 16 #include "upb/message/message.h" 17 #include "upb/reflection/def.h" 18 #include "upb/reflection/message.h" 19 20 /* Lua changes its API in incompatible ways in every minor release. 21 * This is some shim code to paper over the differences. */ 22 23 #if LUA_VERSION_NUM == 501 24 #define lua_rawlen lua_objlen 25 #define lua_setuservalue(L, idx) lua_setfenv(L, idx) 26 #define lua_getuservalue(L, idx) lua_getfenv(L, idx) 27 #define lupb_setfuncs(L, l) luaL_register(L, NULL, l) 28 #elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504 29 #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0) 30 #else 31 #error Only Lua 5.1-5.4 are supported 32 #endif 33 34 /* Create a new userdata with the given type and |n| uservals, which are popped 35 * from the stack to initialize the userdata. */ 36 void* lupb_newuserdata(lua_State* L, size_t size, int n, const char* type); 37 38 #if LUA_VERSION_NUM < 504 39 /* Polyfills for this Lua 5.4 function. Pushes userval |n| for the userdata at 40 * |index|. */ 41 int lua_setiuservalue(lua_State* L, int index, int n); 42 int lua_getiuservalue(lua_State* L, int index, int n); 43 #endif 44 45 /* Registers a type with the given name, methods, and metamethods. */ 46 void lupb_register_type(lua_State* L, const char* name, const luaL_Reg* m, 47 const luaL_Reg* mm); 48 49 /* Checks the given upb_Status and throws a Lua error if it is not ok. */ 50 void lupb_checkstatus(lua_State* L, upb_Status* s); 51 52 int luaopen_lupb(lua_State* L); 53 54 /* C <-> Lua value conversions. ***********************************************/ 55 56 /* Custom check/push functions. Unlike the Lua equivalents, they are pinned to 57 * specific C types (instead of lua_Number, etc), and do not allow any implicit 58 * conversion or data loss. */ 59 int64_t lupb_checkint64(lua_State* L, int narg); 60 int32_t lupb_checkint32(lua_State* L, int narg); 61 uint64_t lupb_checkuint64(lua_State* L, int narg); 62 uint32_t lupb_checkuint32(lua_State* L, int narg); 63 double lupb_checkdouble(lua_State* L, int narg); 64 float lupb_checkfloat(lua_State* L, int narg); 65 bool lupb_checkbool(lua_State* L, int narg); 66 const char* lupb_checkstring(lua_State* L, int narg, size_t* len); 67 const char* lupb_checkname(lua_State* L, int narg); 68 69 void lupb_pushint64(lua_State* L, int64_t val); 70 void lupb_pushint32(lua_State* L, int32_t val); 71 void lupb_pushuint64(lua_State* L, uint64_t val); 72 void lupb_pushuint32(lua_State* L, uint32_t val); 73 74 /** From def.c. ***************************************************************/ 75 76 const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg); 77 const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg); 78 const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg); 79 upb_DefPool* lupb_DefPool_check(lua_State* L, int narg); 80 void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f); 81 82 void lupb_def_registertypes(lua_State* L); 83 84 /** From msg.c. ***************************************************************/ 85 86 void lupb_pushmsgval(lua_State* L, int container, upb_CType type, 87 upb_MessageValue val); 88 int lupb_MessageDef_call(lua_State* L); 89 upb_Arena* lupb_Arena_pushnew(lua_State* L); 90 91 void lupb_msg_registertypes(lua_State* L); 92 93 #define lupb_assert(L, predicate) \ 94 if (!(predicate)) \ 95 luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__); 96 97 #define LUPB_UNUSED(var) (void)var 98 99 #if defined(__GNUC__) || defined(__clang__) 100 #define LUPB_UNREACHABLE() \ 101 do { \ 102 assert(0); \ 103 __builtin_unreachable(); \ 104 } while (0) 105 #else 106 #define LUPB_UNREACHABLE() \ 107 do { \ 108 assert(0); \ 109 } while (0) 110 #endif 111 112 #endif /* UPB_LUA_UPB_H_ */ 113