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
- 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
//// 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__) */
- 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
//// 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); }}
- 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
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
//// 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;}
- 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
运行结果
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21