lua和c的交互包括:c訪問lua的變量、c訪問lua的table、c調(diào)用lua的方法、lua調(diào)用c的函數(shù)
參考鏈接:笨木頭
一、Lua堆棧
要理解Lua和C++交互,首先要理解Lua堆棧。
簡單來說,Lua和C/C++語言通信的主要方法是一個無處不在的虛擬棧。棧的特點是先進后出。
在Lua中,Lua堆棧就是一個struct,堆棧索引的方式可是是正數(shù)也可以是負數(shù)。
- 正數(shù)索引,棧底是1,然后一直到棧頂是逐漸+1
- 負數(shù)索引,棧底是-9,然后一直到棧頂是逐漸+1
二、訪問lua變量
- C++想獲取Lua的myName字符串的值,所以它把myName放到Lua堆棧(棧頂),以便Lua能看到
- Lua從堆棧(棧頂)中獲取myName,此時棧頂再次變?yōu)榭?/li>
- Lua拿著這個myName去Lua全局表查找myName對應(yīng)的字符串
- 全局表返回一個字符串”beauty girl”
- Lua把取得的“beauty girl”字符串放到堆棧(棧頂)
- C++可以從Lua堆棧中取得“beauty girl”
lua和c的交互包括:c訪問lua的變量、c訪問lua的table、c調(diào)用lua的方法、lua調(diào)用c的函數(shù)
參考鏈接:笨木頭
一、Lua堆棧
要理解Lua和C++交互,首先要理解Lua堆棧。
簡單來說,Lua和C/C++語言通信的主要方法是一個無處不在的虛擬棧。棧的特點是先進后出。
在Lua中,Lua堆棧就是一個struct,堆棧索引的方式可是是正數(shù)也可以是負數(shù)。
- 正數(shù)索引,棧底是1,然后一直到棧頂是逐漸+1
- 負數(shù)索引,棧底是-9,然后一直到棧頂是逐漸+1
二、訪問lua變量
- C++想獲取Lua的myName字符串的值,所以它把myName放到Lua堆棧(棧頂),以便Lua能看到
- Lua從堆棧(棧頂)中獲取myName,此時棧頂再次變?yōu)榭?/li>
- Lua拿著這個myName去Lua全局表查找myName對應(yīng)的字符串
- 全局表返回一個字符串”beauty girl”
- Lua把取得的“beauty girl”字符串放到堆棧(棧頂)
-
C++可以從Lua堆棧中取得“beauty girl”
image
三、訪問table變量
- lua_getglobal和lua_gettable是用來取得table相關(guān)的數(shù)據(jù)的
- lua_gettable函數(shù)會從棧頂取得一個值,然后根據(jù)這個值去table中尋找對應(yīng)的值,最后把找到的值放到棧頂。
- lua_pushstring()函數(shù)可以把C++中的字符串存放到Lua的棧里;
然后再用lua_gettable()取執(zhí)行前面所說的步驟,lua_gettable的第二個參數(shù)是指定的table變量在棧中的索引。
lua_gettable倒底做了什么事情?
首先,我們來解釋一下lua_gettable的第二個參數(shù),-2是什么意思,-2就是剛剛helloTable變量在棧中的索引。
然后,Lua會去取得棧頂?shù)闹担ㄖ暗臈m斒恰眓ame”),然后拿著這個值去helloTable變量中尋找對應(yīng)的值。 -3就是對應(yīng)的id。-1對應(yīng)的是那個table
四、調(diào)用lua方法
- 執(zhí)行腳本(旁白:我就知道你會說廢話。。。)
- 將printHello函數(shù)放到棧中:lua_getglobal(pL, “printHello”) 。(旁白:看吧,我就知道~!)
- printHello有2個參數(shù),我們要把參數(shù)傳遞給lua,所以2個參數(shù)都要放到棧里。
- 第2和第3步已經(jīng)把函數(shù)所需要的數(shù)據(jù)都放到棧里了,接下來只要告訴lua去棧里取數(shù)據(jù),執(zhí)行函數(shù)~! 調(diào)用lua_call即可,注釋已經(jīng)很詳細了,這里就不重復(fù)了。
lua腳本:
function Communicate(name)
return ("Hello "..name..", I`m in Lua"), "test_1", "test_2";
end
str = "I am so cool"
tbl = {name = "name name name !!!", id = "id id id 20114442"}
function add(a,b)
return a + b
end
print(mytestlib.printHello())
print(mytestlib.foo(99))
print(mytestlib.add(1,5,3,6))
c代碼:
#include <iostream>
#include <string.h>
#include "mLualib.hpp"
using namespace std;
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
//要想注冊進lua,函數(shù)的定義為 typedef int (*lua_CFunction)(lua_State* L)
extern "C" int printHello(lua_State * l)
{
lua_pushstring(l,"hello lua");
lua_pushinteger(l, 100);
//返回值代表向棧內(nèi)壓入的元素個數(shù)
return 2;
}
extern "C" int foo(lua_State * l)
{
//獲得Lua傳遞過來的參數(shù)個數(shù)
int n = lua_gettop(l);
if(n != 0)
{
//獲得第一個參數(shù)
int i = lua_tonumber(l,1);
//將傳遞過來的參數(shù)加一以后最為返回值傳遞回去
lua_pushnumber(l,i+1);
return 1;
}
return 0;
}
//相加
extern "C" int add(lua_State * l)
{
//獲得Lua傳遞過來的參數(shù)個數(shù)
int n = lua_gettop(l);
int sum = 0;
for (int i=0;i<n;i++)
{
sum += lua_tonumber(l,i+1);
}
if(n!=0)
{
lua_pushnumber(l,sum);
return 1;
}
return 0;
}
//把需要用到的函數(shù)都放到注冊表中,統(tǒng)一進行注冊
const luaL_Reg myLib[]=
{
{"printHello",printHello},
{"foo",foo},
{"add",add},
{nullptr,nullptr}
};
int main(int argc, const char * argv[]) {
//1.創(chuàng)建一個state
lua_State *L = luaL_newstate();
luaL_openlibs(L); /* opens the standard libraries */
if (L == NULL) {
return 0;
}
#pragma test_1
// //2.入棧操作
// lua_pushstring(L, "Hello World~");
// //3.取值操作
// if (lua_isstring(L, 1)) { //判斷是否可以轉(zhuǎn)為string
// cout << lua_tostring(L, 1) << endl; //轉(zhuǎn)為string并返回
// }
#pragma test_2 加載lua文件
//加載lua文件
int bRet = luaL_loadfile(L, "/Users/zwf/Documents/CppTest/LuaCTest/LuaCTest/test.lua");
if (bRet) {
cout<<"load file error!"<<endl;
return 0;
}
//運行l(wèi)ua文件
bRet = lua_pcall(L, 0, 0, 0);
if (bRet)
{
cout << "pcall error" << endl;
return 0;
}
//#pragma 調(diào)用函數(shù)
// lua_getglobal(L, "Communicate"); // 獲取函數(shù),壓入棧中
// lua_pushstring(L, "Zack"); // 壓入?yún)?shù)
// int iRet = lua_pcall(L, 1, 3, 0); //調(diào)用函數(shù),調(diào)用完成以后,會將返回值壓入棧中,第一個1表示參數(shù)個數(shù),第二個1表示返回結(jié)果個數(shù)。
// if (iRet) {
// const char* pErrorMsg = lua_tostring(L, -1);
// cout << pErrorMsg << endl;
// lua_close(L);
// return 0;
// }
// //第一個返回值
// if (lua_isstring(L, -1)) {
// string Result = lua_tostring(L, -1);
// cout<<"lua 中的函數(shù)返回值為: "<< Result.c_str() <<endl;
// }
// if (lua_isstring(L, -2)) {
// string Result = lua_tostring(L, -2);
// cout<<"lua 中的第二個返回值:"<< Result.c_str() <<endl;
// }
// if (lua_isstring(L, -3)) {
// string Result = lua_tostring(L, -3);
// cout<<"lua 中的第三個返回值:"<< Result.c_str() <<endl;
// }
//#pragma 調(diào)用函數(shù)
// lua_getglobal(L, "add");
// lua_pushinteger(L, 10);
// lua_pushinteger(L, 20);
// int ret = lua_pcall(L, 2, 1, 0);
// if (ret) {
// const char* pErrorMsg = lua_tostring(L, -1);
// cout<< "pErrorMsg: "<<pErrorMsg<<endl;
// lua_close(L);
// return 0;
// }
// //讀取返回值
// if (lua_isinteger(L, -1)) {
// int result = lua_tonumber(L, -1);
// cout<< "lua 中add的函數(shù)返回值:" << result<<endl;
// }
#pragma 讀取變量
lua_getglobal(L, "str");
if (lua_isstring(L, -1)) {
string str = lua_tostring(L, -1);
cout<<"string is: "<<str<<endl;
}
//
//#pragma 讀取table
// lua_getglobal(L, "tbl");
//
// lua_pushstring(L, "name");
// lua_gettable(L, -2);
// if (lua_isstring(L, -1)) {
// string name = lua_tostring(L, -1);
// cout<<"name is: "<< name.c_str() <<endl;
// }
//
// lua_pushstring(L, "id");
// lua_gettable(L, -3);
// if (lua_isinteger(L, -1)) {
// int id = lua_tonumber(L, -1);
// cout << "id is: " << id << endl;
// }
//
#pragma lua 調(diào)用C++方法
lua_newtable(L);
luaL_setfuncs(L, myLib, 0); //lua_newtable和luaL_setfuncs是個連招 就是為了把c函數(shù)注冊到lua環(huán)境中的
lua_setglobal(L, "mytestlib"); //Pops a value from the stack and sets it as the new value of global name.
int doRet = luaL_dofile(L, "/Users/zwf/Documents/CppTest/LuaCTest/LuaCTest/test.lua");
if (doRet) {
cout<<"dofile error! " << endl;
lua_close(L);
return 0;
}
//4.關(guān)閉state
lua_close(L);
return 0;
}