a one header library for creating Lua bindings to C++

Overview

Description

A C++ library for generating Lua bindings.

Supported features:

  • functions,
  • constructors,
  • inheritance,
  • member functions,
  • properties,
  • standard containers,
  • user types.

lualite is now the stuff of legends. History became legend. Legend became myth.

Example

  lualite::module{L,
    lualite::class_<testbase>("testbase")
      .constant("__classname", "testbase")
      .constant("__b", true)
      .constant("__pi", 3.1459)
      .def<decltype(&testbase::dummy), &testbase::dummy>("dummy"),
    lualite::class_<testclass>("testclass")
      .constructor("defaultNew")
      .constructor<int>()
      .inherits<testbase>() // you can add more classes to inherit from
      .enum_("smell", 9)
      .def<std::tuple<int, std::string, char const*> (testclass::*)(int), &testclass::print>("print")
      .def<std::vector<std::string> (testclass::*)(std::string) const, &testclass::print>("print_")
      .def<LLFUNC(testclass::pointer)>("pointer")
      .def<LLFUNC(testclass::reference)>("reference")
      .property<LLFUNC(testclass::a), LLFUNC(testclass::set_a)>("a")
      .def<LLFUNC(testclass::test_array)>("test_array"),
    lualite::scope("subscope",
      lualite::class_<testclass>("testclass")
        .constructor<>("defaultNew")
        .constructor<int>()
        .enum_("smell", 10)
        .def<LLFUNC(testfunc)>("testfunc")
        .def<std::tuple<int, std::string, char const*> (testclass::*)(int), &testclass::print>("print")
        .def<std::vector<std::string> (testclass::*)(std::string) const, &testclass::print>("print_")
    )
  }
  .enum_("apple", 1)
  .def<LLFUNC(testfunc)>("testfunc")
  .def<LLFUNC(testpair)>("testpair")
  .def<LLFUNC(testtuple)>("testtuple");

  luaL_dostring(
    L,
    "local a = testfunc(3, 2, 1)\n"
    "r = { \"my\", \"pair\" }\n"
    "testpair(r)\n"
    "r[3] = 3\n"
    "testtuple(r)\n"
    "print(a.y)\n"
    "print(apple)\n"
    "print(testclass.smell)\n"
    "print(testbase.__classname)\n"
    "print(testbase.__b)\n"
    "print(testbase.__pi)\n"
    "local b = testclass.defaultNew()\n"
    "print(\"---\")\n"
    "print(b.a)\n"
    "b:reference().a = 888\n"
    "print(b.dummy)\n"
    "print(b.a .. \" \" .. b:dummy(\"test\"))\n"
    "local tmp1, tmp2, tmp3 = b:pointer():print(100)\n"
    "print(tmp1 .. \" \" .. tmp2 .. \" \" .. tmp3)\n"
    "b:reference():print_(\"msg1\")\n"
    "local a = subscope.testclass.new(1111)\n"
    "print(subscope.testclass.smell)\n"
    "subscope.testclass.testfunc(200, 0, 1)\n"
    "local c = a:reference():print_(\"msg2\")\n"
    "print(c[10])\n"
    "r = {}"
    "for i = 1, 10 do\n"
    "  r[i] = 7\n"
    "end\n"
    "print(a:test_array(r))\n"
  );

FAQ

Q: Why is there no support for introspection in lualite:

A: There is, but it is not exposed to lua by default. The idea is not to pollute the lua namespace and not to make users pay for something they potentially don't want. Example:

void Object::init()
{
  lualite::module(L,
    lualite::class_<Object>("Object")
      .constructor<Object*>()
      .def_func<bool(*)(char const*) noexcept,
        &lualite::class_<Object>::inherits>("inherits")
  );
}

Adds the ability to query whether an object's class inherits from another class. By default, an object's class always inherits from itself. That is, obj.inherits("Object") always returns true. Similar mechanisms can be implemented for querying property names and the types of objects they expose.

Q: Why can't I return char*:

A: Try returning char const*. Don't return references or pointers to non-const objects, if you don't provide wrappers for them (returning a non-const reference or pointer implies the ability to change the referred-to object within a Lua script, which is not possible without also writing a wrapper class for the type of the referred-to object).

Q: Why am I getting:

Assertion failed: (sizeof...(A) + O - 1 == lua_gettop(L)), function member_stub, file ../src/Script/LuaLite.h, line 1172.

whenever I try to call a member function declared here:

class TestClass {
public:
	TestClass();
	virtual ~TestClass();

	void sayHello();

	static void registerFunc(lua_State* L) {
		lualite::module(L,
			lualite::class_<TestClass>("TestClass")
			.constructor("new")
			.def<LLFUNC(TestClass::sayHello)>("sayHello"));
	}
};

using this lua code:

luaL_dostring(luaState,
	"local test = TestClass.new()\n"
	"test.sayHello()");

A: Try test:sayHello(). Or try:

			.def_func<LLFUNC(TestClass::sayHello)>("sayHello"));
You might also like...
A simple and easy-to-use Lua library to enjoy videogames programming
A simple and easy-to-use Lua library to enjoy videogames programming

raylib-lua-sol Lua bindings for raylib, a simple and easy-to-use library to enjoy videogames programming, with sol (www.raylib.com) raylib-lua-sol bin

Highly efficent, caching, copy-on-write lua vector math library

lua-vec Table constructions in Lua are expensive, creating and destroying thousands of "vector" tables frame to frame in an engine can cause serious p

LLVM bindings for Node.js/JavaScript/TypeScript

llvm-bindings LLVM bindings for Node.js/JavaScript/TypeScript Supported OS macOS Ubuntu Windows Supported LLVM methods listed in the TypeScript defini

C# bindings for Sokol using Sokol's binding generator

C# bindings for Sokol using Sokol's binding generator

Android Bindings for QuickJS, A fine little javascript engine.

quickjs-android quickjs-android 是 QuickJS JavaScript 引擎的 Android 接口框架,整体基于面向对象设计,提供了自动GC功能,使用简单。armeabi-v7a 的大小仅 350KB,是 Google V8 不错的替代品,启动速度比 V8 快,内

Python bindings of silk codec.

Python silk module. --- pysilk --- APIs See test\test.py. import pysilk as m m.silkEncode(buf , 24000) m.silkDecode(buf , 24000) #the first param is b

Ziggified GLFW bindings with 100% API coverage, zero-fuss installation, cross compilation, and more.

mach/glfw - Ziggified GLFW bindings Ziggified GLFW bindings that Mach engine uses, with 100% API coverage, zero-fuss installation, cross compilation,

Bindings, from the comfort and speed of C++ and without Qt.

KDBindings Bindings, from the comfort and speed of C++ and without Qt. From plain C++ you get: Signals + Slots. Properties templated on the contained

hb-xlib bindings for Harbour language.

hb-xlib hb-xlib is a Harbour module providing bindings for the Xlib graphics library. This project is intended for people who want to start to program

Comments
  • Support for VS2013

    Support for VS2013

    Would be great if lualite supported VS2013. This would require jumping through some hoops to get the forward() functions to work, since VS2013 can't match function signatures against zero-length parameter packs (the typename... A arguments, in particular). Simpler fixes are needed to avoid using constexpr and noexcept, and to use ::value instead of implicit conversions of type_traits classes to bool via the brace constructor syntax. VS2013 may also have some issues with SFINAE and complex conditionals for enable_if, but I can't experiment more to track those down.

    Definitely a nifty library, and I'd use it if I didn't have to support VS2013.

    wontfix 
    opened by demianmnave 2
Owner
null
💉 Source code for creating Lua executor

FiveM Lua Executor This is the ultimate great source code for building the best injectable Exec on FiveM. I'm not going to tell you how to create a pr

Achtzehn 49 Nov 21, 2022
A dependency free, embeddable debugger for Lua in a single file (.lua or .c)

debugger.lua A simple, embedabble debugger for Lua 5.x, and LuaJIT 2.x. debugger.lua is a simple, single file, pure Lua debugger that is easy to integ

Scott Lembcke 593 Nov 16, 2022
The Lua development repository, as seen by the Lua team. Mirrored irregularly

The Lua development repository, as seen by the Lua team. Mirrored irregularly

Lua 6.3k Nov 24, 2022
It creates a random word by mixing two English common words into a single one, each one with the first character in capital letter. It also allow you to scroll down infinitely without repeating the same word twice.

startup_namer A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if

Samuel Cobas 2 Feb 3, 2022
A C++ header-only library for creating, displaying, iterating and manipulating dates

The ASAP date/time library for beautiful C++ code ASAP is a small, header-only date-time library for C++11 and beyond. It is heavily inspired by my gr

Leonardo Guilherme de Freitas 56 Oct 26, 2022
Tuibox - A single-header terminal UI (TUI) library, capable of creating mouse-driven, interactive applications on the command line.

tuibox tuibox ("toybox") is a single-header terminal UI library, capable of creating mouse-driven, interactive applications on the command line. It is

Andrew 31 Nov 9, 2022
Simple one file header for hijacking windows version.dll for desired executable to do 3rd party modifying without dll injection.

Version-Hijack Simple one file header for hijacking windows version.dll for desired executable to do 3rd party modifying without dll injection. Usage

sneakyevil 6 Oct 19, 2022
A simple one header solution to interacting with Windows WMI in C++

?? WinWMI A simple one header solution to interacting with Windows WMI in C++ Usage Just #include "winwmi.hpp" in your code! Initialize To initialize

Konstantin 'cpz' L. 6 Nov 5, 2022
Zig bindings for the excellent CRoaring library

Zig-Roaring This library implements Zig bindings for the CRoaring library. Naming Any C function that begins with roaring_bitmap_ is a method of the B

Justin Whear 4 Feb 23, 2022
Just a basic mini library for parsing simple files that only have variables written and with Lua extension.

C++ Parser Lua file config Just a basic mini library for parsing simple files that only have variables written and with Lua extension. Note: At the mo

Marcos Oliveira 3 Dec 26, 2021