LuaIntf-第一步「建议收藏」

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108693.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号