您的位置:首页 > 编程语言 > Lua

Lua调用C/C++函数

2016-07-18 21:00 483 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua