From 70deb7ea257e7e658266e8e2680de2f99d9fb43f Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 5 Nov 2025 13:38:36 +0800 Subject: Fixed Lua5.1 build. [skip CI] --- src/3rdParty/colib/ljson.c | 31 ++++++++++++++++++++++++++++--- src/yue.cpp | 9 +++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/3rdParty/colib/ljson.c b/src/3rdParty/colib/ljson.c index 78f9070..7da4de1 100644 --- a/src/3rdParty/colib/ljson.c +++ b/src/3rdParty/colib/ljson.c @@ -17,6 +17,14 @@ #include "lua.h" #include "lauxlib.h" +#if LUA_VERSION_NUM > 501 +#ifndef LUA_COMPAT_5_1 +#ifndef lua_objlen +#define lua_objlen lua_rawlen +#endif // lua_objlen +#endif // LUA_COMPAT_5_1 +#endif // LUA_VERSION_NUM + // 内存分配函数,方便替换 #define co_malloc malloc #define co_free free @@ -109,11 +117,13 @@ static inline void membuffer_putc_unsafe(membuffer_t *buff, char c) { buff->b[buff->sz++] = c; } +#if LUA_VERSION_NUM > 501 // 写入一段内存:不检查空间(不安全版本) static inline void membuffer_putb_unsafe(membuffer_t *buff, const void *b, size_t sz) { memcpy(buff->b + buff->sz, b, sz); buff->sz += sz; } +#endif // 取当前的指针 static inline char* membuffer_getp(membuffer_t *buff) { @@ -632,6 +642,7 @@ static void dumpper_throw_error(json_dumpper_t *d, lua_State *L, const char *fmt luaL_error(L, d->errmsg); } +#if LUA_VERSION_NUM > 501 static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) { char nbuff[INTEGER_BUFF_SZ]; int i = INTEGER_BUFF_SZ; @@ -647,6 +658,7 @@ static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) { } while (ux /= 10); membuffer_putb_unsafe(&d->buff, nbuff+i, INTEGER_BUFF_SZ-i); } +#endif static void dumpper_process_number(json_dumpper_t *d, lua_State *L, int idx) { lua_Number num = lua_tonumber(L, idx); @@ -706,7 +718,7 @@ static void dumpper_process_string(json_dumpper_t *d, lua_State *L, int idx) { static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth); static int dumpper_check_array(json_dumpper_t *d, lua_State *L, int *len) { - int asize = lua_rawlen(L, -1); + int asize = lua_objlen(L, -1); if (asize > 0) { lua_pushinteger(L, asize); if (lua_next(L, -2) == 0) { @@ -782,9 +794,11 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) { } else if (ktp == LUA_TNUMBER && d->num_as_str) { if (unlikely(d->format)) dumpper_add_indent(d, depth); membuffer_putc(buff, '\"'); +#if LUA_VERSION_NUM > 501 if (lua_isinteger(L, -2)) dumpper_process_integer(d, L, -2); else +#endif dumpper_process_number(d, L, -2); if (likely(!d->format)) membuffer_putb(buff, "\":", 2); @@ -824,9 +838,11 @@ static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth) { dumpper_process_string(d, L, -1); break; case LUA_TNUMBER: +#if LUA_VERSION_NUM > 501 if (lua_isinteger(L, -1)) dumpper_process_integer(d, L, -1); else +#endif dumpper_process_number(d, L, -1); break; case LUA_TBOOLEAN: @@ -891,8 +907,17 @@ static const luaL_Reg lib[] = { {NULL, NULL}, }; -LUAMOD_API int luaopen_colibc_json(lua_State *L) { - luaL_newlib(L, lib); +LUALIB_API int luaopen_colibc_json(lua_State* L) { +#if LUA_VERSION_NUM > 501 + luaL_newlib(L, lib); // json +#else + lua_getglobal(L, "package"); // package + lua_getfield(L, -1, "loaded"); // package loaded + lua_createtable(L, 0, 0); // package loaded json + lua_pushvalue(L, -1); // package loaded json json + lua_setfield(L, -3, "json"); // loaded["json"] = json, package loaded json + luaL_register(L, NULL, lib); // package loaded json +#endif // json.null lua_pushlightuserdata(L, NULL); lua_setfield(L, -2, "null"); diff --git a/src/yue.cpp b/src/yue.cpp index aebc31d..c6b35e6 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -77,21 +77,18 @@ int luaopen_colibc_json(lua_State* L); static void openlibs(void* state) { lua_State* L = static_cast(state); - luaL_openlibs(L); int top = lua_gettop(L); + DEFER(lua_settop(L, top)); + luaL_openlibs(L); #if LUA_VERSION_NUM > 501 luaL_requiref(L, "yue", luaopen_yue, 0); luaL_requiref(L, "json", luaopen_colibc_json, 0); #else lua_pushcfunction(L, luaopen_yue); lua_call(L, 0, 0); - lua_getglobal(L, "package"); // package - lua_getfield(L, -1, "loaded"); // package loaded lua_pushcfunction(L, luaopen_colibc_json); - lua_call(L, 0, 1); // package loaded json - lua_setfield(L, -2, "json"); // loaded["json"] = json, package loaded + lua_call(L, 0, 0); #endif - lua_settop(L, top); } void pushYue(lua_State* L, std::string_view name) { -- cgit v1.2.3-55-g6feb