import re

SRC = open('/tmp/opencode/make_runner.py').read()
shim = re.search(r"shim = r'''(.*?)'''", SRC, re.S).group(1)

extra = r'''
function __V(x)
  local t = type(x)
  if t == "string" then
    local s = x:sub(1, 64):gsub("%c", ".")
    return string.format("str#%d:%q", #x, s)
  elseif t == "number" then return string.format("%.10g", x)
  elseif t == "table" then
    local n=0 for _ in pairs(x) do n=n+1 if n>100000 then break end end
    return "tbl#" .. tostring(#x) .. "/" .. tostring(n)
  elseif t == "function" then return "fn"
  end
  return t .. ":" .. tostring(x)
end
local function __g(t,k) local ok,v=pcall(function() return t[k] end); if ok then return v end return nil end
__NINS={}
local function __nins(O) local n=__NINS[O]; if n then return n end; local o3=__g(O,3); if type(o3)=="table" then n=#o3 else n=-1 end; __NINS[O]=n; return n end
__HARGSN=0
function __HARGS(O, ...)
  local n=__nins(O)
  if n ~= 140 and n ~= 31 then return end
  if __HARGSN >= 40 then return end
  __HARGSN = __HARGSN + 1
  local cnt = select("#", ...)
  local parts={}
  for i=1,math.min(cnt,12) do parts[#parts+1]=i.."="..__V((select(i,...))) end
  __realprint("HARGS", "nins"..tostring(n), "cnt"..tostring(cnt), table.concat(parts," "))
end
__MAKEN=0
function __MAKE140(O,m)
  local o3=__g(O,3)
  if type(o3)~="table" or #o3~=140 then return end
  if __MAKEN>=10 then return end
  __MAKEN=__MAKEN+1
  local parts={}
  local cnt=0
  if type(m)=="table" then
    for k,v in pairs(m) do cnt=cnt+1; if cnt<=80 then parts[#parts+1]=tostring(k).."="..__V(v) end end
  end
  __realprint("MAKE140", tostring(O), "mtype", type(m), "npairs", cnt, table.concat(parts," "))
end
__OPN=0
__PREVQ=nil
__PREVO=nil
function __OP(O,pc,z,a,b,c,d,e,f,regs)
  local n=__nins(O)
  if n==140 then
    if __PREVO==O and __PREVQ then
      local ch={}
      for i=1,20 do
        local old=__PREVQ[i]; local new=regs[i]
        if old~=new then ch[#ch+1]=i..":"..__V(old).."->"..__V(new) end
      end
      if #ch>0 and __OPN<300 then
        __OPN=__OPN+1
        __realprint("WR", "pc", pc, "op", z, table.concat(ch," "))
      end
    end
    local copy={}
    for i=1,20 do copy[i]=regs[i] end
    __PREVQ=copy
  end
  __PREVO=O
end
'''

shim += extra

raw = open('/tmp/opencode/stage2.lua').read()
rhex = re.search(r'__RHEX__\t([0-9a-f]+)', open('/tmp/opencode/cap4.txt').read()).group(1)
rbytes = bytes.fromhex(rhex)
rlit = '"' + ''.join('\\x%02x' % b for b in rbytes) + '"'

s = raw
s = 'local R=buffer.fromstring(' + rlit + ');' + s[len('local R=...;'):]

old_factory = '(l)[55]=function(O,m,A)'
assert s.count(old_factory)==1
s = s.replace(old_factory, '(l)[55]=function(O,m,A)__MAKE140(O,m);', 1)
old_h = 'h=(function(...)'
assert s.count(old_h) == 1
s = s.replace(old_h, 'h=(function(...)__HARGS(O,...);', 1)

head = 'while true do local Z=o[Q];'
assert s.count(head) == 1
s = s.replace(head, 'while true do local Z=o[Q];__OP(O,Q,Z,t[Q],d[Q],P[Q],y[Q],b[Q],p[Q],q);', 1)

tail = r'''
local __ok,__err = pcall(function()
''' + s + r'''
end)
__realprint("EXEC1", __ok, type(__err))
if __ok then
  local __ok2,__err2 = pcall(__err)
  __realprint("EXEC2", __ok2, tostring(__err2))
end
'''
open('/tmp/opencode/probe_hargs.luau','w').write(shim + tail)
print("wrote")
