Fix compiling problems with lua 5.4 (#15113)

Fixes #15112
Fixes #13750

Closes #15113

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/15113 from owent:fixes_15112 dd3a847a0654b06a2e6f8c11d4e57c9bec9bae68
PiperOrigin-RevId: 684928784
diff --git a/lua/def.c b/lua/def.c
index 1f9ee12..194800e 100644
--- a/lua/def.c
+++ b/lua/def.c
@@ -597,7 +597,7 @@
 
 static int lupb_FileDef_Dependency(lua_State* L) {
   const upb_FileDef* f = lupb_FileDef_check(L, 1);
-  int index = luaL_checkint(L, 2);
+  int index = lupb_checkint32(L, 2);
   const upb_FileDef* dep = upb_FileDef_Dependency(f, index);
   lupb_wrapper_pushwrapper(L, 1, dep, LUPB_FILEDEF);
   return 1;
@@ -611,7 +611,7 @@
 
 static int lupb_FileDef_enum(lua_State* L) {
   const upb_FileDef* f = lupb_FileDef_check(L, 1);
-  int index = luaL_checkint(L, 2);
+  int index = lupb_checkint32(L, 2);
   const upb_EnumDef* e = upb_FileDef_TopLevelEnum(f, index);
   lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
   return 1;
@@ -625,7 +625,7 @@
 
 static int lupb_FileDef_msg(lua_State* L) {
   const upb_FileDef* f = lupb_FileDef_check(L, 1);
-  int index = luaL_checkint(L, 2);
+  int index = lupb_checkint32(L, 2);
   const upb_MessageDef* m = upb_FileDef_TopLevelMessage(f, index);
   lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
   return 1;
diff --git a/lua/upb.c b/lua/upb.c
index e1e4061..6a0eec4 100644
--- a/lua/upb.c
+++ b/lua/upb.c
@@ -41,11 +41,13 @@
 /* Lua compatibility code *****************************************************/
 
 /* Shims for upcoming Lua 5.3 functionality. */
+#if LUA_VERSION_NUM < 503
 static bool lua_isinteger(lua_State* L, int argn) {
   LUPB_UNUSED(L);
   LUPB_UNUSED(argn);
   return false;
 }
+#endif
 
 /* Utility functions **********************************************************/
 
diff --git a/lua/upbc.cc b/lua/upbc.cc
index bce4b7d..5193614 100644
--- a/lua/upbc.cc
+++ b/lua/upbc.cc
@@ -50,6 +50,11 @@
   printer->WriteRaw(&text, 1);
 }
 
+static bool IsPrint(int ch) {
+  // isprint(ch) with negative values is UB.
+  return ch < 0 ? false : isprint(ch);
+}
+
 static void PrintString(int max_cols, absl::string_view* str,
                         protobuf::io::Printer* printer) {
   printer->Print("\'");
@@ -61,7 +66,7 @@
     } else if (ch == '\'') {
       printer->PrintRaw("\\'");
       max_cols--;
-    } else if (isprint(ch)) {
+    } else if (IsPrint(ch)) {
       printer->WriteRaw(&ch, 1);
       max_cols--;
     } else {