{"id":16759409,"url":"https://github.com/cleoold/lua_interpreter_wrapper","last_synced_at":"2025-10-24T01:58:34.162Z","repository":{"id":126543783,"uuid":"291358313","full_name":"cleoold/lua_interpreter_wrapper","owner":"cleoold","description":"That wraps Lua interpreter in C++ (used to read config files)","archived":false,"fork":false,"pushed_at":"2021-04-23T04:01:50.000Z","size":62,"stargazers_count":13,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-24T01:58:34.009Z","etag":null,"topics":["cpp14","lua","lua-state","raii"],"latest_commit_sha":null,"homepage":"https://cleoold.github.io/lua_interpreter_wrapper/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cleoold.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":"2020-08-29T22:22:52.000Z","updated_at":"2025-04-14T05:40:50.000Z","dependencies_parsed_at":"2023-06-17T04:45:59.917Z","dependency_job_id":null,"html_url":"https://github.com/cleoold/lua_interpreter_wrapper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cleoold/lua_interpreter_wrapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleoold%2Flua_interpreter_wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleoold%2Flua_interpreter_wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleoold%2Flua_interpreter_wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleoold%2Flua_interpreter_wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cleoold","download_url":"https://codeload.github.com/cleoold/lua_interpreter_wrapper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleoold%2Flua_interpreter_wrapper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280723924,"owners_count":26380108,"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","status":"online","status_checked_at":"2025-10-23T02:00:06.710Z","response_time":142,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cpp14","lua","lua-state","raii"],"created_at":"2024-10-13T04:08:04.541Z","updated_at":"2025-10-24T01:58:34.147Z","avatar_url":"https://github.com/cleoold.png","language":"C++","readme":"# lua_interpreter_cpp\n\nDemo project that wraps a little basic Lua API in a C++ manner - object-oriented, RAII, templates. It can execute Lua scripts and extract variables from the Lua state. The C code comes from book _Programming in Lua_ by Roberto Ierusalimschy.\n\n## Dependency\n*   Lua \u003e= 5.3\n\n## Build\n```sh\ncmake CMakeLists.txt\nmake\nmake test\n```\n\nIt will generate a library archive under `build/lib` folder. It also generates two demo executables: `demo_repl`, a Lua REPL basically the same as the built-in one, and `demo_test`, an executable that shows the result of running `demo_test.cxx`.\n\n## Example\n\n### Globals\n\nopen up a Lua state, execute some code:\n\n```cpp\nusing namespace luai;\n\nauto state = lua_interpreter();\nstate.openlibs(); // load all standard libraries\nstate.run_chunk(\"x = 55 y = 66 f = 6.6 s = 'hehe' print(x + y)\");\n```\n\nOutputs `121` on a new line.\n\nif there are errors, it returns a tuple whose first boolean field is `false` and second field is a string indicating the error, for example\n\n```cpp\nauto r = state.run_chunk(\"print(x + z)\");\ncout \u003c\u003c std::get\u003c1\u003e(r) \u003c\u003c endl;\n```\n\nOutputs `attempt to perform arithmetic on a nil value (global 'z')` on my machine.\n\nOne can grab global variables of type integer, number, string, bool and table (they are defined as `enum class types;` in `lua_interpreter.hxx`). For example:\n\n```cpp\nauto x = state.get_global\u003ctypes::INT\u003e(\"x\"); // 55\nauto f = state.get_global\u003ctypes::NUM\u003e(\"f\"); // 6.6\nauto s = state.get_global\u003ctypes::STR\u003e(\"s\"); // \"hehe\"\n```\n\n`luastate_error` will be thrown if variable does not exist (is `nil`) or is other types.\n\nThe types are introspectible by passing special template parameter `types::LTYPE` to `get_global` method:\n\n```cpp\nauto xtype = state.get_global\u003ctypes::LTYPE\u003e(\"x\"); // types::INT\nauto ftype = state.get_global\u003ctypes::LTYPE\u003e(\"f\"); // types::NUM (floats)\nauto ztype = state.get_global\u003ctypes::LTYPE\u003e(\"z\"); // types::NIL (variable does not exist)\n```\n\n### Tables\n\nFor tables, getting a `types::TABLE` returns a `table_handle`. When this object is constructed, the corresponding table is pushed to the Lua stack so we can use `get_field` to obtain its fields (whose signature is the same as previous `get_global`). When this object is destructed, it removes that table from the lua Stack. Use a block scope to contain the returned object so it resets the Lua stack as appropriate when it is destroyed:\n\n```cpp\nstate.run_chunk(\"config = {\\n\"\n                \"   active = true,\\n\"\n                \"   volume = 77.7,\\n\"\n                \"   profile = 'normal',\\n\"\n                \"   menu = {'roar', 'only my railgun', 'level5 - judglight', 'late in autumn'}\\n\"\n                \"}\\n\"\n);\n{\n    // define new table handles only in the scope\n    auto tbl = state.get_global\u003ctypes::TABLE\u003e(\"config\"); // handle\n    // config pushed to Lua stack\n    auto active = tbl.get_field\u003ctypes::BOOL\u003e(\"active\"); // true\n    auto volume = tbl.get_field\u003ctypes::NUM\u003e(\"volume\"); // 77.7\n    auto profile = tbl.get_field\u003ctypes::STR\u003e(\"profile\"); // \"normal\"\n\n    // open new scope for nested tables\n    // this is also an array, so one can use get_index()\n    {\n        auto menu = tbl.get_field\u003ctypes::TABLE\u003e(\"menu\");\n        // menu pushed to Lua stack\n        auto myvec = std::vector\u003cstd::string\u003e();\n        auto menu_len = menu.len();\n        for (auto i = 1LL; i \u003c= menu_len; ++i)\n            myvec.emplace_back(menu.get_index\u003ctypes::STR\u003e(i));\n    }\n    // menu popped from Lua stack\n}\n// config popped from Lua stack\n```\n\nSo do not try to move the `table_handle` to containers, threads, etc that live longer than its current scope - it breaks RAII. `demo_test` can be referred to for detailed usage.\n\nBy taking the advantage of object destruction order, `table_handle`s need not be nested:\n\n```cpp\n{\n    auto config = state.get_global\u003ctypes::TABLE\u003e(\"config\");\n    auto menu = config.get_field\u003ctypes::TABLE\u003e(\"menu\");\n    std::cout \u003c\u003c \"now playing - \" \u003c\u003c menu.get_index\u003ctypes::STR\u003e(1)\n              \u003c\u003c \" 🔊\" \u003c\u003c config.get_field\u003ctypes::NUM\u003e(\"volume\")\n              \u003c\u003c std::endl;\n}\n```\n\nHowever, that beginning scope block is still needed. This prints `now playing - roar  🔊77.7` on a new line.\n\n## End note\n\nThese functions are not thread-safe, though. Use a mutex lock to ensure sync.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcleoold%2Flua_interpreter_wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcleoold%2Flua_interpreter_wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcleoold%2Flua_interpreter_wrapper/lists"}