GithubHelp home page GithubHelp logo

thelartians / luaglue Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 62 KB

Lua bindings for the Glue library

Home Page: https://github.com/TheLartians/Glue

License: MIT License

CMake 24.71% C++ 75.24% Lua 0.05%
cpp lua bindings lua-bindings glue

luaglue's Introduction

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

luaglue's People

Contributors

thelartians avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.