From 44bb1ec86678d8366f3690774fbc95c3666669ed Mon Sep 17 00:00:00 2001 From: IonutParau Date: Tue, 1 Jul 2025 18:53:24 +0200 Subject: [PATCH] I will murder someone for this --- .gitmodules | 12 ++++++++++++ build.zig | 45 +++++++++++++++++++++++++++++++++++++++++---- foreign/lua52 | 1 + foreign/lua53 | 1 + foreign/lua54 | 1 + src/testLuaArch.c | 33 ++++++++++++++++++++++++++++++--- 6 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 .gitmodules create mode 160000 foreign/lua52 create mode 160000 foreign/lua53 create mode 160000 foreign/lua54 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4b7cc6b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,12 @@ +[submodule "foreign/lua52"] + path = foreign/lua52 + url = https://github.com/lua/lua + branch = v5-2 +[submodule "foreign/lua53"] + path = foreign/lua53 + url = https://github.com/lua/lua + branch = v5.3 +[submodule "foreign/lua54"] + path = foreign/lua54 + url = https://github.com/lua/lua + branch = v5.4 diff --git a/build.zig b/build.zig index 8d995b8..3544698 100644 --- a/build.zig +++ b/build.zig @@ -24,6 +24,45 @@ fn addEngineSources(c: *std.Build.Step.Compile) void { }); } +const LuaVersion = enum { + lua52, + lua53, + lua54, +}; + +// For the test architecture, we specify the target Lua version we so desire. +// This can be checked for with Lua's _VERSION + +fn compileTheRightLua(b: *std.Build, c: *std.Build.Step.Compile, version: LuaVersion) !void { + const alloc = b.allocator; + const dirName = @tagName(version); + + const rootPath = try std.mem.join(alloc, std.fs.path.sep_str, &.{"foreign", dirName}); + + c.addIncludePath(b.path(rootPath)); + + // get all the .c files + var files = std.ArrayList([]const u8).init(alloc); + errdefer files.deinit(); + + var dir = try std.fs.cwd().openDir(rootPath, std.fs.Dir.OpenDirOptions {.iterate = true}); + defer dir.close(); + + var iter = dir.iterate(); + + while(try iter.next()) |e| { + if(std.mem.startsWith(u8, e.name, "l") and std.mem.endsWith(u8, e.name, ".c") and !std.mem.eql(u8, e.name, "lua.c")) { + const name = try alloc.dupe(u8, e.name); + try files.append(name); + } + } + + c.addCSourceFiles(.{ + .root = b.path(rootPath), + .files = files.items, + }); +} + pub fn build(b: *std.Build) void { const os = builtin.target.os.tag; @@ -72,21 +111,19 @@ pub fn build(b: *std.Build) void { if (os == .windows) { // use the msvc win64 dll versions and copy them to raylib/ and lua/ // get raylib from https://github.com/raysan5/raylib/releases - // get lua from https://luabinaries.sourceforge.net/ - emulator.addIncludePath(b.path("lua/include")); emulator.addIncludePath(b.path("raylib/include")); - emulator.addObjectFile(b.path("lua/lua54.lib")); emulator.addObjectFile(b.path("raylib/lib/raylibdll.lib")); } else { - emulator.linkSystemLibrary("lua"); emulator.linkSystemLibrary("raylib"); } + const luaVer = b.option(LuaVersion, "lua", "The version of Lua to use.") orelse LuaVersion.lua54; emulator.addCSourceFiles(.{ .files = &.{ "src/testLuaArch.c", "src/emulator.c", }, }); + compileTheRightLua(b, emulator, luaVer) catch unreachable; // forces us to link in everything too emulator.linkLibrary(engineStatic); diff --git a/foreign/lua52 b/foreign/lua52 new file mode 160000 index 0000000..4324904 --- /dev/null +++ b/foreign/lua52 @@ -0,0 +1 @@ +Subproject commit 4324904b60db5243ede68d0922c1bf3c0dd05986 diff --git a/foreign/lua53 b/foreign/lua53 new file mode 160000 index 0000000..75ea9cc --- /dev/null +++ b/foreign/lua53 @@ -0,0 +1 @@ +Subproject commit 75ea9ccbea7c4886f30da147fb67b693b2624c26 diff --git a/foreign/lua54 b/foreign/lua54 new file mode 160000 index 0000000..1b0f943 --- /dev/null +++ b/foreign/lua54 @@ -0,0 +1 @@ +Subproject commit 1b0f943da7dfb25987456a77259edbeea0b94edc diff --git a/src/testLuaArch.c b/src/testLuaArch.c index 0bfd0a7..ad96a7a 100644 --- a/src/testLuaArch.c +++ b/src/testLuaArch.c @@ -3,10 +3,29 @@ #include #include #include +#include #include "neonucleus.h" char *testLuaSandbox = NULL; +#if LUA_VERSION_NUM == 502 + +// monkey patching + +bool lua_isinteger(lua_State *L, int i) { + if(lua_type(L, i) != LUA_TNUMBER) return false; + double x = lua_tonumber(L, i); + if(isinf(x)) return false; + if(isnan(x)) return false; + return trunc(x) == x; +} + +void lua_seti(lua_State *L, int arr, int i) { + lua_rawseti(L, arr, i); +} + +#endif + typedef struct testLuaArch { lua_State *L; nn_computer *computer; @@ -55,12 +74,12 @@ static nn_value testLuaArch_getValue(lua_State *L, int index) { const char *s = lua_tolstring(L, index, &l); return nn_values_string(s, l); } - if(type == LUA_TNUMBER && lua_isnumber(L, index)) { - return nn_values_number(lua_tonumber(L, index)); - } if(type == LUA_TNUMBER && lua_isinteger(L, index)) { return nn_values_integer(lua_tointeger(L, index)); } + if(type == LUA_TNUMBER && lua_isnumber(L, index)) { + return nn_values_number(lua_tonumber(L, index)); + } //TODO: bring it back once I make everything else not leak memory //luaL_argcheck(L, false, index, luaL_typename(L, index)); return nn_values_nil(); @@ -628,7 +647,15 @@ void testLuaArch_teardown(nn_computer *computer, testLuaArch *arch, void *_) { void testLuaArch_tick(nn_computer *computer, testLuaArch *arch, void *_) { int ret = 0; +#if LUA_VERSION_NUM == 504 int res = lua_resume(arch->L, NULL, 0, &ret); +#endif +#if LUA_VERSION_NUM == 503 + int res = lua_resume(arch->L, NULL, 0); +#endif +#if LUA_VERSION_NUM == 502 + int res = lua_resume(arch->L, NULL, 0); +#endif if(res == LUA_OK) { // machine halted, this is no good lua_pop(arch->L, ret);