ziglua icon indicating copy to clipboard operation
ziglua copied to clipboard

LuaJIT miscompilation

Open digitcrusher opened this issue 1 year ago • 1 comments

The code below freezes with Ziglua's LuaJIT. The C++ translation of my example works correctly. I reckon this may be a problem with how LuaJIT is compiled by Ziglua? The culprit seems to be a JIT-compiled version of the JLOOP opcode.

main.lua:

-- Interestingly enough, this is the minimal number of X's that causes the hang-up.
local xs = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
for i = 1, #xs do end

main.zig:

pub fn main() !void {
  var gpa = @import("std").heap.GeneralPurposeAllocator(.{}){};
  defer _ = gpa.deinit();

  var vm = try @import("ziglua").Lua.init(&gpa.allocator());
  defer vm.deinit();
  vm.openLibs();

  try vm.doFile("main.lua");
}

build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
  const target = b.standardTargetOptions(.{});
  const optimize = b.standardOptimizeOption(.{});
  const exe = b.addExecutable(.{
    .name = "ziglua-bug",
    .root_source_file = b.path("main.zig"),
    .target = target,
    .optimize = optimize,
  });
  exe.root_module.addImport("ziglua", b.dependency("ziglua", .{
    .lang = .luajit,
    .target = target,
    .optimize = optimize,
  }).module("ziglua"));
  b.installArtifact(exe);
}

build.zig.zon:

.{
  .name = "ziglua-bug",
  .version = "0.0.0",
  .dependencies = .{
    .ziglua = .{
      .url = "https://github.com/natecraddock/ziglua/archive/7479680.tar.gz",
      .hash = "122094a86e7171946ec9dc47ea22c4782511c26ae555a7e79e315ae857ccce15cfa5",
    },
  },
  .paths = .{
    "build.zig",
    "build.zig.zon",
    "main.zig",
  },
}

digitcrusher avatar Aug 30 '24 07:08 digitcrusher

Hey there! LuaJIT support is still WIP because the build process is very complex and I haven't had time to dedicate to that. Sorry about that.

I am open to contributions though!

See https://github.com/natecraddock/ziglua/issues/19 for tracking luajit support

natecraddock avatar Aug 30 '24 15:08 natecraddock