From: Brendan Hansen Date: Mon, 2 Mar 2020 05:45:00 +0000 (-0600) Subject: small changes and improvements X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=0c7963d0936442d2a1889b3f8a4f5cb55fa0186d;p=wasm-analyzer.git small changes and improvements --- diff --git a/conf.lua b/conf.lua index e01fe28..a97a94b 100644 --- a/conf.lua +++ b/conf.lua @@ -49,5 +49,5 @@ end return { COLOR_SCHEMES = COLOR_SCHEMES; - COLOR_SCHEME = COLOR_SCHEMES.LIGHT; + COLOR_SCHEME = COLOR_SCHEMES.DARK; } diff --git a/main.lua b/main.lua index 81c37b3..e993964 100644 --- a/main.lua +++ b/main.lua @@ -9,12 +9,14 @@ import { Ui = "src.ui.ui:Ui"; UiRegion = "src.ui.ui:UiRegion"; + UiButton = "src.ui.ui:UiButton"; ScrollingUiRegion = "src.ui.ui:ScrollingUiRegion"; DraggableRect = "src.ui.ui:DraggableRect"; render_text = "src.ui.ui:render_text"; scissor_points = "src.utils:scissor_points"; + Rectangle = "src.utils:Rectangle"; COLORS = "conf:COLOR_SCHEME"; } @@ -25,6 +27,7 @@ class "FunctionBlock" [DraggableRect] { WasmCodeGenerator:setup(func, mod, COLORS) self.header_text = WasmCodeGenerator:build_header() + if func.body then self.body_text = WasmCodeGenerator:build_body() else @@ -38,12 +41,26 @@ class "FunctionBlock" [DraggableRect] { self.old_height = 400 end; - onclick = function(self, button, x, y) - DraggableRect.onclick(self, button, x, y) + toggle_body_visible = function(self) + self.body_visible = not self.body_visible + self.rect.h = self.body_visible and self.old_height or 22 + end; - if y <= 24 and button == 2 then - self.body_visible = not self.body_visible - self.rect.h = self.body_visible and self.old_height or 22 + onclick = function(self, button, x, y, ox, oy) + DraggableRect.onclick(self, button, x, y, ox, oy) + + if button == 2 then + local cmr + cmr = FunctionBlockContextMenu(self.ui, ox, oy, { + { text = "Toggle open", action = function() + cmr.should_delete = true + self:toggle_body_visible() + end + }; + { text = "Resize", action = function() cmr.should_delete = true end }; + { text = "Hide", action = function() end; }; + }) + self.ui:add_region(cmr) end end; @@ -55,7 +72,7 @@ class "FunctionBlock" [DraggableRect] { ondrag = function(self, button, x, y, dx, dy) DraggableRect.ondrag(self, button, x, y, dx, dy) - if y >= 24 and button == 2 then + if y >= 24 and button == 3 then self.rect.w = math.max(100, x) if self.body_visible then self.rect.h = math.max(22, y) @@ -82,6 +99,37 @@ class "FunctionBlock" [DraggableRect] { end; } +class "FunctionBlockContextMenu" [UiRegion] { + init = function(self, ui, cx, cy, options) + UiRegion.init(self, ui) + + self.rect.w = 200 + self.rect.h = 25 * #options + self.rect.x = cx - self.rect.w / 2 + self.rect.y = cy - self.rect.h / 2 + + self.background = COLORS.secondary_dark + + for i, opt in ipairs(options) do + self:add_object(with(UiButton()) { + text = opt.text; + background = COLORS.secondary_dark; + hover_background = COLORS.secondary; + click_background = COLORS.secondary_light; + foreground = COLORS.secondary_text; + rect = Rectangle(0, (i - 1) * 25, 200, 25); + click = opt.action + }) + end + + self.layer = 100 + end; + + onmouseleave = function(self) + self.should_delete = true + end; +} + mod = nil ui = nil function love.load() @@ -89,7 +137,7 @@ function love.load() mod = decompile(arg[2]) - ui_region = ScrollingUiRegion() + ui_region = ScrollingUiRegion(ui) ui_region.rect.x = 0 ui_region.rect.y = 0 ui_region.rect.w = 1200 diff --git a/src/ui/ui.lua b/src/ui/ui.lua index 3b6b7c4..63e7f3b 100644 --- a/src/ui/ui.lua +++ b/src/ui/ui.lua @@ -1,4 +1,5 @@ import { + pprint = "lualib.pprint"; Rectangle = "src.utils:Rectangle"; uuid = "src.utils:uuidv4"; revipairs = "src.utils:revipairs"; @@ -26,7 +27,7 @@ class "Ui" { end if region ~= nil then - region:onmousedown(button, x - region.rect.x, y - region.rect.y) + region:onmousedown(button, x - region.rect.x, y - region.rect.y, x, y) end end; @@ -40,7 +41,7 @@ class "Ui" { end if region ~= nil then - region:onmouseup(button, x - region.rect.x, y - region.rect.y) + region:onmouseup(button, x - region.rect.x, y - region.rect.y, x, y) end end; @@ -86,8 +87,12 @@ class "Ui" { end; update = function(self, dt) - for _, region in ipairs(self.regions) do - region:update(dt); + for i, region in ipairs(self.regions) do + region:update(dt) + + if region.should_delete then + table.remove(self.regions, i) + end end end; @@ -103,8 +108,9 @@ class "Ui" { } class "UiRegion" { - init = function(self) + init = function(self, ui) self.active = false + self.ui = ui self.selected_object = nil self.objects = {} @@ -116,9 +122,12 @@ class "UiRegion" { -- Internals self.is_mouse_down = false self.fire_onclick = false + self.should_delete = false + self.hovered_object = nil end; add_object = function(self, obj) + obj.ui = self.ui table.insert(self.objects, obj) end; @@ -131,7 +140,7 @@ class "UiRegion" { return obj.rect end; - onmousedown = function(self, button, x, y) + onmousedown = function(self, button, x, y, ox, oy) self.is_mouse_down = button self.fire_onclick = true local new_selected = nil @@ -166,17 +175,33 @@ class "UiRegion" { self.selected_object:onselect() end end + + if self.selected_object then + self.selected_object:onmousedown(button, x, y, ox, oy) + end end; - onmouseup = function(self, button, x, y) + onmouseup = function(self, button, x, y, ox, oy) self.is_mouse_down = false if self.selected_object ~= nil and self.fire_onclick then local obj_rect = self:_obj_rect(self.selected_object) - self.selected_object:onclick(button, x - obj_rect.x, y - obj_rect.y) + self.selected_object:onclick(button, x - obj_rect.x, y - obj_rect.y, ox, oy) end end; onmousemove = function(self, x, y, dx, dy) + for _, obj in revipairs(self.objects) do + if self:_obj_rect(obj):contains(x, y) then + if self.hovered_object and obj ~= self.hovered_object then + self.hovered_object:onunhover() + end + + obj:onhover(x, y) + self.hovered_object = obj + break + end + end + if self.is_mouse_down and self.selected_object ~= nil then local obj_rect = self:_obj_rect(self.selected_object) self.selected_object:ondrag(self.is_mouse_down, x - obj_rect.x, y - obj_rect.y, dx, dy) @@ -226,8 +251,8 @@ class "UiRegion" { } class "ScrollingUiRegion" [UiRegion] { - init = function(self) - UiRegion.init(self) + init = function(self, ui) + UiRegion.init(self, ui) self.offset = { x = 0; y = 0; } self.line_color = { 0, 0, 0 } @@ -245,14 +270,14 @@ class "ScrollingUiRegion" [UiRegion] { ((y + self.rect.y) - self.rect.h / 2) / self.zoom + self.rect.h / 2 - self.offset.y - self.rect.y end; - onmousedown = function(self, button, x, y) + onmousedown = function(self, button, x, y, ox, oy) local mx, my = self:_transform_mouse(x, y) - UiRegion.onmousedown(self, button, mx, my) + UiRegion.onmousedown(self, button, mx, my, ox, oy) end; - onmouseup = function(self, button, x, y) + onmouseup = function(self, button, x, y, ox, oy) local mx, my = self:_transform_mouse(x, y) - UiRegion.onmouseup(self, button, mx, my) + UiRegion.onmouseup(self, button, mx, my, ox, oy) end; onmousemove = function(self, x, y, dx, dy) @@ -270,8 +295,8 @@ class "ScrollingUiRegion" [UiRegion] { if self.selected_object == nil then self.zoom = self.zoom * (dy > 0 and 1.05 or (1 / 1.05)) - if self.zoom >= 1 then self.zoom = 1 end - if self.zoom <= (1 / 1.05) ^ 30 then self.zoom = (1 / 1.05) ^ 30 end + -- if self.zoom >= 1 then self.zoom = 1 end + if self.zoom <= 0.2 then self.zoom = 0.2 end end end; @@ -341,11 +366,17 @@ class "UiObject" { self.uuid = uuid() self.selected = false self.order = math.random(1, 1024) + self.ui = {} end; - onclick = function(self, button, x, y) end; + onclick = function(self, button, x, y, ox, oy) end; ondrag = function(self, button, x, y, dx, dy) end; + onhover = function(self, x, y) end; + onunhover = function(self) end; onwheel = function(self, dx, dy) end; + onmousedown = function(self, button, x, y, ox, oy) + self.selected = true + end; onselect = function(self) self.selected = true @@ -356,41 +387,64 @@ class "UiObject" { onkey = function(self, button) end; - update = function(dt) end; - draw = function() end; + update = function(self, dt) end; + draw = function(self) end; } -class "DraggableRect" [UiObject] { - init = function(self, x, y, w, h) +class "UiButton" [UiObject] { + init = function(self) UiObject.init(self) - self.rect = Rectangle(x, y, w, h) + self.text = "Placeholder" + self.hovered = false + self.background = { 0, 0, 0 } + self.hover_background = { 0.2, 0.2, 0.2 } + self.click_background = { 0.4, 0.4, 0.4 } + self.foreground = { 1, 1, 1 } end; - ondrag = function(self, button, x, y, dx, dy) - if button == 1 then - self.rect.x = self.rect.x + dx - self.rect.y = self.rect.y + dy + onclick = function(self, button, x, y, ox, oy) + if button == 1 and self.click then + self.click(self, x, y) end + self.selected = false + end; + + onhover = function(self, x, y) + self.hovered = true + end; + + onunhover = function(self) + self.hovered = false end; draw = function(self) if self.selected then - love.graphics.setColor(0, 0, 1) - love.graphics.rectangle("fill", -2, -2, self.rect.w + 4, self.rect.h + 4) + love.graphics.setColor(self.click_background) + elseif self.hovered then + love.graphics.setColor(self.hover_background) + else + love.graphics.setColor(self.background) end - love.graphics.setColor(1, 1, 1) love.graphics.rectangle("fill", 0, 0, self.rect.w, self.rect.h) - render_text(0, 0, { - { text = "1 Hello ", color = { 1, 0, 0 } }; - { text = "world", color = { 0, 0, 1 } }; - { newline = true }; - { text = "2 Nother line", color = { 0, 0.5, 0 } }; - { newline = true }; - { newline = true }; - { text = "3 nother Nother line", color = { 0, 0.5, 0 } }; - }) + love.graphics.setColor(self.foreground) + love.graphics.printf(self.text, 2, 2, self.rect.w, "center") + end; +} + +class "DraggableRect" [UiObject] { + init = function(self, x, y, w, h) + UiObject.init(self) + + self.rect = Rectangle(x, y, w, h) + end; + + ondrag = function(self, button, x, y, dx, dy) + if button == 1 then + self.rect.x = self.rect.x + dx + self.rect.y = self.rect.y + dy + end end; } @@ -455,6 +509,7 @@ return module { UiRegion = UiRegion; ScrollingUiRegion = ScrollingUiRegion; UiObject = UiObject; + UiButton = UiButton; DraggableRect = DraggableRect; render_text = render_text; diff --git a/src/utils.lua b/src/utils.lua index d5ff427..07f3235 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -122,6 +122,11 @@ function scissor_points(x, y, w, h) return nx, ny, tx - nx, ty - ny end +function wrap(f, ...) + local a = { ... } + return function() f(unpack(a)) end +end + return module { buffered_read = buffered_read; build_str = build_str; @@ -129,6 +134,7 @@ return module { uuidv4 = uuidv4; revipairs = revipairs; + wrap = wrap; scissor_points = scissor_points; diff --git a/src/wasm/decompile.lua b/src/wasm/decompile.lua index 08534db..512d686 100644 --- a/src/wasm/decompile.lua +++ b/src/wasm/decompile.lua @@ -165,6 +165,9 @@ function parse_instr(r) local else_instrs = {} local inelse = false + local block = { "if", label = name, rt = rt } + r.label_stack:push(block) + while true do local peek = r:peek_byte() if peek == 0x0B then @@ -172,7 +175,9 @@ function parse_instr(r) break elseif peek == 0x05 then r:read_byte() + r.label_stack:pop() inelse = true + r.label_stack:push({ "else", label = name, rt = rt }) else if not inelse then table.insert(instrs, parse_instr(r)) @@ -182,6 +187,8 @@ function parse_instr(r) end end + r.label_stack:pop() + return { "if", rt = rt, instrs = instrs, else_instrs = else_instrs } end; @@ -679,7 +686,7 @@ function parse_module(r) types = functypes; tables = tables; mems = mems; - globals = globas; + globals = globals; exports = exports; start = start; elems = elems; diff --git a/src/wasm/text.lua b/src/wasm/text.lua index ed9d61e..adfee3e 100644 --- a/src/wasm/text.lua +++ b/src/wasm/text.lua @@ -14,7 +14,10 @@ WasmCodeGenerator = singleton { build_header = function(self) local text = {} - table.insert(text, { text = "[" .. self.func.funcidx .. "] ", color = self.colors.code }) + if self.mod.start ~= nil and self.mod.start.contents.func == self.func.funcidx then + table.insert(text, { text = "*START* ", color = self.colors.primary_light }) + end + table.insert(text, { text = "func[" .. self.func.funcidx .. "] ", color = self.colors.code }) table.insert(text, { text = self.func.name, color = self.colors.keyword }) table.insert(text, { text = ": ", color = self.colors.code }) diff --git a/testing.lua b/testing.lua index 6f76fe2..6823349 100644 --- a/testing.lua +++ b/testing.lua @@ -1,11 +1,11 @@ require "lualib.oop" import { - decompile = "decompile:"; - build_str = "utils:build_str"; + decompile = "src.wasm.decompile:"; + build_str = "src.utils:build_str"; pprint = "lualib.pprint"; } -local mod = decompile("./data/main.wasm") +local mod = decompile(arg[1]) pprint(mod)