I will murder someone for this

This commit is contained in:
IonutParau 2025-07-01 18:53:24 +02:00
parent eccbf800eb
commit 44bb1ec866
6 changed files with 86 additions and 7 deletions

12
.gitmodules vendored Normal file
View File

@ -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

View File

@ -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);

1
foreign/lua52 Submodule

@ -0,0 +1 @@
Subproject commit 4324904b60db5243ede68d0922c1bf3c0dd05986

1
foreign/lua53 Submodule

@ -0,0 +1 @@
Subproject commit 75ea9ccbea7c4886f30da147fb67b693b2624c26

1
foreign/lua54 Submodule

@ -0,0 +1 @@
Subproject commit 1b0f943da7dfb25987456a77259edbeea0b94edc

View File

@ -3,10 +3,29 @@
#include <lauxlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#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);