https://github.com/thelartians/luaglue
Lua bindings for the Glue library
https://github.com/thelartians/luaglue
bindings cpp glue lua lua-bindings
Last synced: about 1 month ago
JSON representation
Lua bindings for the Glue library
- Host: GitHub
- URL: https://github.com/thelartians/luaglue
- Owner: TheLartians
- License: mit
- Created: 2020-04-29T09:40:34.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-29T17:35:10.000Z (over 2 years ago)
- Last Synced: 2024-10-06T09:44:04.772Z (about 1 year ago)
- Topics: bindings, cpp, glue, lua, lua-bindings
- Language: C++
- Homepage: https://github.com/TheLartians/Glue
- Size: 60.5 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/TheLartians/LuaGlue/actions)
[](https://github.com/TheLartians/LuaGlue/actions)
[](https://github.com/TheLartians/LuaGlue/actions)
[](https://github.com/TheLartians/LuaGlue/actions)
[](https://github.com/TheLartians/LuaGlue/actions)
[](https://codecov.io/gh/TheLartians/LuaGlue)
# LuaGlue
Lua bindings for [Glue](https://github.com/TheLartians/Glue).
## Usage
### Example
Using LuaGlue you can interact with Lua using a simple binding interface.
The following example illustrates the basic usage.
```cpp
#include
#include
void exampleBasics() {
glue::lua::State state;
state.openStandardLibs();
// run Lua code
state.run("print('Hello Lua!')");
// extract values
std::cout << state.get("'Hello' .. ' ' .. 'C++!'")->get() << std::endl;
// extract maps
auto map = state.get("({a=1, b='2'})").asMap();
map["a"]->get(); // -> 1
// extract functions
auto f = state.get("function(a,b) return a+b end").asFunction();
f(3, 4).get(); // -> 7
// inject values
auto global = state.root();
global["x"] = 42;
global["square"] = [](double x){ return x*x; };
// interact with Lua directly
state.run("print(square(x))");
// or using Glue
global["print"](global["square"](global["x"]));
}
```
Classes and inheritance are also supported.
```cpp
#include
struct A {
std::string member;
A(std::string m): member(m) {}
auto method() const { return "member: " + member; }
};
void exampleModules() {
glue::lua::State state;
state.openStandardLibs();
// inject C++ classes and APIs into JavaScript
auto module = glue::createAnyMap();
module["A"] = glue::createClass()
.addConstructor()
.addMember("member", &A::member)
.addMethod("method", &A::method)
;
state.addModule(module, state.root());
state.run("a = A.__new('test');");
state.run("print(a:member());");
state.run("print(a:method());");
}
```
Check the [API](include/glue/lua/state.h) and [tests](test/source/state.cpp) for functionality and examples.
See [here](https://github.com/TheLartians/TypeScriptXX) for a full example project using automatic TypeScript declarations.
### Adding to your project
LuaGlue can be easily integrated through CPM.
If not available before, this will automatically add a Lua and Glue target as well.
```cmake
CPMAddPackage(
NAME LuaGlue
VERSION 1.1.2
GITHUB_REPOSITORY TheLartians/LuaGlue
)
target_link_libraries(myLibrary LuaGlue)
```
### Build and run tests
To build and run the tests, run the following commands from the project's root.
```bash
cmake -Htest -Bbuild
cmake --build build -j8
./build/LuaGlueTests
```