Lua调用C/C++函数

11 篇文章 0 订阅
//
//  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)
end

local t2 = backTable2()

for k , v in pairs(t2) do
    print(k,v)
end

运行结果:

lua call c function 
string from c
test string from lua
get lua string
2dx cocos
--1--   test -1-
cocos 2dx
--2--
string  --back--    number  100
1   num is 1
2   num is 2
3   num is 3
4   num is 4
5   num is 5
6   num is 6
7   num is 7
8   num is 8
9   num is 9
10  num is 10
num is 10   app
num is 2    app
num is 6    app
num is 1    app
num is 5    app
num is 3    app
num is 9    app
num is 7    app
num is 8    app
num is 4    app
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值