From b04e2c58c3137a8b168b51f6ae1fd4b155dfd170 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 22 Dec 2020 12:15:29 -0600 Subject: [PATCH] added `make tree` function --- app.lua | 2 +- src/ui/components.lua | 63 +++++++++++++++++++++++++++++++++++++++++++ src/utils.lua | 7 +++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/app.lua b/app.lua index 2da02f4..c3bfa45 100644 --- a/app.lua +++ b/app.lua @@ -28,7 +28,7 @@ function init() layer = 100; } - globals.ui.function_blocks_container = ui.make_element "node" + globals.ui.function_blocks_container = ui.make_element "function_blocks_container" ui.insert_child(globals.ui.scrolling_area, globals.ui.function_blocks_container) ui.insert_child(globals.ui.root, globals.ui.scrolling_area) diff --git a/src/ui/components.lua b/src/ui/components.lua index 4c48187..8bb6c03 100644 --- a/src/ui/components.lua +++ b/src/ui/components.lua @@ -4,6 +4,7 @@ import { Rectangle = "src.utils:Rectangle"; draw_arrow = "src.utils:draw_arrow"; + revipairs = "src.utils:revipairs"; wasm_text = "src.wasm.text"; wasm_decompile = "src.wasm.decompile"; @@ -275,6 +276,11 @@ function function_context_menu:init(x, y) rect = Rectangle(0, 0, 200, 20) }) + ui.insert_child(self, with(ui.make_element("button", "Make tree")) { + click = function_context_menu.btn_make_tree; + rect = Rectangle(0, 20, 200, 20) + }) + ui.focus(self) end @@ -299,6 +305,11 @@ function function_context_menu:btn_view_decompiled(button, x, y) for _, r in ipairs(lines) do print(r) end end +function function_context_menu:btn_make_tree(button, x, y) + local wasm_func = self.parent.parent + ui.fire_stoppable_event(globals.ui.root, "make_tree", wasm_func) +end + local scrolling_area = {} function scrolling_area:init() self.offset = { x = 0; y = 0 } @@ -484,6 +495,57 @@ function function_ref_lines:predraw() end end +local function_blocks_container = {} +function function_blocks_container:make_tree(root) + print("Creating tree!", root) + + local funcs = {} + for _, f in ipairs(self.children) do + funcs[f.func.funcidx] = f + end + + local sx = root.rect.x + local sy = root.rect.y + + local padding = 10 + + local have_added = {} + local to_add = {} + + table.insert(to_add, { block = root, x = sx, y = sy }) + + while #to_add > 0 do + local adding = to_add[1] + table.remove(to_add, 1) + table.insert(have_added, adding.block) + print("Processing", adding.block.func.funcidx) + + adding.block.rect.x = adding.x + adding.block.rect.y = adding.y + + if adding.block.func.callees then + local cc = #adding.block.func.callees + + local x = adding.x + for i, calls in revipairs(adding.block.func.callees) do + local b = funcs[calls] + if not table.contains(have_added, b) then + table.insert(to_add, { + block = b, + x = x, + y = adding.y + adding.block.rect.h + padding + }) + + x = x + b.rect.w + padding + end + end + end + end + + return true +end + + ui.register_component "rect" (rect) ui.register_component "drag_rect" (drag_rect) ui.register_component "button" (button) @@ -491,6 +553,7 @@ ui.register_component "function_block" (function_block) ui.register_component "function_context_menu" (function_context_menu) ui.register_component "scrolling_area" (scrolling_area) ui.register_component "function_ref_lines" (function_ref_lines) +ui.register_component "function_blocks_container" (function_blocks_container) return module { rect = rect; diff --git a/src/utils.lua b/src/utils.lua index 176b891..c0cb500 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -77,6 +77,13 @@ function table.append(orig, new) end end +function table.contains(t, val) + for _, v in pairs(t) do + if v == val then return true end + end + return false +end + class "Rectangle" { init = function(self, x, y, w, h) self.x = x -- 2.25.1