在工作中,我们经常会遇到不同语言之间的互相调用,比如:c++与lua的交互,c++与java的交互。今天就整理了下在我们工作中 c++调用lua中的函数、变量等等。lua调用c++我们可以用coos2d自带的bindings-generator,很好,很方便。
c++与java的交互:http://blog.csdn.net/qq_22990635/article/details/67056385
c++与lua的交互:http://blog.csdn.net/qq_22990635/article/details/53128533
在lua API中,调用一个函数的步骤很简单:
1.压入你要调用的函数,使用lua_getglobal。
2.压入调用参数。
3.使用lua_pcall
4.从栈中弹出结果。
FromC.lua 文件
local a = -10
print('--math.abs(a)--',math.abs(a))
width = 1080
height = 720
function test1()
print('--function test1--')
end
function test2(var)
print('--function test2--')
print('--var--',var)
end
function test3()
print('--function test3--')
return "调用了lua function test3"
end
function test4(var)
print('--function test4--')
local num = tonumber(var)
num = num + 10
return num
end
function test5(var)
print('--function test5--')
local t = 10
var = var + 100
return t , var
end
function test6(x,y)
print('--function test6--')
print('--x--y--',x,y)
return x + y
end
--Lua变量
strTemp = "cjs66666"
--Lua的table
tableTemp = {name = "cjs", uuid = 100}
CCallLua.h 文件
#ifndef _CCALLLUA_H_
#define _CCALLLUA_H_
#include "cocos2d.h"
USING_NS_CC;
class CCallLua
{
public:
CCallLua() :m_pLuaState(NULL){};
~CCallLua();
void init();
std::string getFilePath(std::string fileName);
static CCallLua* getInstance();
static void destroyInstance();
// read variable in lua
void getConfigWidthAndHeight(int* width, int* height);
// call lua function that no param and no return value
void callLuaFunction1();
// call lua function that one param and no return value
void callLuaFunction2();
// call lua function that no param and one return value
void callLuaFunction3();
// call lua function that one param and one return value
void callLuaFunction4();
// call lua function that one param and two return value
void callLuaFunction5();
// call lua function that two param and one return value
void callLuaFunction6();
// read lua global variable
void callLuaFunction7();
//read lua table
void callLuaFunction8();
private:
lua_State* m_pLuaState;
};
#endif
CCallLua.cpp 文件
#include "CCallLua.h"
#include "CCLuaEngine.h"
using namespace std;
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
CCallLua::~CCallLua()
{
lua_close(m_pLuaState);
}
static CCallLua* LuaEngine_instance = NULL;
CCallLua* CCallLua::getInstance()
{
if (LuaEngine_instance == NULL)
{
LuaEngine_instance = new CCallLua();
LuaEngine_instance->init();
}
return LuaEngine_instance;
}
void CCallLua::destroyInstance()
{
if (LuaEngine_instance)
{
delete LuaEngine_instance;
LuaEngine_instance = NULL;
}
}
void CCallLua::init()
{
m_pLuaState = luaL_newstate();
luaL_openlibs(m_pLuaState);
luaL_dofile(m_pLuaState, this->getFilePath("FromC.lua").c_str());
// lua_pcall(m_pLuaState, 0, 0, 0);
}
std::string CCallLua::getFilePath(std::string fileName)
{
return "src/app/" + fileName;
}
void CCallLua::getConfigWidthAndHeight(int* pWidth, int* pHeight)
{
lua_getglobal(m_pLuaState, "width");
// -1 栈顶位置 lua_isnumber 检测是否能转换成数字类型
if (lua_isnumber(m_pLuaState, -1))
{
int width = (int)lua_tonumber(m_pLuaState, -1);
log("width =%d", width);
*pWidth = width;
}
lua_pop(m_pLuaState, 1);
lua_getglobal(m_pLuaState, "height");
if (lua_isnumber(m_pLuaState, -1))
{
int height = (int)lua_tonumber(m_pLuaState, -1);
log("height =%d", height);
*pHeight = height;
}
lua_pop(m_pLuaState, 1);
lua_getglobal(m_pLuaState, "name");
if (lua_isstring(m_pLuaState, -1))
{
char* str = (char*)lua_tostring(m_pLuaState, -1);
log("name =%s", str);
}
lua_pop(m_pLuaState, 1);
// Lua中的 numStr 是 number 类型
// lua_isstring 不是判断是不是字符串类型,而是检测是否能转换成字符串类型
lua_getglobal(m_pLuaState, "numStr");
if (lua_isstring(m_pLuaState, -1))
{
char* str = (char*)lua_tostring(m_pLuaState, -1);
log("name =%s", *str);
}
lua_pop(m_pLuaState, 1);
}
void CCallLua::callLuaFunction1()
{
lua_getglobal(m_pLuaState, "test1");
//the second param means called function param number;the third param means return param number
int rel = lua_pcall(m_pLuaState, 0, 0, 1);
if (rel == -1)
{
log("callLuaFunction1 error \n");
}
}
void CCallLua::callLuaFunction2()
{
lua_getglobal(m_pLuaState, "test2");
lua_pushstring(m_pLuaState, "call function test2");
int rel = lua_pcall(m_pLuaState, 1, 0, -1);
if (rel == -1)
{
log("callLuaFunction1 error \n");
}
}
void CCallLua::callLuaFunction3()
{
lua_getglobal(m_pLuaState, "test3");
int rel = lua_pcall(m_pLuaState, 0, 1, -1);
if (rel == -1)
{
log("callLuaFunction3 error \n");
}
char* str = (char*)lua_tostring(m_pLuaState, -1);
log("lua function test3 返回值 =%s",str);
}
void CCallLua::callLuaFunction4()
{
lua_getglobal(m_pLuaState, "test4");
// 传参
lua_pushnumber(m_pLuaState, 10);
int rel = lua_pcall(m_pLuaState, 1, 1, -1);
if (rel == -1)
{
log("callLuaFunction4 error \n");
}
int num = (int)lua_tonumber(m_pLuaState, -1);
log("lua function test4 返回值 =%d", num);
}
void CCallLua::callLuaFunction5()
{
lua_getglobal(m_pLuaState, "test5");
lua_pushnumber(m_pLuaState, 10);
int rel = lua_pcall(m_pLuaState, 1, 2, -1);
if (rel == -1)
{
log("callLuaFunction5 error \n");
}
int t = (int)lua_tonumber(m_pLuaState, -2); //获取第一个返回值
int var = (int)lua_tonumber(m_pLuaState, -1); //获取第二个返回值
log("lua function test5 返回值 -- t=%d , var=%d ",t,var);
}
void CCallLua::callLuaFunction6()
{
lua_getglobal(m_pLuaState, "test6");
lua_pushnumber(m_pLuaState, 10);
lua_pushnumber(m_pLuaState, 13);
int rel = lua_pcall(m_pLuaState, 2, 1, -1);
if (rel == -1)
{
log("callLuaFunction6 error \n");
}
if (lua_isnumber(m_pLuaState, -1))
{
int num = (int)lua_tonumber(m_pLuaState, -1);
log("x+y=%d", num);
lua_pop(m_pLuaState, 1);
}
}
void CCallLua::callLuaFunction7()
{
lua_getglobal(m_pLuaState, "strTemp");
string strTemp = lua_tostring(m_pLuaState, -1);
log("read lua param---%s", strTemp.c_str());
}
void CCallLua::callLuaFunction8()
{
lua_getglobal(m_pLuaState, "tableTemp");
lua_getfield(m_pLuaState, -1, "name");
lua_getfield(m_pLuaState, -2, "uuid");
string strName = lua_tostring(m_pLuaState, -2);
int struuid = lua_tonumber(m_pLuaState, -1);
log("read lua table--strName=====%s,uuid=====%d", strName.c_str(), struuid);
}
最后在一个文件中直接调用CCallLua::getInstance()->XXX() 这样我们就能成功的调用了lua中的变量、函数了。
注意:lua方法中函数、变量等必须是全局的
2017-4-14 1:31:50
写于苏州