• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            兔子的技術博客

            兔子

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              202 Posts :: 0 Stories :: 43 Comments :: 0 Trackbacks

            留言簿(10)

            最新評論

            閱讀排行榜

            評論排行榜

            一.   lua調用C++

                  在lua中是以函數指針的形式調用函數, 并且所有的函數指針都必須滿足如下此種類型:
            typedef int (*lua_CFunction) (lua_State *L);  
            也就是說, 偶們在C++中定義函數時必須以lua_State為參數, 以int為返回值才能被Lua所調用. 但是不要忘記了, 偶們的lua_State是支持棧的, 所以通過棧可以傳遞無窮個參數, 大小只受內存大小限制. 而返回的int值也只是指返回值的個數真正的返回值都存儲在
            lua_State的棧中. 偶們通常的做法是做一個wrapper, 把所有需要調用的函數都wrap一下, 這樣就可以調用任意的函數了.

            1. #include<iostream>  
            2. using namespace std;  
            3. #include<stdio.h>  
            4. extern "C" {  
            5. #include <lua.h>  
            6. #include <lualib.h>  
            7. #include <lauxlib.h>  
            8. }  
            9. //#pragma comment(lib, "lua5.1.lib")  
            10. lua_State* L;  
            11. static int average(lua_State *L)  
            12. {  
            13.     //返回棧中元素的個數  
            14.     int n = lua_gettop(L);  
            15.     double sum = 0;  
            16.     int i;  
            17.     for (i = 1; i <= n; i++)  
            18.     {  
            19.         if (!lua_isnumber(L, i))   
            20.         {  
            21.             lua_pushstring(L, "Incorrect argument to 'average'");  
            22.             lua_error(L);  
            23.         }  
            24.         sum += lua_tonumber(L, i);  
            25.     }  
            26.     /* push the average */  
            27.     lua_pushnumber(L, sum / n);  
            28.     /* push the sum */  
            29.     lua_pushnumber(L, sum);  
            30.       
            31.     /* return the number of results */  
            32.     return 2;  
            33. }  
            34. int main (int argc,char*argv[])  
            35. {  
            36.     /* initialize Lua */  
            37.     L = lua_open();  
            38.     /* load Lua libraries */  
            39.     luaL_openlibs(L);  
            40.     /* register our function */  
            41.     lua_register(L, "average", average);  
            42.     /* run the script */  
            43.     luaL_dofile(L, "e15.lua");  
            44.       
            45.     lua_getglobal(L,"avg");  
            46.     cout<<"avg is:"<<lua_tointeger(L,-1)<<endl;  
            47.     lua_pop(L,1);  
            48.     lua_getglobal(L,"sum");  
            49.     cout<<"sum is:"<<lua_tointeger(L,-1)<<endl;  
            50.     /* cleanup Lua */  
            51.     lua_close(L);  
            52.       
            53.     return 0;  
            54. }  
            55. //程序  
            56. //*lua_gettop()的作用是返回棧頂元素的序號. 由于Lua的棧是從1開始編號的,  
            57. // 所以棧頂元素的序號也相當于棧中的元素個數. 在這里, 棧中元素的個數就  
            58. // 是傳入的參數個數.  
            59. //* for循環計算所有傳入參數的總和. 這里用到了數值轉換lua_tonumber().  
            60. //* 然后偶們用lua_pushnumber()把平均值和總和push到棧中.  
            61. //* 最后, 偶們返回2, 表示有兩個返回值.  
            62. //* 雖然在C++中定義了average()函數, 但Lua程序并不知道, 所以需  
            63. //  要在main函數中加入  
            64. //     // register our function   
            65. //  lua_register(L, "average", average);  
            66. //  這兩行的作用就是告訴e15.lua有average()這樣一個函數.  
            67. //* 這個程序可以存成cpp也可以存成c, 如果以.c為擴展名就不需要加extern "C"  
            68. //       
            69. //編譯的方法偶們上次說過了, 方法相同.  
            70. //e15.lua執行的方法只能用上例中的C++中執行, 而不能用命令行方式執行.*/  

             

            腳本為

             

            avg, sum = average(10, 20, 30, 40, 50)

            print("The average is ", avg)

            print("The sum is ", sum)

             

             

             

            二.  C++調用lua

            1. #include "stdafx.h"  
            2. #include <stdio.h>  
            3. extern "C" {  
            4. #include "lua.h"  
            5. #include "lualib.h"  
            6. #include "lauxlib.h"  
            7. }  
            8. /* Lua解釋器指針 */  
            9. lua_State* L;  
            10. int main ( int argc, char *argv[] )  
            11. {  
            12.     /* 初始化Lua */  
            13.     L = lua_open();  
            14.     /* 載入Lua基本庫 */  
            15.     luaL_openlibs(L);  
            16.     /* 運行腳本 */  
            17.     luaL_dofile(L, "Lua1.lua");  
            18.     /* 清除Lua */  
            19.     lua_close(L);  
            20.     /* 暫停 */  
            21.     printf( "Press enter to exit…" );  
            22.     getchar();  
            23.     return 0;  
            24. }  

             

             

             

            1. /* A simple Lua interpreter. */   
            2. #include <stdio.h>   
            3. extern "C" {  
            4. #include <lua.h>   
            5. #include <lualib.h>  
            6. #include <lauxlib.h>  
            7. }  
            8. #include <stdio.h>  
            9. extern "C" { // 這是個C++程序, 所以要extern "C",  
            10.     // 因為lua的頭文件都是C格式的  
            11. #include "lua.h"  
            12. #include "lualib.h"  
            13. #include "lauxlib.h"  
            14. }  
            15. #pragma comment(lib, "lua5.1.lib")  
            16. /* the Lua interpreter */  
            17. lua_State* L;  
            18. int luaadd ( int x, int y )  
            19. {  
            20.     int sum;  
            21.     /* the function name */  
            22.     lua_getglobal(L, "add");        int nTop = lua_gettop(L); //得到棧的元素個數。棧頂的位置。  
            23.     /* the first argument */  
            24.     lua_pushnumber(L, x);           nTop = lua_gettop(L);  
            25.     /* the second argument */  
            26.     lua_pushnumber(L, y);           nTop = lua_gettop(L);  
            27.     /* call the function with 2 
            28.     arguments, return 1 result */  
            29.     lua_call(L, 2, 1);              nTop = lua_gettop(L);  
            30.     /* get the result */  
            31.     sum = (int)lua_tonumber(L, -1); nTop = lua_gettop(L);  
            32.     /*清掉返回值*/  
            33.     lua_pop(L, 1);                  nTop = lua_gettop(L);  
            34.     /*取出腳本中的變量z的值*/  
            35.     lua_getglobal(L, "z");          nTop = lua_gettop(L);  
            36.     int z = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);  
            37.     lua_pop(L, 1);                  nTop = lua_gettop(L);  
            38.       
            39.     //沒調通  
            40.     /*lua_pushnumber(L, 4);         nTop = lua_gettop(L); 
            41.     lua_setglobal(L, "r");          nTop = lua_gettop(L); 
            42.     int r = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);*/  
            43.     return sum;  
            44. }  
            45. int main ( int argc, char *argv[] )  
            46. {  
            47.     int sum;  
            48.     /* initialize Lua */  
            49.     L = lua_open();  
            50.     /* load Lua base libraries */  
            51.     //lua_baselibopen(L);  
            52.     /* load the script */  
            53.     luaL_dofile(L, "e12.lua");  
            54.     /* call the add function */  
            55.     sum = luaadd( 10, 15 );  
            56.     /* print the result */  
            57.     printf( "The sum is %d", sum );  
            58.     /* cleanup Lua */  
            59.     lua_close(L);  
            60.     return 0;  
            61. }  
            62. /*程序說明: 
            63. main中過程偶們上次已經說過了, 所以這次只說說luaadd的過程 
            64. * 首先用lua_getglobal()把add函數壓棧 
            65. * 然后用lua_pushnumber()依次把x,y壓棧 
            66. * 然后調用lua_call(), 并且告訴程序偶們有兩個參數一個返回值 
            67. * 接著偶們從棧頂取回返回值, 用lua_tonumber() 
            68. * 最后偶們用lua_pop()把返回值清掉 
            69. */  
             

             

            腳本為:

             

            -- add two numbers

            function add ( x, y )

            return x + y + 2

            end

             

            z = 6

             

            轉自:http://blog.csdn.net/sndaxdrs/article/details/6230999
            posted on 2013-04-17 16:53 會飛的兔子 閱讀(420) 評論(0)  編輯 收藏 引用 所屬分類: 非C++技術資料
            久久国产精品成人免费| 大香伊人久久精品一区二区| 国内精品久久久久久99| 热久久这里只有精品| 中文字幕无码久久精品青草| 久久久国产精华液| 91久久九九无码成人网站| 伊人久久大香线蕉AV一区二区| 欧洲成人午夜精品无码区久久 | 久久久高清免费视频| 人妻精品久久久久中文字幕一冢本| 久久免费视频观看| 久久婷婷国产剧情内射白浆| 秋霞久久国产精品电影院| 久久人人爽人人爽人人片AV麻烦| 国产欧美一区二区久久| 久久天天躁狠狠躁夜夜2020一| 色综合久久精品中文字幕首页| 久久天天躁夜夜躁狠狠| 久久国产精品一区| 久久夜色精品国产亚洲| 亚洲综合熟女久久久30p| 一级做a爰片久久毛片看看| 国产精品综合久久第一页| 久久久噜噜噜www成人网| 久久人妻少妇嫩草AV蜜桃| 久久激情五月丁香伊人| 国产69精品久久久久99尤物| 99国产欧美精品久久久蜜芽| 久久这里有精品| 日韩欧美亚洲综合久久影院Ds| 国内精品久久久久久麻豆| 大蕉久久伊人中文字幕| 久久这里只精品国产99热| 嫩草影院久久99| 久久精品亚洲乱码伦伦中文| 99热成人精品免费久久| 久久精品国产第一区二区| 久久婷婷五月综合色99啪ak| 亚洲国产精品无码久久九九 | 99久久国产宗和精品1上映|