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" AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY 20 * DIRECT, 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/def.h" 37 #include "upb/msg.h" 38 39 /* Lua changes its API in incompatible ways in every minor release. 40 * This is some shim code to paper over the differences. */ 41 42 #if LUA_VERSION_NUM == 501 43 #define lua_rawlen lua_objlen 44 #define lua_setuservalue(L, idx) lua_setfenv(L, idx) 45 #define lua_getuservalue(L, idx) lua_getfenv(L, idx) 46 #define lupb_setfuncs(L, l) luaL_register(L, NULL, l) 47 #elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504 48 #define lupb_setfuncs(L, l) luaL_setfuncs(L, l, 0) 49 #else 50 #error Only Lua 5.1-5.4 are supported 51 #endif 52 53 /* Create a new userdata with the given type and |n| uservals, which are popped 54 * from the stack to initialize the userdata. */ 55 void *lupb_newuserdata(lua_State *L, size_t size, int n, const char *type); 56 57 #if LUA_VERSION_NUM < 504 58 /* Polyfills for this Lua 5.4 function. Pushes userval |n| for the userdata at 59 * |index|. */ 60 int lua_setiuservalue(lua_State *L, int index, int n); 61 int lua_getiuservalue(lua_State *L, int index, int n); 62 #endif 63 64 /* Registers a type with the given name, methods, and metamethods. */ 65 void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m, 66 const luaL_Reg *mm); 67 68 /* Checks the given upb_status and throws a Lua error if it is not ok. */ 69 void lupb_checkstatus(lua_State *L, upb_status *s); 70 71 int luaopen_lupb(lua_State *L); 72 73 /* C <-> Lua value conversions. ***********************************************/ 74 75 /* Custom check/push functions. Unlike the Lua equivalents, they are pinned to 76 * specific C types (instead of lua_Number, etc), and do not allow any implicit 77 * conversion or data loss. */ 78 int64_t lupb_checkint64(lua_State *L, int narg); 79 int32_t lupb_checkint32(lua_State *L, int narg); 80 uint64_t lupb_checkuint64(lua_State *L, int narg); 81 uint32_t lupb_checkuint32(lua_State *L, int narg); 82 double lupb_checkdouble(lua_State *L, int narg); 83 float lupb_checkfloat(lua_State *L, int narg); 84 bool lupb_checkbool(lua_State *L, int narg); 85 const char *lupb_checkstring(lua_State *L, int narg, size_t *len); 86 const char *lupb_checkname(lua_State *L, int narg); 87 88 void lupb_pushint64(lua_State *L, int64_t val); 89 void lupb_pushint32(lua_State *L, int32_t val); 90 void lupb_pushuint64(lua_State *L, uint64_t val); 91 void lupb_pushuint32(lua_State *L, uint32_t val); 92 93 /** From def.c. ***************************************************************/ 94 95 const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg); 96 const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg); 97 const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg); 98 upb_symtab *lupb_symtab_check(lua_State *L, int narg); 99 void lupb_msgdef_pushsubmsgdef(lua_State *L, const upb_fielddef *f); 100 101 void lupb_def_registertypes(lua_State *L); 102 103 /** From msg.c. ***************************************************************/ 104 105 int lupb_msgdef_call(lua_State *L); 106 upb_arena *lupb_arena_pushnew(lua_State *L); 107 108 void lupb_msg_registertypes(lua_State *L); 109 110 #define lupb_assert(L, predicate) \ 111 if (!(predicate)) \ 112 luaL_error(L, "internal error: %s, %s:%d ", #predicate, __FILE__, __LINE__); 113 114 #define LUPB_UNUSED(var) (void)var 115 116 #if defined(__GNUC__) || defined(__clang__) 117 #define LUPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0) 118 #else 119 #define LUPB_UNREACHABLE() do { assert(0); } while(0) 120 #endif 121 122 123 #endif /* UPB_LUA_UPB_H_ */ 124