Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tolua.c中的LUAJIT_VERSION不生效(The LUAJIT_VERSION is not working in tolua.c) #37

Open
zhangjiequan opened this issue Jun 10, 2023 · 1 comment

Comments

@zhangjiequan
Copy link

zhangjiequan commented Jun 10, 2023

问题(Issue)

在tolua.c中,试图根据宏LUAJIT_VERSION对代码进行区分,
然而实际情况是,并未按预期走入我们所期望的分支。
在文件tolua_runtime\tolua.c中,代码如下:
(In tolua.c, there is an attempt to differentiate the code based on the LUAJIT_VERSION macro.
However, in reality, it does not follow the expected path.
The code snippet in the file tolua_runtime\tolua.c is as follows:)

#ifdef LUAJIT_VERSION            
        luaL_traceback(L, L1, msg, level);
#else        
        lua_getref(L, LUA_RIDX_TRACEBACK);
        lua_pushthread(L1);
        lua_pushvalue(L, arg + 1);
        lua_pushnumber(L, level + 1);
        lua_call(L, 3, 1);
#endif

分析(Analysis)

以当前的Windows版本为例,代码的初衷是要走LuaJIT的逻辑分支。
然而,由于既未在tolua.c中引用定义LUAJIT_VERSION的头文件luajit.h,也未在编译指令中加入宏LUAJIT_VERSION,
因此实际上执行了else分支的代码。
(Taking the current Windows version as an example, the code was intended to handle the LuaJIT logical branch.
However, due to the lack of inclusion of the luajit.h header file, which defines LUAJIT_VERSION, in the tolua.c file, and the absence of the LUAJIT_VERSION macro in the compilation instructions,
the code actually executes the else branch.)

解决(Solution)

考虑到macnojit也会使用该tolua.c文件,直接包含"luajit.h"的方式并不十分友好,需要进行其他的改动。
建议在编译指令中加入LUAJIT_VERSION。
(Since the tolua.c file is also used for macnojit, directly including "luajit.h" is not very friendly and requires other modifications.
Here, it is suggested to add LUAJIT_VERSION in the compilation instructions.)

Windows

以Windows为例,示例如下:
(Here is an example for Windows::)

#tolua_runtime\build_win64.sh
gcc -m64 -O2 -std=gnu99 -shared \
 -DLUAJIT_VERSION="\"LuaJIT 2.1.0-beta3\"" \   #add this line
 tolua.c \

如上述代码所示,我在第二行中增加了对LUAJIT_VERSION的定义。
(As shown in the code above, a definition for LUAJIT_VERSION has been added in the second line.)

Android

#tolua_runtime\android\jni\Android.mk
LOCAL_CFLAGS :=  -O2 -std=gnu99
LOCAL_CFLAGS += "-DLUAJIT_VERSION=\"LuaJIT 2.1.0-beta3\""   #add this line
LOCAL_SRC_FILES :=  ../../tolua.c \

iOS

Xcode ---> select "target" ---> "Build Settings" ---> Search “Preprocessor Macros” ---> add LUAJIT_VERSION="LuaJIT 2.1.0-beta3":
image
After adding it, the changes to the corresponding serialized file tolua_runtime\iOS\tolua.xcodeproj\project.pbxproj are as follows:
image

@zhangjiequan
Copy link
Author

后续-兼容代码(Subsequent - Compatibility Code)

而宏LUAJIT_VERSION生效后,我们可以基于它优雅地写兼容性代码。
(Once the LUAJIT_VERSION macro is working, we can elegantly write compatibility code based on it. )

现状(Current Status)

以cjson为例子,需要根据是否为luajit注释与反注释第一、二行。
(Take cjson as an example, we need to comment and uncomment the first and second lines depending on whether it's luajit.)
当前代码如下:
(The current code is as follows:)
tolua_runtime\cjson\lua_cjson.c

//#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
#if 0
static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
{
    //...
}
#endif

LUAJIT_VERSION 生效后(After LUAJIT_VERSION is working)

可修改为:

#if !defined(LUAJIT_VERSION) && LUA_VERSION_NUM < 502
static void luaL_setfuncs(lua_State *l, const luaL_Reg *reg, int nup)
{
    //...
}
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant