luadec icon indicating copy to clipboard operation
luadec copied to clipboard

The logic is confused when I compile and decompile this code with lua 5.1.5

Open ufosky opened this issue 9 years ago • 2 comments

The origin code

area = "native"
g = {}
local g = g
local a = function(text)
  textCache = textCache or {}
  local tc = textCache
  if area == "native" then
    if sdk_android and sdk_type == 100 then
      require("texttransform")
      local tt = g.texttransform.s[text]
      if tt then
        return tt
      end
    end
    text = string.gsub(text, "\\n", "\n")
    text = string.gsub(text, "<key:.*>", "")
    return text
  end
  if not g.checkChinese(text) then
    if config.chinese_character_forbid then
      string.gsub(text, "。", ".")
    end
    return text
  end
  local key = string.match(text, "<key:(.*)>") or "nil"
  text = string.gsub(text, "<key:.*>", "")
  if area ~= "native" then
    local tfix = string.gsub(text, " ", "")
    tc[tfix] = tc[tfix] or {}
    if tc[tfix][key] then
      return tc[tfix][key]
    else
      if g.textConfig then
        for i,v in ipairs(g.textConfig) do
          if v.text == tfix then
            local id = v.key or "nil"
            if id == key then
              id = v.id
              local tt = g.getDataTable("textconfig")
              local trans = tt[id] or {}
              trans = trans[config.textconfig_translation_key or "TRANSLATION"]
              trans = trans or text
              if trans == "<nil>" then
                trans = ""
              end
              trans = string.gsub(trans, "\\n", "\n")
              if string.match(trans, "^<注释>") then
                trans = ""
              end
              tc[tfix][key] = trans
              return trans
            end
          end
        end
      end
    end
  end
  return text
end

g.formatText = a

and the decompiled code

-- Decompiled using luadec 2.2 rev: 705713f for Lua 5.1 from https://github.com/viruscamp/luadec
-- Command line: test1.lua

-- params : ...
-- function num : 0
area = "native"
g = {}
local g = g
local a = function(text)
  -- function num : 0_0 , upvalues : g
  if not textCache then
    textCache = {}
  end
  local tc = textCache
  if area == "native" then
    if sdk_android and sdk_type == 100 then
      require("texttransform")
      local tt = ((g.texttransform).s)[text]
      if tt then
        return tt
      end
    end
    do
      text = (string.gsub)(text, "\\n", "\n")
      text = (string.gsub)(text, "<key:.*>", "")
      return text
      if not (g.checkChinese)(text) and config.chinese_character_forbid then
        (string.gsub)(text, "。", ".")
      end
      return text
      local key = (string.match)(text, "<key:(.*)>") or "nil"
      text = (string.gsub)(text, "<key:.*>", "")
      if area ~= "native" then
        local tfix = (string.gsub)(text, " ", "")
        if not tc[tfix] then
          tc[tfix] = {}
        end
        if (tc[tfix])[key] then
          return (tc[tfix])[key]
        else
          if g.textConfig then
            for i,v in ipairs(g.textConfig) do
              if not v.key then
                local id = v.text ~= tfix or "nil"
              end
              if id == key then
                id = v.id
                local tt = (g.getDataTable)("textconfig")
                if not tt[id] then
                  local trans = {}
                end
                trans = trans[config.textconfig_translation_key or "TRANSLATION"]
                if not trans then
                  trans = text
                end
                if trans == "<nil>" then
                  trans = ""
                end
                trans = (string.gsub)(trans, "\\n", "\n")
                if (string.match)(trans, "^<注释>") then
                  trans = ""
                end
                -- DECOMPILER ERROR at PC149: Confused about usage of register: R12 in 'UnsetPending'

                ;
                (tc[tfix])[key] = trans
                return trans
              end
            end
          end
          do
            return text
          end
        end
      end
    end
  end
end

g.formatText = a

ufosky avatar Nov 07 '14 01:11 ufosky

The simple case for reproducing the problem

function a (b)
  c = c or {}
  return b
end

result

-- Decompiled using luadec 2.2 rev: 705713f for Lua 5.1 from https://github.com/viruscamp/luadec
-- Command line: test1.lua

-- params : ...
-- function num : 0
a = function(b)
  -- function num : 0_0
  if not c then
    c = {}
    return b
  end
end

ufosky avatar Nov 09 '14 13:11 ufosky

test case https://github.com/viruscamp/luadec/blob/master/test/issues/issue32_global_or_to_if.lua

viruscamp avatar Apr 27 '15 08:04 viruscamp