diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-11-05 13:38:36 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-11-05 13:38:36 +0800 |
| commit | 70deb7ea257e7e658266e8e2680de2f99d9fb43f (patch) | |
| tree | 32b08eacd001491e72a8a12b00b24dd5840fb575 | |
| parent | 2c40c5f766eaab3e90cab6a54f6b513ff60e7e3e (diff) | |
| download | yuescript-70deb7ea257e7e658266e8e2680de2f99d9fb43f.tar.gz yuescript-70deb7ea257e7e658266e8e2680de2f99d9fb43f.tar.bz2 yuescript-70deb7ea257e7e658266e8e2680de2f99d9fb43f.zip | |
Fixed Lua5.1 build. [skip CI]
| -rw-r--r-- | src/3rdParty/colib/ljson.c | 31 | ||||
| -rw-r--r-- | 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 @@ | |||
| 17 | #include "lua.h" | 17 | #include "lua.h" |
| 18 | #include "lauxlib.h" | 18 | #include "lauxlib.h" |
| 19 | 19 | ||
| 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 | |||
| 20 | // 内存分配函数,方便替换 | 28 | // 内存分配函数,方便替换 |
| 21 | #define co_malloc malloc | 29 | #define co_malloc malloc |
| 22 | #define co_free free | 30 | #define co_free free |
| @@ -109,11 +117,13 @@ static inline void membuffer_putc_unsafe(membuffer_t *buff, char c) { | |||
| 109 | buff->b[buff->sz++] = c; | 117 | buff->b[buff->sz++] = c; |
| 110 | } | 118 | } |
| 111 | 119 | ||
| 120 | #if LUA_VERSION_NUM > 501 | ||
| 112 | // 写入一段内存:不检查空间(不安全版本) | 121 | // 写入一段内存:不检查空间(不安全版本) |
| 113 | static inline void membuffer_putb_unsafe(membuffer_t *buff, const void *b, size_t sz) { | 122 | static inline void membuffer_putb_unsafe(membuffer_t *buff, const void *b, size_t sz) { |
| 114 | memcpy(buff->b + buff->sz, b, sz); | 123 | memcpy(buff->b + buff->sz, b, sz); |
| 115 | buff->sz += sz; | 124 | buff->sz += sz; |
| 116 | } | 125 | } |
| 126 | #endif | ||
| 117 | 127 | ||
| 118 | // 取当前的指针 | 128 | // 取当前的指针 |
| 119 | static inline char* membuffer_getp(membuffer_t *buff) { | 129 | 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 | |||
| 632 | luaL_error(L, d->errmsg); | 642 | luaL_error(L, d->errmsg); |
| 633 | } | 643 | } |
| 634 | 644 | ||
| 645 | #if LUA_VERSION_NUM > 501 | ||
| 635 | static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) { | 646 | static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) { |
| 636 | char nbuff[INTEGER_BUFF_SZ]; | 647 | char nbuff[INTEGER_BUFF_SZ]; |
| 637 | int i = INTEGER_BUFF_SZ; | 648 | int i = INTEGER_BUFF_SZ; |
| @@ -647,6 +658,7 @@ static void dumpper_process_integer(json_dumpper_t *d, lua_State *L, int idx) { | |||
| 647 | } while (ux /= 10); | 658 | } while (ux /= 10); |
| 648 | membuffer_putb_unsafe(&d->buff, nbuff+i, INTEGER_BUFF_SZ-i); | 659 | membuffer_putb_unsafe(&d->buff, nbuff+i, INTEGER_BUFF_SZ-i); |
| 649 | } | 660 | } |
| 661 | #endif | ||
| 650 | 662 | ||
| 651 | static void dumpper_process_number(json_dumpper_t *d, lua_State *L, int idx) { | 663 | static void dumpper_process_number(json_dumpper_t *d, lua_State *L, int idx) { |
| 652 | lua_Number num = lua_tonumber(L, idx); | 664 | 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) { | |||
| 706 | static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth); | 718 | static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth); |
| 707 | 719 | ||
| 708 | static int dumpper_check_array(json_dumpper_t *d, lua_State *L, int *len) { | 720 | 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); |
| 710 | if (asize > 0) { | 722 | if (asize > 0) { |
| 711 | lua_pushinteger(L, asize); | 723 | lua_pushinteger(L, asize); |
| 712 | if (lua_next(L, -2) == 0) { | 724 | if (lua_next(L, -2) == 0) { |
| @@ -782,9 +794,11 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) { | |||
| 782 | } else if (ktp == LUA_TNUMBER && d->num_as_str) { | 794 | } else if (ktp == LUA_TNUMBER && d->num_as_str) { |
| 783 | if (unlikely(d->format)) dumpper_add_indent(d, depth); | 795 | if (unlikely(d->format)) dumpper_add_indent(d, depth); |
| 784 | membuffer_putc(buff, '\"'); | 796 | membuffer_putc(buff, '\"'); |
| 797 | #if LUA_VERSION_NUM > 501 | ||
| 785 | if (lua_isinteger(L, -2)) | 798 | if (lua_isinteger(L, -2)) |
| 786 | dumpper_process_integer(d, L, -2); | 799 | dumpper_process_integer(d, L, -2); |
| 787 | else | 800 | else |
| 801 | #endif | ||
| 788 | dumpper_process_number(d, L, -2); | 802 | dumpper_process_number(d, L, -2); |
| 789 | if (likely(!d->format)) | 803 | if (likely(!d->format)) |
| 790 | membuffer_putb(buff, "\":", 2); | 804 | membuffer_putb(buff, "\":", 2); |
| @@ -824,9 +838,11 @@ static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth) { | |||
| 824 | dumpper_process_string(d, L, -1); | 838 | dumpper_process_string(d, L, -1); |
| 825 | break; | 839 | break; |
| 826 | case LUA_TNUMBER: | 840 | case LUA_TNUMBER: |
| 841 | #if LUA_VERSION_NUM > 501 | ||
| 827 | if (lua_isinteger(L, -1)) | 842 | if (lua_isinteger(L, -1)) |
| 828 | dumpper_process_integer(d, L, -1); | 843 | dumpper_process_integer(d, L, -1); |
| 829 | else | 844 | else |
| 845 | #endif | ||
| 830 | dumpper_process_number(d, L, -1); | 846 | dumpper_process_number(d, L, -1); |
| 831 | break; | 847 | break; |
| 832 | case LUA_TBOOLEAN: | 848 | case LUA_TBOOLEAN: |
| @@ -891,8 +907,17 @@ static const luaL_Reg lib[] = { | |||
| 891 | {NULL, NULL}, | 907 | {NULL, NULL}, |
| 892 | }; | 908 | }; |
| 893 | 909 | ||
| 894 | LUAMOD_API int luaopen_colibc_json(lua_State *L) { | 910 | LUALIB_API int luaopen_colibc_json(lua_State* L) { |
| 895 | luaL_newlib(L, lib); | 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 | ||
| 896 | // json.null | 921 | // json.null |
| 897 | lua_pushlightuserdata(L, NULL); | 922 | lua_pushlightuserdata(L, NULL); |
| 898 | lua_setfield(L, -2, "null"); | 923 | 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); | |||
| 77 | 77 | ||
| 78 | static void openlibs(void* state) { | 78 | static void openlibs(void* state) { |
| 79 | lua_State* L = static_cast<lua_State*>(state); | 79 | lua_State* L = static_cast<lua_State*>(state); |
| 80 | luaL_openlibs(L); | ||
| 81 | int top = lua_gettop(L); | 80 | int top = lua_gettop(L); |
| 81 | DEFER(lua_settop(L, top)); | ||
| 82 | luaL_openlibs(L); | ||
| 82 | #if LUA_VERSION_NUM > 501 | 83 | #if LUA_VERSION_NUM > 501 |
| 83 | luaL_requiref(L, "yue", luaopen_yue, 0); | 84 | luaL_requiref(L, "yue", luaopen_yue, 0); |
| 84 | luaL_requiref(L, "json", luaopen_colibc_json, 0); | 85 | luaL_requiref(L, "json", luaopen_colibc_json, 0); |
| 85 | #else | 86 | #else |
| 86 | lua_pushcfunction(L, luaopen_yue); | 87 | lua_pushcfunction(L, luaopen_yue); |
| 87 | lua_call(L, 0, 0); | 88 | lua_call(L, 0, 0); |
| 88 | lua_getglobal(L, "package"); // package | ||
| 89 | lua_getfield(L, -1, "loaded"); // package loaded | ||
| 90 | lua_pushcfunction(L, luaopen_colibc_json); | 89 | lua_pushcfunction(L, luaopen_colibc_json); |
| 91 | lua_call(L, 0, 1); // package loaded json | 90 | lua_call(L, 0, 0); |
| 92 | lua_setfield(L, -2, "json"); // loaded["json"] = json, package loaded | ||
| 93 | #endif | 91 | #endif |
| 94 | lua_settop(L, top); | ||
| 95 | } | 92 | } |
| 96 | 93 | ||
| 97 | void pushYue(lua_State* L, std::string_view name) { | 94 | void pushYue(lua_State* L, std::string_view name) { |
