LuaIntf-第一步

LuaInft   https://github.com/SteveKChiu/lua-intf

ReadME .MD https://github.com/SteveKChiu/lua-intf/blob/master/README.md

一些基础信息,作者在github上面描述很清楚

这个lib 提供了cpp和lua交互 解决方案

1.运行执行lua文件 函数

	lua_State *l = luaL_newstate();
	luaL_openlibs(l);

	LuaIntf:: LuaContext ctx(l);
	ctx.doFile("1.lua");

	LuaIntf::LuaRef func(l, "func");
	func(1000);

2.导出class 


#include "LuaIntf/LuaIntf.h"

using namespace LuaIntf;

class Test
{
public:
	void Print()
	{
		cout << __FUNCTION__ << this->GetValue() << endl;
	}

	void SetValue(int v) { this->_value = v; }//setter

	int GetValue() { return this->_value; };//getter

	Test(string s) {}
	Test(void) {}
private:
	int _value = 0; // inner value
};

int main(int argc, char *argv[])
{
	lua_State *l = luaL_newstate();
	luaL_openlibs(l);

	LuaIntf::LuaBinding(l).beginClass<Test>("Test")
		.addConstructor(LUA_ARGS(_opt<std::string>))
		.addConstructor(LUA_ARGS())
		.addFunction("Print", &Test::Print)
		.addProperty("v", &Test::GetValue, &Test::SetValue) // 绑定getter 和setter,lua变量名为v
		.endClass();

	try
	{
		LuaIntf::LuaContext ctx(l);
		ctx.doFile("1.lua");

		LuaIntf::LuaRef func(l, "func");
		func(1000);
	}

	catch (LuaException  e)
	{
		cout << e.what();
	}
	_CrtDumpMemoryLeaks();
	system("pause");
	return 0;
}


lua代码

print("567856");


  function func(x)
    print(x);
end




local xxx= Test();
 xxx:Print();
 xxx.v=3;
 xxx:Print();



 

3.注册模块

比上面的代码多了:


	LuaIntf::LuaBinding(l).beginModule("cpp") /// cpp 模块名字
		.addFunction("log", &log1)
		.addFunction("FUNCNAME", [=] 
	{
		log1(__FUNCTION__);
	})
		.endModule();


lua代码


cpp.log("haha");
cpp.FUNCNAME();

 

 

 

 

 

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/825686

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值