lua和c的初始

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ù)。

  1. 正數(shù)索引,棧底是1,然后一直到棧頂是逐漸+1
  2. 負數(shù)索引,棧底是-9,然后一直到棧頂是逐漸+1
二、訪問lua變量
  1. C++想獲取Lua的myName字符串的值,所以它把myName放到Lua堆棧(棧頂),以便Lua能看到
  2. Lua從堆棧(棧頂)中獲取myName,此時棧頂再次變?yōu)榭?/li>
  3. Lua拿著這個myName去Lua全局表查找myName對應(yīng)的字符串
  4. 全局表返回一個字符串”beauty girl”
  5. Lua把取得的“beauty girl”字符串放到堆棧(棧頂)
  6. 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ù)。

  1. 正數(shù)索引,棧底是1,然后一直到棧頂是逐漸+1
  2. 負數(shù)索引,棧底是-9,然后一直到棧頂是逐漸+1
二、訪問lua變量
  1. C++想獲取Lua的myName字符串的值,所以它把myName放到Lua堆棧(棧頂),以便Lua能看到
  2. Lua從堆棧(棧頂)中獲取myName,此時棧頂再次變?yōu)榭?/li>
  3. Lua拿著這個myName去Lua全局表查找myName對應(yīng)的字符串
  4. 全局表返回一個字符串”beauty girl”
  5. Lua把取得的“beauty girl”字符串放到堆棧(棧頂)
  6. C++可以從Lua堆棧中取得“beauty girl”


    image
三、訪問table變量
  1. lua_getglobal和lua_gettable是用來取得table相關(guān)的數(shù)據(jù)的
  2. lua_gettable函數(shù)會從棧頂取得一個值,然后根據(jù)這個值去table中尋找對應(yīng)的值,最后把找到的值放到棧頂。
  3. 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方法
  1. 執(zhí)行腳本(旁白:我就知道你會說廢話。。。)
  2. 將printHello函數(shù)放到棧中:lua_getglobal(pL, “printHello”) 。(旁白:看吧,我就知道~!)
  3. printHello有2個參數(shù),我們要把參數(shù)傳遞給lua,所以2個參數(shù)都要放到棧里。
  4. 第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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 當在Lua和C之間交換數(shù)據(jù)時主要的問題是自動回收與手動回收內(nèi)存管理的不一致。因此,Lua 用一個抽象的棧在Lua與...
    luffier閱讀 2,695評論 0 3
  • 1. 寫在前面 很多時候我們都需要借助一些腳本語言來為我們實現(xiàn)一些動態(tài)的配置,那么就會涉及到如何讓腳本語言跟原生語...
    杰嗒嗒的阿杰閱讀 3,454評論 9 31
  • 第一篇 語言 第0章 序言 Lua僅讓你用少量的代碼解決關(guān)鍵問題。 Lua所提供的機制是C不擅長的:高級語言,動態(tài)...
    testfor閱讀 2,719評論 1 7
  • lua C api PS:這里是默認我已經(jīng)學(xué)完了lua腳本的基本知識(包括table,元表,函數(shù),基本庫, 文件i...
    綠箭ML閱讀 3,488評論 0 1
  • 時光太長,長得像一幅怎樣都鋪展不完的畫卷,在這一幅畫卷里,有些事如細水長流,讓人一品再品,也有一些事,如蜻蜓點水,...
    淑窈閱讀 961評論 0 1