From ca667ed7f38de4d12e52130fa261fb6c792079e5 Mon Sep 17 00:00:00 2001 From: MentatBot <160964065+MentatBot@users.noreply.github.com> Date: Mon, 14 Apr 2025 05:08:10 +0000 Subject: [PATCH] Use internal Luau headers with compatibility layer for luaL_register --- .github/workflows/build.yml | 173 +++--------------------------------- source/lfs.c | 44 ++++++++- 2 files changed, 50 insertions(+), 167 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index aa0c4115..70629108 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -55,172 +55,19 @@ jobs: fi fi - # Note: We're using standard Lua headers - echo "Using standard Lua headers - we'll look for them on the system" - - # Install lua and luarocks with homebrew - brew install lua@5.4 || brew install lua || echo "Lua installation failed but we'll continue" - which lua || echo "Lua not found in PATH" - - # Create our include directories - mkdir -p /tmp/lua_include - mkdir -p /tmp/lua_lib - - # Create minimal header files directly - echo "Creating minimal Lua headers..." - cat > /tmp/lua_include/lua.h << 'EOF' -/* Minimal lua.h for LuaFileSystem */ -#ifndef LUA_H -#define LUA_H - -#include -#include - -#define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "1" -#define LUA_VERSION "Lua 5.1" - -#define LUA_REGISTRYINDEX (-10000) -#define LUA_GLOBALSINDEX (-10002) - -#define LUA_TNONE (-1) -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - -typedef struct lua_State lua_State; -typedef int (*lua_CFunction) (lua_State *L); - -/* Basic stack manipulation */ -int lua_gettop (lua_State *L); -void lua_settop (lua_State *L, int idx); -void lua_pushvalue (lua_State *L, int idx); -void lua_remove (lua_State *L, int idx); -void lua_insert (lua_State *L, int idx); -void lua_replace (lua_State *L, int idx); - -/* Basic type checks */ -int lua_isnumber (lua_State *L, int idx); -int lua_isstring (lua_State *L, int idx); -int lua_istable (lua_State *L, int idx); -int lua_isfunction (lua_State *L, int idx); -int lua_isuserdata (lua_State *L, int idx); -int lua_type (lua_State *L, int idx); -const char *lua_typename (lua_State *L, int tp); - -/* Get functions (Lua -> stack) */ -void lua_gettable (lua_State *L, int idx); -void lua_getfield (lua_State *L, int idx, const char *k); -void lua_rawget (lua_State *L, int idx); -void lua_rawgeti (lua_State *L, int idx, int n); -void lua_createtable (lua_State *L, int narr, int nrec); -void *lua_newuserdata (lua_State *L, size_t sz); -int lua_getmetatable (lua_State *L, int objindex); - -/* Set functions (stack -> Lua) */ -void lua_settable (lua_State *L, int idx); -void lua_setfield (lua_State *L, int idx, const char *k); -void lua_rawset (lua_State *L, int idx); -void lua_rawseti (lua_State *L, int idx, int n); -int lua_setmetatable (lua_State *L, int objindex); - -/* Push functions (C -> stack) */ -void lua_pushnil (lua_State *L); -void lua_pushnumber (lua_State *L, double n); -void lua_pushinteger (lua_State *L, int n); -void lua_pushlstring (lua_State *L, const char *s, size_t l); -void lua_pushstring (lua_State *L, const char *s); -const char *lua_pushfstring (lua_State *L, const char *fmt, ...); -const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp); -void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); -void lua_pushboolean (lua_State *L, int b); - -/* Pop values */ -#define lua_pop(L,n) lua_settop(L, -(n)-1) -#define lua_newtable(L) lua_createtable(L, 0, 0) -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) -int luaL_ref (lua_State *L, int t); -void luaL_unref (lua_State *L, int t, int ref); - -#endif /* LUA_H */ -EOF - - cat > /tmp/lua_include/lauxlib.h << 'EOF' -/* Minimal lauxlib.h for LuaFileSystem */ -#ifndef LAUXLIB_H -#define LAUXLIB_H - -#include "lua.h" -#include -#include - -/* Compatibility */ -#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502 -#define luaL_register(L,n,l) luaL_setfuncs(L,l,0) -#endif - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - -void luaL_openlib (lua_State *L, const char *libname, const luaL_Reg *l, int nup); -void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l); -int luaL_getmetafield (lua_State *L, int obj, const char *event); -int luaL_callmeta (lua_State *L, int obj, const char *event); -void luaL_checkany (lua_State *L, int narg); -int luaL_error (lua_State *L, const char *fmt, ...); -int luaL_checkoption (lua_State *L, int narg, const char *def, const char *const lst[]); -void luaL_checktype (lua_State *L, int narg, int t); -int luaL_newmetatable (lua_State *L, const char *tname); -void *luaL_checkudata (lua_State *L, int ud, const char *tname); - -#endif /* LAUXLIB_H */ -EOF - - cat > /tmp/lua_include/lualib.h << 'EOF' -/* Minimal lualib.h for LuaFileSystem */ -#ifndef LUALIB_H -#define LUALIB_H - -#include "lua.h" - -#define LUA_FILEHANDLE "FILE*" - -int luaopen_base (lua_State *L); -int luaopen_package (lua_State *L); -int luaopen_string (lua_State *L); -int luaopen_table (lua_State *L); -int luaopen_math (lua_State *L); -int luaopen_io (lua_State *L); -int luaopen_os (lua_State *L); -int luaopen_debug (lua_State *L); - -#endif /* LUALIB_H */ -EOF - - echo "Created minimal Lua header files in /tmp/lua_include" + # Note: Using Luau headers that are already in the project + echo "Using Luau headers from source/cpp/luau" - # Create dummy library - echo "/* Dummy liblua.dylib */" > /tmp/lua_lib/liblua.dylib - echo "/* Dummy liblua.a */" > /tmp/lua_lib/liblua.a + # No need to install Lua or create custom headers + # The compatibility layer is added directly in source/lfs.c - # Set environment variables - echo "LUA_INCLUDE_DIR=/tmp/lua_include" >> $GITHUB_ENV - echo "LUA_LIBRARIES=/tmp/lua_lib/liblua.dylib" >> $GITHUB_ENV + # Create directories for compiler to find headers + mkdir -p build/include - # Add these directories to compiler flags - echo "CFLAGS=-I/tmp/lua_include" >> $GITHUB_ENV - echo "CXXFLAGS=-I/tmp/lua_include" >> $GITHUB_ENV + # Set environment variables to use internal Luau headers + echo "LUA_INCLUDE_DIR=$GITHUB_WORKSPACE/source/cpp/luau" >> $GITHUB_ENV + echo "CFLAGS=-I$GITHUB_WORKSPACE/source/cpp/luau -I$GITHUB_WORKSPACE/source" >> $GITHUB_ENV + echo "CXXFLAGS=-I$GITHUB_WORKSPACE/source/cpp/luau -I$GITHUB_WORKSPACE/source" >> $GITHUB_ENV # Create directories for project resources (only once) mkdir -p Resources/AIData/LocalModels diff --git a/source/lfs.c b/source/lfs.c index 570fe607..09a01ac8 100644 --- a/source/lfs.c +++ b/source/lfs.c @@ -86,10 +86,46 @@ #include "lauxlib.h" #include "lualib.h" #else -// Always use quotes for includes since paths might be non-standard -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" +// Use the Luau headers directly from the project +#include "cpp/luau/lua.h" +#include "cpp/luau/lualib.h" + +// Define a compatibility layer for Luau +#define luaL_register(L, libname, l) luau_register(L, libname, l) + +// Function to simulate lua_pushcfunction for Luau +static void lua_pushcfunction_compat(lua_State* L, lua_CFunction f) { + lua_pushcclosurek(L, f, "lfs_func", 0, NULL); +} +#undef lua_pushcfunction +#define lua_pushcfunction(L, f) lua_pushcfunction_compat(L, f) + +// Forward declaration of our custom luaL_register implementation +static void luau_register(lua_State* L, const char* libname, const struct luaL_Reg* l); + +// Implementation of luaL_register for Luau +static void luau_register(lua_State* L, const char* libname, const struct luaL_Reg* l) { + if (libname) { + lua_getglobal(L, libname); // get table + if (lua_type(L, -1) != LUA_TTABLE) { // check if exists + lua_pop(L, 1); // remove previous result + lua_newtable(L); // create a new table + lua_pushvalue(L, -1); // copy table + lua_setglobal(L, libname); // set global variable + } + } else { + lua_pushglobaltable(L); // Use global table + } + + // Register all functions + for (; l->name; l++) { + lua_pushcclosurek(L, l->func, l->name, 0, NULL); + lua_setfield(L, -2, l->name); + } + + if (libname) + lua_pop(L, 1); // remove table from stack +} #endif #include "lfs.h"