Skip to content

TheLartians/LuaGlue

Repository files navigation

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

LuaGlue

Lua bindings for Glue.

Usage

Example

Using LuaGlue you can interact with Lua using a simple binding interface. The following example illustrates the basic usage.

#include <glue/lua/state.h>
#include <iostream>

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::string>() << std::endl;

  // extract maps
  auto map = state.get("({a=1, b='2'})").asMap();
  map["a"]->get<int>(); // -> 1

  // extract functions
  auto f = state.get("function(a,b) return a+b end").asFunction();
  f(3, 4).get<int>(); // -> 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.

#include <glue/class.h>

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<A>()
    .addConstructor<std::string>()
    .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 and tests for functionality and examples. See here 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.

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.

cmake -Htest -Bbuild
cmake --build build -j8
./build/LuaGlueTests