Lua 與 C 擴充 -透過 C++ 執行檔的各種處理
環境
windows 11 64bit
Visual Studio 2022
Lua 5.4.6
首先做好基本準備
main.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#include <K:\Lua\546\lib17\include\lua.hpp>
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib,"K:\\Lua\\546\\lib17\\x64\\Debug\\lua54.lib")
#else
#pragma comment(lib,"K:\\Lua\\546\\lib17\\x64\\Release\\lua54.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib,"K:\\Lua\\546\\lib17\\x86\\Debug\\lua54.lib")
#else
#pragma comment(lib,"K:\\Lua\\546\\lib17\\x86\\Release\\lua54.lib")
#endif
#endif
lua_State* LuaOpen()
{
lua_State* pLua = luaL_newstate();
if (!pLua)
{
printf("Failed to open Lua\n");
return 0;
}
luaL_openlibs(pLua);
return pLua;
}
int main()
{
//LuaTest01(); //單純執行 lua 檔
//LuaTest02(); //傳去 lua 做加法
//LuaTest03(); //Lua 呼叫 C++ Function
//LuaTest04(); //Lua 呼叫 C++ Function
system("pause");
return 0;
}
1. 單純執行 lua 檔
void LuaTest01() //單純執行 lua 檔
{
// 開啟Lua
lua_State* pLua = LuaOpen();
if (!pLua)
return;
// 執行整個Lua檔
luaL_dofile(pLua, "hello.lua");
lua_close(pLua);
return;
}
hello.lua
--hello.lua
print("Hello world! Hello Lua!")
執行結果
2. 傳去 lua 做加法
int add(lua_State *pLua, int x, int y)
{
int result;
lua_getglobal(pLua, "luaadd");
lua_pushnumber(pLua, x);
lua_pushnumber(pLua, y);
lua_call(pLua, 2, 1); // 2 個傳入值和 1 個回傳值
result = lua_tointeger(pLua, -1);
/*上面這行code有個地方要注意,
就是第二個參數(index), index 1 and -n stand for the first element in the stack,
index n and -1 stand for the last element in the stack,
所以上面這行就是把最後放進去的data拿出來, 也就是要回傳的值*/
lua_pop(pLua, 1); //pop returned value
return result;
}
void LuaTest02() //傳去 lua 做加法
{
// 開啟Lua
lua_State* pLua = LuaOpen();
if (!pLua)
return;
luaL_dofile(pLua, "add.lua");
//呼叫lua的函式
int sum = add(pLua, 4, 3);
printf("The sum is %d\n", sum);
lua_close(pLua);
}
add.lua
function luaadd(x, y)
return x+y
end
執行結果
3. Lua 呼叫 C++ Function
int SayHello(lua_State* pLua)
{
const char* Name = lua_tostring(pLua, 1); // 取得堆疊底層第一個參數的Name
char Hello[128] = "Hello! ";
strcat_s(Hello, Name);
lua_pushstring(pLua, Hello); // 推入回傳字串進堆疊
return 1; // 告訴Lua我們有一個回傳值
}
void LuaTest03() //Lua 呼叫 C++ Function
{
// 開啟Lua
lua_State* pLua = LuaOpen();
if (!pLua)
return;
lua_register(pLua, "SayHello", SayHello);
// 執行整個Lua檔
luaL_dofile(pLua, "hello2.lua");
lua_close(pLua);
}
hello2.lua
-- hello2.lua
print(SayHello("Charlie"))
執行結果
4. Lua 呼叫 C++ Function
int sum_and_minus(lua_State* pLua)
{
//get the total number of arguments
int num_args = lua_gettop(pLua);
double sum = 0;
double minus = 0;
//index 1 indicates the first argument, index num_args indicates the last argument
for (int i = 1; i <= num_args; i++)
sum += lua_tonumber(pLua, i);
//這裡的作法是0-3-5 所以是 -8
for (int i = 1; i <= num_args; i++)
minus -= lua_tonumber(pLua, i); //push the result into the stack
lua_pushnumber(pLua, sum);
lua_pushnumber(pLua, minus);
return 2; //return the total number of the results
}
void LuaTest04() //Lua 呼叫 C++ Function
{
lua_State* pLua = LuaOpen();
if (!pLua)
return;
lua_register(pLua, "sum_and_minus", sum_and_minus);
luaL_dofile(pLua, "func.lua");
lua_close(pLua);
}
func.lua
sum,minus = sum_and_minus(3,5)
print("Sum = ", sum)
print("Minus = ", minus)
執行結果