一. lua調用C++ 在lua中是以函數指針的形式調用函數, 并且所有的函數指針都必須滿足如下此種類型: typedef int (*lua_CFunction) (lua_State *L); 也就是說, 偶們在C++中定義函數時必須以lua_State為參數, 以int為返回值才能被Lua所調用. 但是不要忘記了, 偶們的lua_State是支持棧的, 所以通過棧可以傳遞無窮個參數, 大小只受內存大小限制. 而返回的int值也只是指返回值的個數真正的返回值都存儲在lua_State的棧中. 偶們通常的做法是做一個wrapper, 把所有需要調用的函數都wrap一下, 這樣就可以調用任意的函數了.
- #include<iostream>
- using namespace std;
- #include<stdio.h>
- extern "C" {
- #include <lua.h>
- #include <lualib.h>
- #include <lauxlib.h>
- }
- //#pragma comment(lib, "lua5.1.lib")
- lua_State* L;
- static int average(lua_State *L)
- {
- //返回棧中元素的個數
- int n = lua_gettop(L);
- double sum = 0;
- int i;
- for (i = 1; i <= n; i++)
- {
- if (!lua_isnumber(L, i))
- {
- lua_pushstring(L, "Incorrect argument to 'average'");
- lua_error(L);
- }
- sum += lua_tonumber(L, i);
- }
- /* push the average */
- lua_pushnumber(L, sum / n);
- /* push the sum */
- lua_pushnumber(L, sum);
-
- /* return the number of results */
- return 2;
- }
- int main (int argc,char*argv[])
- {
- /* initialize Lua */
- L = lua_open();
- /* load Lua libraries */
- luaL_openlibs(L);
- /* register our function */
- lua_register(L, "average", average);
- /* run the script */
- luaL_dofile(L, "e15.lua");
-
- lua_getglobal(L,"avg");
- cout<<"avg is:"<<lua_tointeger(L,-1)<<endl;
- lua_pop(L,1);
- lua_getglobal(L,"sum");
- cout<<"sum is:"<<lua_tointeger(L,-1)<<endl;
- /* cleanup Lua */
- lua_close(L);
-
- return 0;
- }
- //程序
- //*lua_gettop()的作用是返回棧頂元素的序號. 由于Lua的棧是從1開始編號的,
- // 所以棧頂元素的序號也相當于棧中的元素個數. 在這里, 棧中元素的個數就
- // 是傳入的參數個數.
- //* for循環計算所有傳入參數的總和. 這里用到了數值轉換lua_tonumber().
- //* 然后偶們用lua_pushnumber()把平均值和總和push到棧中.
- //* 最后, 偶們返回2, 表示有兩個返回值.
- //* 雖然在C++中定義了average()函數, 但Lua程序并不知道, 所以需
- // 要在main函數中加入
- // // register our function
- // lua_register(L, "average", average);
- // 這兩行的作用就是告訴e15.lua有average()這樣一個函數.
- //* 這個程序可以存成cpp也可以存成c, 如果以.c為擴展名就不需要加extern "C"
- //
- //編譯的方法偶們上次說過了, 方法相同.
- //e15.lua執行的方法只能用上例中的C++中執行, 而不能用命令行方式執行.*/
腳本為 avg, sum = average(10, 20, 30, 40, 50) print("The average is ", avg) print("The sum is ", sum) 二. C++調用lua - #include "stdafx.h"
- #include <stdio.h>
- extern "C" {
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- }
- /* Lua解釋器指針 */
- lua_State* L;
- int main ( int argc, char *argv[] )
- {
- /* 初始化Lua */
- L = lua_open();
- /* 載入Lua基本庫 */
- luaL_openlibs(L);
- /* 運行腳本 */
- luaL_dofile(L, "Lua1.lua");
- /* 清除Lua */
- lua_close(L);
- /* 暫停 */
- printf( "Press enter to exit…" );
- getchar();
- return 0;
- }
- /* A simple Lua interpreter. */
- #include <stdio.h>
- extern "C" {
- #include <lua.h>
- #include <lualib.h>
- #include <lauxlib.h>
- }
- #include <stdio.h>
- extern "C" { // 這是個C++程序, 所以要extern "C",
- // 因為lua的頭文件都是C格式的
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- }
- #pragma comment(lib, "lua5.1.lib")
- /* the Lua interpreter */
- lua_State* L;
- int luaadd ( int x, int y )
- {
- int sum;
- /* the function name */
- lua_getglobal(L, "add"); int nTop = lua_gettop(L); //得到棧的元素個數。棧頂的位置。
- /* the first argument */
- lua_pushnumber(L, x); nTop = lua_gettop(L);
- /* the second argument */
- lua_pushnumber(L, y); nTop = lua_gettop(L);
- /* call the function with 2
- arguments, return 1 result */
- lua_call(L, 2, 1); nTop = lua_gettop(L);
- /* get the result */
- sum = (int)lua_tonumber(L, -1); nTop = lua_gettop(L);
- /*清掉返回值*/
- lua_pop(L, 1); nTop = lua_gettop(L);
- /*取出腳本中的變量z的值*/
- lua_getglobal(L, "z"); nTop = lua_gettop(L);
- int z = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);
- lua_pop(L, 1); nTop = lua_gettop(L);
-
- //沒調通
- /*lua_pushnumber(L, 4); nTop = lua_gettop(L);
- lua_setglobal(L, "r"); nTop = lua_gettop(L);
- int r = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);*/
- return sum;
- }
- int main ( int argc, char *argv[] )
- {
- int sum;
- /* initialize Lua */
- L = lua_open();
- /* load Lua base libraries */
- //lua_baselibopen(L);
- /* load the script */
- luaL_dofile(L, "e12.lua");
- /* call the add function */
- sum = luaadd( 10, 15 );
- /* print the result */
- printf( "The sum is %d", sum );
- /* cleanup Lua */
- lua_close(L);
- return 0;
- }
- /*程序說明:
- main中過程偶們上次已經說過了, 所以這次只說說luaadd的過程
- * 首先用lua_getglobal()把add函數壓棧
- * 然后用lua_pushnumber()依次把x,y壓棧
- * 然后調用lua_call(), 并且告訴程序偶們有兩個參數一個返回值
- * 接著偶們從棧頂取回返回值, 用lua_tonumber()
- * 最后偶們用lua_pop()把返回值清掉
- */
腳本為: -- add two numbers function add ( x, y ) return x + y + 2 end z = 6 轉自: http://blog.csdn.net/sndaxdrs/article/details/6230999
|