https://github.com/esrrhs/blua
C++ binding to Lua
https://github.com/esrrhs/blua
lua luabinding
Last synced: about 1 month ago
JSON representation
C++ binding to Lua
- Host: GitHub
- URL: https://github.com/esrrhs/blua
- Owner: esrrhs
- License: mit
- Created: 2021-12-23T08:15:33.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-23T11:33:19.000Z (about 2 years ago)
- Last Synced: 2025-04-12T21:12:12.737Z (about 1 month ago)
- Topics: lua, luabinding
- Language: C++
- Homepage:
- Size: 43 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bLua
[
](https://github.com/esrrhs/bLua)
[](https://github.com/esrrhs/bLua)
[](https://github.com/esrrhs/bLua/actions)
C++与Lua的胶水层,b代表着bridge
# 特性
* 依赖C++17
* 只有一个头文件
* 接口简单轻量
* 完全隔离,无侵入
* userdata的方式管理c++指针生命周期# 用法
### lua调用c++
首先注册类及需要的成员函数
```c++
// 注册全局函数
bLua::reg_global_func(L, "newA", newA);
bLua::reg_global_func(L, "printA", printA);-- 调用对象函数
a:set_int(123)
print("a:get_int() ", a:get_int())-- 调用对象函数
a:set_string("abc")
print("a:set_string() ", a:get_string())
```### c++调用lua
c++调用lua全局函数
```c++
// 多个返回值
int output_int = 0;
std::string output_str;
uint64_t output_int64 = 0;// 输入参数
int input_int = 123;
std::string input_str = "test";// 调用
auto err = bLua::call_lua_global_func(L, "global_func_name",
std::tie(output_int, output_str, output_int64),
input_int, input_str);// 调用是否成功
if (err) {
// 出错,输出错误信息
printf("ret error %s\n", err.value().c_str());
} else {
// 成功则输出函数返回结果
printf("%d %s %llu\n", output_int, output_str.c_str(), output_int64);
}
```
如果lua的函数在一层层table中,例如
```lua
function _G.test.func.test(a, b)
return a - b
end
```
那么c++调用lua嵌套的table函数
```c++
// 调用
auto err = bLua::call_lua_table_func(L, {"_G", "test", "func"}, "test",
std::tie(output_int, output_str, output_int64),
input_int, input_str);
```
具体例子可以参考test目录的test.cpp和test.lua# 编译
```
# mkdir build
# cd build
# cmake ..
# make
```
运行test程序
```
# ./bLua
```## 其他
[lua全家桶](https://github.com/esrrhs/lua-family-bucket)