Skip to content

Commit 70deb7e

Browse files
committed
Fixed Lua5.1 build. [skip CI]
1 parent 2c40c5f commit 70deb7e

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/3rdParty/colib/ljson.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
#include "lua.h"
1818
#include "lauxlib.h"
1919

20+
#if LUA_VERSION_NUM > 501
21+
#ifndef LUA_COMPAT_5_1
22+
#ifndef lua_objlen
23+
#define lua_objlen lua_rawlen
24+
#endif // lua_objlen
25+
#endif // LUA_COMPAT_5_1
26+
#endif // LUA_VERSION_NUM
27+
2028
// 内存分配函数,方便替换
2129
#define co_malloc malloc
2230
#define co_free free
@@ -109,11 +117,13 @@ static inline void membuffer_putc_unsafe(membuffer_t *buff, char c) {
109117
buff->b[buff->sz++] = c;
110118
}
111119

120+
#if LUA_VERSION_NUM > 501
112121
// 写入一段内存:不检查空间(不安全版本)
113122
static inline void membuffer_putb_unsafe(membuffer_t *buff, const void *b, size_t sz) {
114123
memcpy(buff->b + buff->sz, b, sz);
115124
buff->sz += sz;
116125
}
126+
#endif
117127

118128
// 取当前的指针
119129
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
632642
luaL_error(L, d->errmsg);
633643
}
634644

645+
#if LUA_VERSION_NUM > 501
635646
static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) {
636647
char nbuff[INTEGER_BUFF_SZ];
637648
int i = INTEGER_BUFF_SZ;
@@ -647,6 +658,7 @@ static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) {
647658
} while (ux /= 10);
648659
membuffer_putb_unsafe(&d->buff, nbuff+i, INTEGER_BUFF_SZ-i);
649660
}
661+
#endif
650662

651663
static void dumpper_process_number(json_dumpper_t *d, lua_State *L, int idx) {
652664
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) {
706718
static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth);
707719

708720
static int dumpper_check_array(json_dumpper_t *d, lua_State *L, int *len) {
709-
int asize = lua_rawlen(L, -1);
721+
int asize = lua_objlen(L, -1);
710722
if (asize > 0) {
711723
lua_pushinteger(L, asize);
712724
if (lua_next(L, -2) == 0) {
@@ -782,9 +794,11 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) {
782794
} else if (ktp == LUA_TNUMBER && d->num_as_str) {
783795
if (unlikely(d->format)) dumpper_add_indent(d, depth);
784796
membuffer_putc(buff, '\"');
797+
#if LUA_VERSION_NUM > 501
785798
if (lua_isinteger(L, -2))
786799
dumpper_process_integer(d, L, -2);
787800
else
801+
#endif
788802
dumpper_process_number(d, L, -2);
789803
if (likely(!d->format))
790804
membuffer_putb(buff, "\":", 2);
@@ -824,9 +838,11 @@ static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth) {
824838
dumpper_process_string(d, L, -1);
825839
break;
826840
case LUA_TNUMBER:
841+
#if LUA_VERSION_NUM > 501
827842
if (lua_isinteger(L, -1))
828843
dumpper_process_integer(d, L, -1);
829844
else
845+
#endif
830846
dumpper_process_number(d, L, -1);
831847
break;
832848
case LUA_TBOOLEAN:
@@ -891,8 +907,17 @@ static const luaL_Reg lib[] = {
891907
{NULL, NULL},
892908
};
893909

894-
LUAMOD_API int luaopen_colibc_json(lua_State *L) {
895-
luaL_newlib(L, lib);
910+
LUALIB_API int luaopen_colibc_json(lua_State* L) {
911+
#if LUA_VERSION_NUM > 501
912+
luaL_newlib(L, lib); // json
913+
#else
914+
lua_getglobal(L, "package"); // package
915+
lua_getfield(L, -1, "loaded"); // package loaded
916+
lua_createtable(L, 0, 0); // package loaded json
917+
lua_pushvalue(L, -1); // package loaded json json
918+
lua_setfield(L, -3, "json"); // loaded["json"] = json, package loaded json
919+
luaL_register(L, NULL, lib); // package loaded json
920+
#endif
896921
// json.null
897922
lua_pushlightuserdata(L, NULL);
898923
lua_setfield(L, -2, "null");

src/yue.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,18 @@ int luaopen_colibc_json(lua_State* L);
7777

7878
static void openlibs(void* state) {
7979
lua_State* L = static_cast<lua_State*>(state);
80-
luaL_openlibs(L);
8180
int top = lua_gettop(L);
81+
DEFER(lua_settop(L, top));
82+
luaL_openlibs(L);
8283
#if LUA_VERSION_NUM > 501
8384
luaL_requiref(L, "yue", luaopen_yue, 0);
8485
luaL_requiref(L, "json", luaopen_colibc_json, 0);
8586
#else
8687
lua_pushcfunction(L, luaopen_yue);
8788
lua_call(L, 0, 0);
88-
lua_getglobal(L, "package"); // package
89-
lua_getfield(L, -1, "loaded"); // package loaded
9089
lua_pushcfunction(L, luaopen_colibc_json);
91-
lua_call(L, 0, 1); // package loaded json
92-
lua_setfield(L, -2, "json"); // loaded["json"] = json, package loaded
90+
lua_call(L, 0, 0);
9391
#endif
94-
lua_settop(L, top);
9592
}
9693

9794
void pushYue(lua_State* L, std::string_view name) {

0 commit comments

Comments
 (0)