简体   繁体   English

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

[英]luajit segmentation fault - not in lua-5.2

hello i am trying to switch to luajit. 你好,我正在尝试切换到luajit。 the code below compiles and runs fine when using liblua5.2. 使用liblua5.2时,以下代码可以编译并正常运行。 when trying to compile and link against luajit-2.0 - it compiles fine but segfaults 尝试编译并链接到luajit-2.0时-可以正常编译但出现段错误

has anyone a hint for me? 有人对我有提示吗?

compile 5.2: 编译5.2:

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

compile luajit-2.0 编译luajit-2.0

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

Output on 5.2: 在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

output on luajit: luajit上的输出:

# ./luajit_sample 
LUA: MAIN
Segmentation fault

lua_sample.c 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 (in same folder) 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

the return 0; return 0; in lua_print() was missing. lua_print()中丢失。 adding this - fixed the segfault. 添加此内容-修复了段错误。

thx - to all commenters - adding -Wall -Werror -pedantic would have shown that the lua_print() does not return a value. -Wall -Werror -pedantic对所有评论者-添加-Wall -Werror -pedantic将显示lua_print()不返回值。

so the final lua_print looks like: 所以最终的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM