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 /* 29 * Shared definitions for upb Lua modules. 30 */ 31 32 #ifndef UPB_LUA_UPB_H_ 33 #define UPB_LUA_UPB_H_ 34 35 #include "lauxlib.h" 36 #include "upb/msg.h" 37 #include "upb/reflection/def.h" 38 #include "upb/reflection/message.h" 39 40 /* Lua changes its API in incompatible ways in every minor release. 41 * This is some shim code to paper over the differences. */ 42 43 #if LUA_VERSION_NUM == 501 44 #define lua_rawlen lua_objlen 45 #define lua_setuservalue(L, idx) lua_setfenv(L, idx) 46 #define lua_getuservalue(L, idx) lua_getfenv(L, idx) 47 #define lupb_setfuncs(L, l) luaL_register(L, NULL, l) 48 #elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504 49 #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0) 50 #else 51 #error Only Lua 5.1-5.4 are supported 52 #endif 53 54 /* Create a new userdata with the given type and |n| uservals, which are popped 55 * from the stack to initialize the userdata. */ 56 void* lupb_newuserdata(lua_State* L, size_t size, int n, const char* type); 57 58 #if LUA_VERSION_NUM < 504 59 /* Polyfills for this Lua 5.4 function. Pushes userval |n| for the userdata at 60 * |index|. */ 61 int lua_setiuservalue(lua_State* L, int index, int n); 62 int lua_getiuservalue(lua_State* L, int index, int n); 63 #endif 64 65 /* Registers a type with the given name, methods, and metamethods. */ 66 void lupb_register_type(lua_State* L, const char* name, const luaL_Reg* m, 67 const luaL_Reg* mm); 68 69 /* Checks the given upb_Status and throws a Lua error if it is not ok. */ 70 void lupb_checkstatus(lua_State* L, upb_Status* s); 71 72 int luaopen_lupb(lua_State* L); 73 74 /* C <-> Lua value conversions. ***********************************************/ 75 76 /* Custom check/push functions. Unlike the Lua equivalents, they are pinned to 77 * specific C types (instead of lua_Number, etc), and do not allow any implicit 78 * conversion or data loss. */ 79 int64_t lupb_checkint64(lua_State* L, int narg); 80 int32_t lupb_checkint32(lua_State* L, int narg); 81 uint64_t lupb_checkuint64(lua_State* L, int narg); 82 uint32_t lupb_checkuint32(lua_State* L, int narg); 83 double lupb_checkdouble(lua_State* L, int narg); 84 float lupb_checkfloat(lua_State* L, int narg); 85 bool lupb_checkbool(lua_State* L, int narg); 86 const char* lupb_checkstring(lua_State* L, int narg, size_t* len); 87 const char* lupb_checkname(lua_State* L, int narg); 88 89 void lupb_pushint64(lua_State* L, int64_t val); 90 void lupb_pushint32(lua_State* L, int32_t val); 91 void lupb_pushuint64(lua_State* L, uint64_t val); 92 void lupb_pushuint32(lua_State* L, uint32_t val); 93 94 /** From def.c. ***************************************************************/ 95 96 const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg); 97 const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg); 98 const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg); 99 upb_DefPool* lupb_DefPool_check(lua_State* L, int narg); 100 void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f); 101 102 void lupb_def_registertypes(lua_State* L); 103 104 /** From msg.c. ***************************************************************/ 105 106 void lupb_pushmsgval(lua_State* L, int container, upb_CType type, 107 upb_MessageValue val); 108 int lupb_MessageDef_call(lua_State* L); 109 upb_Arena* lupb_Arena_pushnew(lua_State* L); 110 111 void lupb_msg_registertypes(lua_State* L); 112 113 #define lupb_assert(L, predicate) \ 114 if (!(predicate)) \ 115 luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__); 116 117 #define LUPB_UNUSED(var) (void)var 118 119 #if defined(__GNUC__) || defined(__clang__) 120 #define LUPB_UNREACHABLE() \ 121 do { \ 122 assert(0); \ 123 __builtin_unreachable(); \ 124 } while (0) 125 #else 126 #define LUPB_UNREACHABLE() \ 127 do { \ 128 assert(0); \ 129 } while (0) 130 #endif 131 132 #endif /* UPB_LUA_UPB_H_ */ 133