我只写简单的!

  • lua版本 5.1.4
  • 环境 ubuntu 12.04

从C/C++调用lua中自定义的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <lua.hpp>
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
lua_State *L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
int ret = luaL_dofile(L, "test.lua");
if (ret != 0)
{
cout << "load test.lua failed" << endl;
return 1;
}
lua_getglobal(L, "showValue");
lua_pushstring(L, "William");
lua_pushstring(L, "Hsueh");
lua_pcall(L, 2, 0, 0);
lua_close(L);
return 0;c++
}

test.lua文件:

1
2
3
function showValue(strFirstName, strLastName)
print(strFirstName .. "." .. strLastName)
end

从lua中返回一个值给C/C++

main.cpp,注意lua_pcall中设置了返回3个值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <lua.hpp>
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
lua_State *L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
int ret = luaL_dofile(L, "test.lua");
if (ret != 0)
{
cout << "load test.lua failed" << endl;
return 1;
}
lua_getglobal(L, "showValue");
lua_pushstring(L, "William");
lua_pushstring(L, "Hsueh");
lua_pcall(L, 2, 3, 0);
int nOld = lua_tonumber(L, -1);
const char *pIs = lua_tostring(L, -2);
const char *pName = lua_tostring(L, -3);
cout << pName << " " << pIs << " " << nOld << endl;
lua_pop(L, 3);
lua_close(L);
return 0;
}

test.lua文件返回了3个值

1
2
3
4
5
function showValue(strFirstName, strLastName)
strName = strFirstName .. "." .. strLastName
print(strName)
return strName, "is", 18
end

Lua调用C/C++中自定义的函数

Lua不能调用任意的C/C++函数。可以向Lua中注册的函数,需要符合以下的规范:
typedef int (*lua_CFunction) (lua_State *L);
main.cpp,定义了一个名为lua_printValue的函数,并且注册为PrintValue的lua函数,以便于在Lua中使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <lua.hpp>
#include <iostream>
using namespace std;
int lua_printValue(lua_State *L)
{
// get the parameter
const char *pValue = lua_tostring(L, -1);
// do something here...
lua_pushstring(L, pValue);
lua_pushstring(L, "Year");
// return 2 value
return 2;
}
int main(int argc, const char * argv[])
{
lua_State *L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
// register the lua_printValue function
// as PrintValue which can be used in lua
lua_pushcfunction(L, lua_printValue);
lua_setglobal(L, "PrintValue");
int ret = luaL_dofile(L, "test.lua");
if (ret != 0)
{
cout << "load test.lua failed" << endl;
return 1;
}
lua_getglobal(L, "showValue");
lua_pushstring(L, "William");
lua_pushstring(L, "Hsueh");
lua_pcall(L, 2, 3, 0);
int nOld = lua_tonumber(L, -1);
const char *pIs = lua_tostring(L, -2);
const char *pName = lua_tostring(L, -3);
cout << pName << " " << pIs << " " << nOld << endl;
lua_pop(L, 3);
lua_close(L);
return 0;
}

test.lua文件:

1
2
3
4
5
6
function showValue(strFirstName, strLastName)
strName = strFirstName .. "." .. strLastName
strThis, strYear = PrintValue("this")
strAge = strThis .. " " .. strYear .. " is"
return strName, strAge, 18
end

PrintValue从Lua中接受一个参数,在C/C++中处理了一下,返回两个参数给Lua。

Comments