Lua调用C/C++函数

来源:互联网 发布:交换机端口绑定ip地址 编辑:程序博客网 时间:2024/05/17 04:13
////  main.cpp//  LuaAndCpp//#include <iostream>static int lua_getName(lua_State* L){    lua_pushstring(L, "string from c");    std::cout << "lua call c function \n" ;    return 1;}static int showOne(lua_State* L){    const char* value = luaL_checkstring(L, -1);    std::cout << value << std::endl;    lua_pushstring(L, "get lua string");    return 1;}static int showTwo(lua_State* L){    const char* value1 = luaL_checkstring(L, -1);    const char* value2 = luaL_checkstring(L, -2);    std::cout << value1 << " " << value2 << std::endl;    lua_pushstring(L, "test -1-");    return 1;}static int showTwo2(lua_State* L){    const char* value1 = luaL_checkstring(L, -1);    const char* value2 = luaL_checkstring(L, -2);    std::cout << value1 << " " << value2 << std::endl;    lua_pushstring(L, "test -2-");    return 0;}static int backTwo(lua_State* L){    lua_pushstring(L, "--back--");    lua_pushnumber(L, 100);    return 2;}static int backTable(lua_State* L){    lua_newtable(L);    char str[20] = {0};    for (int i = 1 ; i<=10 ; i++) {        lua_pushnumber(L, i);  // key        sprintf(str, "num is %i", i);        lua_pushstring(L, str);  // value        lua_settable(L, -3);    }    return 1;}static int backTable2(lua_State* L){    lua_newtable(L);    char str[20] = {0};    int loop = 1;    while (loop <= 10) {        sprintf(str, "num is %i",loop);        lua_pushstring(L, str);        lua_pushstring(L, "app");        loop++;        lua_settable(L, -3);    }    return 1;}void register_my_functions(lua_State* L){    lua_pushcfunction(L, lua_getName);    lua_setglobal(L, "lua_getName");    lua_pushcfunction(L, showOne);    lua_setglobal(L, "showOne");    lua_pushcfunction(L, showTwo);    lua_setglobal(L, "showTwo");    lua_pushcfunction(L, showTwo2);    lua_setglobal(L, "showTwo2");    lua_pushcfunction(L, backTwo);    lua_setglobal(L, "backTwo");    lua_pushcfunction(L, backTable);    lua_setglobal(L, "backTable");    lua_pushcfunction(L, backTable2);    lua_setglobal(L, "backTable2");}int main(int argc, const char * argv[]) {    // insert code here...    std::cout << "Hello, World!\n";    lua_State* m_luaState = luaL_newstate();    luaL_openlibs(m_luaState);    register_my_functions(m_luaState);    luaL_dofile(m_luaState, "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/scripts/config.lua");    lua_close(m_luaState);    return 0;}

Lua中调用C/C++函数

print(lua_getName())print(showOne('test string from lua'))print('--1--',showTwo('cocos','2dx'))print('--2--',showTwo2('2dx','cocos'))local x , y = backTwo()print(type(x),x,type(y),y)local t = backTable()for key , value in pairs(t) do    print(key,value)endlocal t2 = backTable2()for k , v in pairs(t2) do    print(k,v)end

运行结果:

lua call c function string from ctest string from luaget lua string2dx cocos--1--   test -1-cocos 2dx--2--string  --back--    number  1001   num is 12   num is 23   num is 34   num is 45   num is 56   num is 67   num is 78   num is 89   num is 910  num is 10num is 10   appnum is 2    appnum is 6    appnum is 1    appnum is 5    appnum is 3    appnum is 9    appnum is 7    appnum is 8    appnum is 4    app
0 0
原创粉丝点击