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

C/C++读取Lua中的变量及调用Lua函数

2016-07-06 18:16 411 查看
config.lua

print('--In Lua--')

local a = -10
print('--math.abs(a)--',math.abs(a))

width = 1080
height = 720

name = "iphone"
numStr = 12345

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


//
//  LuaEngine.h
//  LuaAndCpp
//

#ifndef __LuaAndCpp__LuaEngine__
#define __LuaAndCpp__LuaEngine__

#include <stdio.h>
#include <iostream>
#include "lua.hpp"

class LuaEngine {

public:

~LuaEngine();

static LuaEngine* getInstance();

static void destroyInstance();

// 读取Lua中的变量
void getConfigWidthAndHeight(int* width,int* height);
// 调用Lua中的函数 无参无返回值
void callLuaFunction1();
// 调用Lua中的函数 无返回值 有一个参数
void callLuaFunction2();
// 调用Lua中的函数 无参数 有一个返回值
void callLuaFunction3();
// 调用Lua中的函数 有一个参数 有一个返回值
void callLuaFunction4();
// 调用Lua中的函数 有一个参数 有多个返回值
void callLuaFunction5();
// 调用Lua中的函数 有多个参数 有一个返回值
void callLuaFunction6();
private:

LuaEngine():m_pLuaState(NULL){};

void init();

std::string getFilePath(std::string fileName);

lua_State* m_pLuaState;
};

#endif /* defined(__LuaAndCpp__LuaEngine__) */


//
//  LuaEngine.cpp
//  LuaAndCpp
//

#include "LuaEngine.h"

LuaEngine::~LuaEngine(){
lua_close(m_pLuaState);
}

static LuaEngine* LuaEngine_instance = NULL;

LuaEngine* LuaEngine::getInstance(){
if (LuaEngine_instance == NULL) {
LuaEngine_instance = new LuaEngine();
LuaEngine_instance->init();
}
return LuaEngine_instance;
}

void LuaEngine::destroyInstance(){
if (LuaEngine_instance) {
delete LuaEngine_instance;
LuaEngine_instance = NULL;
}
}

void LuaEngine::init(){
m_pLuaState = luaL_newstate();
luaL_openlibs(m_pLuaState);

luaL_dofile(m_pLuaState, this->getFilePath("config.lua").c_str());
//    lua_pcall(m_pLuaState, 0, 0, 0);
}

std::string LuaEngine::getFilePath(std::string fileName){
return "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/scripts/" + fileName ;
}

void LuaEngine::getConfigWidthAndHeight(int* pWidth,int* pHeight){
// 获取Lua文件中的全局变量
lua_getglobal(m_pLuaState, "width");
// -1 栈顶位置 lua_isnumber 检测是否能转换成数字类型
if (lua_isnumber(m_pLuaState, -1)) {
int width = (int)lua_tonumber(m_pLuaState, -1);
std::cout << "width = " << width << std::endl;
*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);
std::cout << "height = " << height << std::endl;
*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);
std::cout << "name = " << str << std::endl;
}
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);
std::cout << "numStr = " << str << std::endl;
}
lua_pop(m_pLuaState, 1);
}

// 调用Lua中的函数 无参无返回值
void LuaEngine::callLuaFunction1(){
lua_getglobal(m_pLuaState, "test1");
// 0 参数 0 返回值
int rel = lua_pcall(m_pLuaState, 0, 0, -1);
if (rel == -1) {
std::cout << "callLuaFunction1 error \n";
}
}

// 调用Lua中的函数 无返回值 有一个参数
void LuaEngine::callLuaFunction2(){
lua_getglobal(m_pLuaState, "test2");
lua_pushstring(m_pLuaState, "调用了function test2");
int rel = lua_pcall(m_pLuaState, 1, 0, -1);
if (rel == -1) {
std::cout << "callLuaFunction2 error \n";
}
}

// 调用Lua中的函数 无参数 有一个返回值
void LuaEngine::callLuaFunction3(){
lua_getglobal(m_pLuaState, "test3");
int rel = lua_pcall(m_pLuaState, 0, 1, -1);
if (rel == -1) {
std::cout << "callLuaFunction3 error \n";
}
char* str = (char*)lua_tostring(m_pLuaState, -1);
std::cout << "lua function test3 返回值 -- " << str << std::endl;
}

// 调用Lua中的函数 有一个参数 有一个返回值
void LuaEngine::callLuaFunction4(){
lua_getglobal(m_pLuaState, "test4");
// 传参
lua_pushnumber(m_pLuaState, 10);
int rel = lua_pcall(m_pLuaState, 1, 1, -1);
if (rel == -1) {
std::cout << "callLuaFunction4 error \n";
}
int num = (int)lua_tonumber(m_pLuaState, -1);
std::cout << "lua function test4 返回值 -- " << num << std::endl;
}

// 调用Lua中的函数 有一个参数 有多个返回值
void LuaEngine::callLuaFunction5(){
lua_getglobal(m_pLuaState, "test5");
lua_pushnumber(m_pLuaState, 10);
int rel = lua_pcall(m_pLuaState, 1, 2, -1);
if (rel == -1) {
std::cout << "callLuaFunction5 error \n";
}
int t = (int)lua_tonumber(m_pLuaState, -2);
int var = (int)lua_tonumber(m_pLuaState, -1);
std::cout << "lua function test5 返回值 -- t , var  " << t << " " << var << std::endl;
}

// 调用Lua中的函数 有多个参数 有一个返回值
void LuaEngine::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) {
std::cout << "callLuaFunction6 error \n";
}
if (lua_isnumber(m_pLuaState, -1)) {
int num = (int)lua_tonumber(m_pLuaState, -1);
std::cout << "x+y== " << num << std::endl;
lua_pop(m_pLuaState, 1);
}
}


//
//  main.cpp
//  LuaAndCpp
//

#include <iostream>

#include "LuaEngine.h"

int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";

LuaEngine* luaEngine = LuaEngine::getInstance();

int width = 0;
int height = 0;

luaEngine->getConfigWidthAndHeight(&width,&height);

std::cout << "getConfigWidthAndHeight = " << width << " " << height << std::endl;

luaEngine->callLuaFunction1();

luaEngine->callLuaFunction2();

luaEngine->callLuaFunction3();

luaEngine->callLuaFunction4();

luaEngine->callLuaFunction5();

luaEngine->callLuaFunction6();

LuaEngine::destroyInstance();

return 0;
}


运行结果

Hello, World!
--In Lua--
--math.abs(a)-- 10
width = 1080
height = 720
name = iphone
numStr = 12345
getConfigWidthAndHeight = 1080 720
--function test1--
--function test2--
--var-- 调用了function test2
--function test3--
lua function test3 返回值 -- 调用了lua function test3
--function test4--
lua function test4 返回值 -- 20
--function test5--
lua function test5 返回值 -- t , var  10 110
--function test6--
--x--y--    10  13
x+y== 23
Program ended with exit code: 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua