美文网首页
[RS] xLua从LuaJit升级Lua5.3记录

[RS] xLua从LuaJit升级Lua5.3记录

作者: _Walker__ | 来源:发表于2022-07-11 18:58 被阅读0次

    记录环境

    • Unity 2021.3.4f1
    • xLua-2.1.16_with_silicon_support
    • Lua 5.3.3 => Lua 5.3.5
    各平台需要的软件

    1、Windows

    2、Centos(用来编译Android)


    源码修改

      我们项目需要cjson跟pbc,所以要添加拓展。

    1、CMakeLists.txt
    ...
    
    #begin ======================== lua-cjson ========================
    set ( CJSON_SRC 
        lua-cjson-2.1.0/strbuf.c
        lua-cjson-2.1.0/lua_cjson.c
        lua-cjson-2.1.0/fpconv.c
    )
    #add_definitions(-DUSE_INTERNAL_FPCONV)
    if(WIN32)
        # Windows sprintf()/strtod() handle NaN/inf differently. Not supported.
        add_definitions(-DDISABLE_INVALID_NUMBERS)
    endif()
    set_property(
        SOURCE ${CJSON_SRC}
        APPEND
        PROPERTY COMPILE_DEFINITIONS
        LUA_LIB
    )
    list(APPEND THIRDPART_INC lua-cjson-2.1.0)
    set (THIRDPART_SRC ${THIRDPART_SRC} ${CJSON_SRC})
    #end ======================== lua-cjson ========================
    
    #begin ======================== pbc ========================
    set ( PBC_SRC 
        pbc/binding/lua/pbc-lua.c 
        pbc/src/alloc.c 
        pbc/src/array.c 
        pbc/src/bootstrap.c 
        pbc/src/context.c 
        pbc/src/decode.c 
        pbc/src/map.c 
        pbc/src/pattern.c 
        pbc/src/proto.c 
        pbc/src/register.c 
        pbc/src/rmessage.c 
        pbc/src/stringpool.c 
        pbc/src/varint.c 
        pbc/src/wmessage.c
    )
    set_property(
        SOURCE ${PBC_SRC}
        APPEND
        PROPERTY COMPILE_DEFINITIONS
        LUA_LIB
    )
    list(APPEND THIRDPART_INC pbc)
    list(APPEND THIRDPART_INC pbc/src)
    set (THIRDPART_SRC ${THIRDPART_SRC} ${PBC_SRC})
    #end ======================== pbc ========================
    
    #begin ======================== 宏定义 ========================
    # 兼容5.1的API
    add_definitions(-DLUA_COMPAT_5_1)
    #end ======================== 宏定义 ========================
    
    if (NOT LUA_VERSION)
        # 这里设置Lua版本
        set(LUA_VERSION "5.3.3")
    endif()
    ...
    
    2、xlua - lua-5.3.3源码修改
    1. 删除 loadlib.c、wmain.c 两个文件
    2. 在编译iOS时,修改loslib.c
    将
    int stat = system(cmd);
    改为
    int stat = nftw(cmd, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
    

    同时添加修改:

    #include <ftw.h>
    
    ...
    
    static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW     *ftwbuf)
    {
        int rv = remove(fpath);
        
        if (rv)
            perror(fpath);
        
        return rv;
    }
    
    3、pbc源码修改
    1. pbc.h中添加:
    ...
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /******************** 修改添加 Begin ********************/
    #ifndef bool
    #define bool char
    #endif
    
    #ifndef true
    #define true 1
    #endif
    
    #ifndef false
    #define false 0
    #endif
    /******************** 修改添加 End ********************/
    ...
    
    1. pbc-lua.c修改
    int
    luaopen_protobuf_c(lua_State *L) {
    改为
    LUALIB_API int
    luaopen_protobuf_c(lua_State *L) {
    
    4、cjson源码修改
    1. lua_cjson.c
    // 添加别名定义
    #ifdef _MSC_VER
    #define strcasecmp stricmp
    #define strncasecmp  strnicmp 
    #endif
    
    // lua导出注册
    int luaopen_cjson(lua_State *l)
    改为
    LUALIB_API int luaopen_cjson(lua_State *l)
    

    编译

    问题记录

    1、Android运行报错

    LuaException: error loading module Main.Main_C from CustomLoader, Main/Main_C: size_t size mismatch in precompiled chunk
    

    Lua 5.3.5之前的版本没有支持通用字节码,这个折腾了两天时间Orz。所以不能使用Lua 5.3.3版本。

    参考文章
    UWA开源库:uLua Project
    lua cjson 数字问题

    相关文章

      网友评论

          本文标题:[RS] xLua从LuaJit升级Lua5.3记录

          本文链接:https://www.haomeiwen.com/subject/ywievrtx.html