added `make tree` function
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Dec 2020 18:15:29 +0000 (12:15 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Dec 2020 18:15:29 +0000 (12:15 -0600)
app.lua
src/ui/components.lua
src/utils.lua

diff --git a/app.lua b/app.lua
index 2da02f4cc4936ab6d798fdbdc279483f2577b4cd..c3bfa45973dff9b7577754a1ac5c1859b5dbe282 100644 (file)
--- 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)
index 4c48187478916fda686a5625e908390bf5246541..8bb6c03a64f4d2acfdf3665eb9042edae1e4867b 100644 (file)
@@ -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;
index 176b891e1f87c101340f3727dbffe74b171defcc..c0cb5007794059989cd6a4af40e3f51730df5f98 100644 (file)
@@ -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