luajit 分段错误 - 不在 lua-5.2 中

luajit segmentation fault - not in lua-5.2

你好,我正在尝试切换到 luajit。 下面的代码在使用 liblua5.2 时可以正常编译和运行。 当尝试针对 luajit-2.0 进行编译和 link - 它编译正常但出现段错误

有没有人给我提示?

编译 5.2:

gcc -g -O0 -I/usr/include/lua5.2/ -llua5.2 -o lua_sample lua_sample.c

编译luajit-2.0

 gcc -Wall -I/usr/local/include/luajit-2.0/ -lluajit-5.1 -o luajit_sample lua_sample.c

5.2 上的输出:

# ./lua_sample 
LUA: MAIN
Script ended with 22
LUA: ######## HOOK CALLED - Start
LUA: # service_id: 322
LUA: # service_name: sssasdf
LUA: #     setting new state to 4
SET_STATUS: Service Object: sssasdf
SET_STATUS: Code: 4
LUA: #    call returned RES:-123
LUA: ######## HOOK CALLED - END
HOOK ended with -123

luajit 上的输出:

# ./luajit_sample 
LUA: MAIN
Segmentation fault

lua_sample.c

#include <stdio.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* the Lua interpreter */

struct service {
    int service_id;
    char service_name[50];
};

static int lua_print(lua_State *L) {
    int i;
    int nargs = lua_gettop(L);

     for (i=1; i <= nargs; ++i) {
        printf("LUA: %s\n",  lua_tostring(L, i));
    }   

}
static int lua_callback_service_set_status(lua_State *L) {
    int status;
    struct service * svc;


    svc=lua_touserdata(L, 1);
    status=lua_tonumber(L, 2);

    printf("SET_STATUS: Service Object: %s\n", svc->service_name);
    printf("SET_STATUS: Code: %d\n",status);

    lua_pushnumber(L, -123);
    return 1;
}

int main ( int argc, char *argv[] )
{
    int res;
    lua_State* L;


    struct service svc = {
        .service_id=322,
        .service_name="sssasdf"
    };

    /* initialize Lua */    
    L = luaL_newstate();

    /* load various Lua libraries */
    luaL_openlibs(L);

    lua_register(L, "callback_service_set_status", lua_callback_service_set_status);
    lua_register(L, "print", lua_print);



    /* run the script */
    luaL_dostring(L, "return dofile('sample.lua')");


    res = lua_tonumber(L, -1);
    printf("Script ended with %d\n", res);


    /* the function name */
    lua_getglobal(L, "callback_service_finish_hook");


    lua_pushlightuserdata(L, (void*)&svc );


    lua_newtable(L);

    lua_pushliteral(L, "service_id" );
    lua_pushnumber(L, svc.service_id );
    lua_settable(L, -3);  


    lua_pushliteral(L, "service_name" );
    lua_pushstring(L, svc.service_name );
    lua_settable(L, -3);  





    if(lua_pcall(L, 2, 1, 0) != 0 ) {

        printf("error running function `callback_service_finish_hook': %s\n", lua_tostring(L, -1));

    } else {
        /* get the result */    
        res = (int)lua_tonumber(L, -1);
        lua_pop(L, 1);
        printf("HOOK ended with %d\n", res);
    }


    /* print the result */






    /* cleanup Lua */
    lua_close(L);

    return 0;
}

sample.lua(在同一文件夹中)

function callback_service_finish_hook(svc_obj, svc_table) 
    print("######## HOOK CALLED - Start")
    print("# service_id: " ..  svc_table["service_id"])
    print("# service_name: " ..  svc_table["service_name"])
    print("#       setting new state to 4")
    r = callback_service_set_status(svc_obj, 4)
    print("#    call returned RES:" .. r)
    print("######## HOOK CALLED - END")
    return r
end


print("MAIN")
return 22

lua_print() 中的 return 0; 丢失了。 添加这个 - 修复了段错误。

thx - 致所有评论者 - 添加 -Wall -Werror -pedantic 会表明 lua_print() 没有 return 值。

所以最后的 lua_print 看起来像:

static int lua_print(lua_State *L) {
    int i;
    int nargs = lua_gettop(L);

    for (i=1; i <= nargs; ++i) {
        printf("LUA: %s\n",  lua_tostring(L, i));
    }   
   return 0;
}