Skip to content
mcpplibsPublic

About

Lua 5.4 as C++23 modules for mcpp: import mcpplibs.mwrap.lua

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

lua

Lua 5.4, the embeddable scripting language, through C++23 modules.

English · 简体中文 · Upstream · Releases · mcpp · Index

C++23 License mcpp-community CI

The upstream C API is unchanged: lua_State, lua_pushinteger, luaL_newstate, luaopen_base are the upstream globals of <lua.h>, <lauxlib.h> and <lualib.h>. The library is compat.lua 5.4.7; this package maps Lua's macros to constants and functions under their original names. Lua's own test suite passes on every CI run, on a standalone interpreter written against the module.

Modules

  • import mcpplibs.mwrap.lua;: the C API (lua_*, luaL_*, luaopen_*, the types), re-exporting the one below
  • import mcpplibs.mwrap.lua.macros;: LUA_OK, lua_pop, lua_tostring, luaL_dostring … in mcpplibs::mwrap::lua::macros

More Modules API -> Usage and API

Getting started

mcpp new scripted --template mcpplibs.mwrap.lua@5.4.7.1
cd scripted
mcpp run

The default template registers a C++ function, runs a script that calls it, and prints what the script returns.

[dependencies]
mcpplibs.mwrap.lua = "5.4.7.1"
import std;
import mcpplibs.mwrap.lua;
using namespace mcpplibs::mwrap::lua::macros;  // lua_pop, lua_tostring, LUA_OK …

int main() {
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    if (luaL_dostring(L, "return ('modules'):upper()")) std::println("error: {}", lua_tostring(L, -1));
    else std::println("{}", lua_tostring(L, -1));
    lua_close(L);
}

Macros

A named module cannot export a macro. Constants are inline constexpr (macros::LUA_OK, macros::LUA_REGISTRYINDEX, macros::LUA_VERSION), and function-like macros are functions that evaluate their arguments as the macro does (lua_pop, lua_pcall, lua_upvalueindex, luaL_newlib …). luaL_argcheck, luaL_argexpected, luaL_opt and luaL_intop cannot be functions without changing what they evaluate; they stay macros in <mwrap/lua/macros.hpp>, included before the import. The table of every macro and why is in docs/usage.md.

Examples

Build and testing

Linux, macOS and Windows CI build the modules and run the tests, the export audit (tools/check_exports.py: every declaration of the three headers exported, every macro mapped or listed with its reason) and Lua's own test suite (lua-5.4.7-tests). A separate job scaffolds the default template as an installed package and runs it.

License

Apache-2.0 for the wrapper, by mcpp-community; Lua remains MIT (upstream/lua/LICENSE).

About

Lua 5.4 as C++23 modules for mcpp: import mcpplibs.mwrap.lua

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages