luabind和c++相互调用

来源:互联网 发布:中国好声音网络歌手 编辑:程序博客网 时间:2024/04/26 06:25
先上代码:
#include "stdafx.h"#include<iostream>#include "luabind\luabind.hpp"extern "C"{#include "lua.h"#include "lualib.h"#include "lauxlib.h"}using namespace std;bool LoadScript(lua_State *L,const string& fname){    if (luaL_dofile(L,fname.c_str()))    {        cerr<<lua_tostring(L,-1)<<endl;        return false;    }     return true;}void testFunc(int k){    cout<<"hello there, i am a cpp fun"<<endl;    cout<<"input num:="<<k<<endl;}class NumberPrinter {public:    NumberPrinter(int number) :      m_number(number) {}      void print() {          cout << m_number << endl;      }private:    int m_number;};int _tmain(int argc, _TCHAR* argv[]){    using namespace luabind;    lua_State* L = luaL_newstate();    module(L, "cppapi")        [            def("testFunc", (void(*)(int))testFunc)        ];    // 使用LuaBind导出NumberPrinter类    luaopen_base(L);    luabind::open(L);    luabind::module(L)         [            luabind::class_<NumberPrinter>("NumberPrinter")            .def(luabind::constructor<int>())            .def("print", &NumberPrinter::print)        ];        luaL_openlibs(L);    LoadScript(L,"test.lua");    try{         int add_ret = luabind::call_function<int>(L,"add",10,4);         int call_global = luabind::object_cast<int>(luabind::globals(L)["nGlobal"]);         string strGlobal = luabind::object_cast<string>(luabind::globals(L)["strGlobal"]) ;         luabind::object lua_object = luabind::globals(L)["t"];         //--2种办法load table         string strName = luabind::object_cast<string>(lua_object["name"]);         int iAge = luabind::object_cast<int>(lua_object["age"]);         string strElse = luabind::object_cast<string>(lua_object["desc"]);    }    catch(luabind::error& e)    {        cout<<e.what()<<endl;        printf("AI throw error: err_msg[%s]", lua_tostring(L, -1));        return false;    }        lua_close(L);    return 0;}
 

其中test.lua的代码如下:
Print2000 = NumberPrinter(2000)
Print2000:print()

nGlobal = 10 --一个全局的整形变量
strGlobal = "hello i am in lua" --一个全局的字符串变量
--一个返回值为int类型的函数
function add(a, b)
    return a+b
end
--一个返回值为string类型的函数
function strEcho(a)
    print(a)
    return 'haha i h

ave print your input param'
end

cppapi.testFunc(10) --调用c++暴露的一个测试函数
t={name='ettan', age=23, desc='正值花季年龄'}

 

运行结果为:



这上面luabind调用c++函数的实例:调用testFunc函数;也有c++调用lua的代码,具体的见代码。
此代码在我的vs2010上面调试通过,前提是必须配好环境如:添加依赖库luabind.debug.lib;lua.debug.lib
添加依赖库路径。

特别注意:

首先Lua是“动态编译的脚本语言”,而loadfile只是把源文件加载到内存中,还少了“编译”这一步,可以用“luaL_dofile(L,"test.lua");”来替换,它既加载又编译。替换之后执行应该就没有问题了。

但是还没完,luaL_dofile 实际上是个宏:

 

  1. #define luaL_dofile(L, fn) \ 
  2. (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

 

LUA_MULTRET也是宏定义,值为-1,表示函数有多个返回值(Lua规则,pil 24.2--堆栈)。

扩展开来就是以下两句:

  1. luaL_loadfile(L, fn);
  2. lua_pcall(L, 0, LUA_MULTRET, 0);

pcall以上述参数执行的时候,会把加载到内存中的源程序编译成可以用于执行的2进制代码,并将全局变量压栈(在Lua中,函数也是变量,pil 2.5 -- Functions,毕竟函数名和函数体是不同的2个东西)。就跟PE文件格式里的Section一样(PE文件就是Windows3.1之后的.exe/.dll文件)。当然如果你不知道什么PE文件也没关系--我只是打个比方--就当成VS2005编译代码时生成的.obj文件。

 

 

虽然实际使用中99%的情况都是直接使用dofile,但是我想将该问题提出来说可以更加直观的理解“动态编译”。

参照:
代码和文字大部分出自 1.   http://blog.csdn.net/caoyanting007/article/details/5709820
                                  2.   http://mobile.51cto.com/iphone-285654.htm

转载:点击打开链接
0 0