{"id":21071625,"url":"https://github.com/esrrhs/blua","last_synced_at":"2025-04-12T21:12:19.214Z","repository":{"id":87695477,"uuid":"441110372","full_name":"esrrhs/bLua","owner":"esrrhs","description":"C++ binding to Lua","archived":false,"fork":false,"pushed_at":"2023-02-23T11:33:19.000Z","size":44,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T21:12:12.737Z","etag":null,"topics":["lua","luabinding"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esrrhs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-23T08:15:33.000Z","updated_at":"2024-08-22T07:19:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"0e0d63a8-4460-4a82-ae78-ee0f4b419ddf","html_url":"https://github.com/esrrhs/bLua","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FbLua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FbLua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FbLua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FbLua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esrrhs","download_url":"https://codeload.github.com/esrrhs/bLua/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631681,"owners_count":21136562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","luabinding"],"created_at":"2024-11-19T18:53:47.223Z","updated_at":"2025-04-12T21:12:19.181Z","avatar_url":"https://github.com/esrrhs.png","language":"C++","readme":"# bLua\r\n\r\n[\u003cimg src=\"https://img.shields.io/github/license/esrrhs/bLua\"\u003e](https://github.com/esrrhs/bLua)\r\n[\u003cimg src=\"https://img.shields.io/github/languages/top/esrrhs/bLua\"\u003e](https://github.com/esrrhs/bLua)\r\n[\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/esrrhs/bLua/c-cpp.yml?branch=master\"\u003e](https://github.com/esrrhs/bLua/actions)\r\n\r\nC++与Lua的胶水层，b代表着bridge\r\n\r\n# 特性\r\n* 依赖C++17\r\n* 只有一个头文件\r\n* 接口简单轻量\r\n* 完全隔离，无侵入\r\n* userdata的方式管理c++指针生命周期\r\n\r\n# 用法\r\n### lua调用c++\r\n首先注册类及需要的成员函数\r\n```c++\r\n// 注册全局函数\r\nbLua::reg_global_func(L, \"newA\", newA);\r\nbLua::reg_global_func(L, \"printA\", printA);\r\n\r\n// 注册类函数\r\nbLua::reg_class\u003cA\u003e(L);\r\nbLua::reg_class_func(L, \"get_this\", \u0026A::get_this);\r\nbLua::reg_class_func(L, \"get_int\", \u0026A::get_int);\r\nbLua::reg_class_func(L, \"set_int\", \u0026A::set_int);\r\nbLua::reg_class_func(L, \"get_string\", \u0026A::get_string);\r\nbLua::reg_class_func(L, \"set_string\", \u0026A::set_string);\r\n```\r\n然后在lua中使用即可\r\n```lua\r\n-- 创建一个对象\r\nlocal a = newA()\r\n\r\n-- 调用对象函数\r\na:set_int(123)\r\nprint(\"a:get_int() \", a:get_int())\r\n\r\n-- 调用对象函数\r\na:set_string(\"abc\")\r\nprint(\"a:set_string() \", a:get_string())\r\n```\r\n\r\n### c++调用lua\r\nc++调用lua全局函数\r\n```c++\r\n// 多个返回值\r\nint output_int = 0;\r\nstd::string output_str;\r\nuint64_t output_int64 = 0;\r\n\r\n// 输入参数\r\nint input_int = 123;\r\nstd::string input_str = \"test\";\r\n\r\n// 调用\r\nauto err = bLua::call_lua_global_func(L, \"global_func_name\", \r\n    std::tie(output_int, output_str, output_int64), \r\n    input_int, input_str);\r\n\r\n// 调用是否成功\r\nif (err) {\r\n    // 出错，输出错误信息\r\n    printf(\"ret error %s\\n\", err.value().c_str());\r\n} else {\r\n    // 成功则输出函数返回结果\r\n    printf(\"%d %s %llu\\n\", output_int, output_str.c_str(), output_int64);\r\n}\r\n```\r\n如果lua的函数在一层层table中，例如\r\n```lua\r\nfunction _G.test.func.test(a, b)\r\n    return a - b\r\nend\r\n```\r\n那么c++调用lua嵌套的table函数\r\n```c++\r\n// 调用\r\nauto err = bLua::call_lua_table_func(L, {\"_G\", \"test\", \"func\"}, \"test\", \r\n    std::tie(output_int, output_str, output_int64), \r\n    input_int, input_str);\r\n```\r\n具体例子可以参考test目录的test.cpp和test.lua\r\n\r\n# 编译\r\n```\r\n# mkdir build\r\n# cd build\r\n# cmake ..\r\n# make\r\n```\r\n运行test程序\r\n```\r\n# ./bLua \r\n```\r\n\r\n## 其他\r\n[lua全家桶](https://github.com/esrrhs/lua-family-bucket)\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Fblua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesrrhs%2Fblua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Fblua/lists"}