cleanup, I think
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 16 Jul 2021 14:56:34 +0000 (09:56 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 16 Jul 2021 14:56:34 +0000 (09:56 -0500)
site/js/js_events.js
src/app.onyx
src/wasm.onyx

index 4ef84a11c0e180db98ed536747bd9cb807f6f8d4..fe0d0fb779691acfa4ec475f6371aa575254cdf1 100644 (file)
@@ -111,15 +111,18 @@ window.ONYX_MODULES.push({
 
             // ROBUSTNESS: Currently, this only gives the first file, which for a lot of purposes, will be enough.
             // But if multiple files are dropped, the application will only know about the first one.
-            ev.dataTransfer.items[0].getAsFile().arrayBuffer()
-                .then(response => {
-                    // 0 is assumed to be reserved in request_file.onyx.
-                    requested_file_data[0] = response;
-                    push_event_to_buffer(esp, event_size, 0x08, [0x01, 0, response.byteLength]);
-                })
-                .catch(error => {
-                    push_event_to_buffer(esp, event_size, 0x08, [0x02, 0, 0]);
-                });
+            var response = ev.dataTransfer.items[0].getAsFile();
+            response.arrayBuffer().then(data => {
+                // 0 is assumed to be reserved in request_file.onyx.
+                requested_file_data[0] = {
+                    name: response.name,
+                    data: data,
+                };
+                push_event_to_buffer(esp, event_size, 0x08, [0x01, 0, data.byteLength, response.name.length ]);
+            })
+            .catch(error => {
+                push_event_to_buffer(esp, event_size, 0x08, [0x02, 0, 0, 0]);
+            });
 
             return false;
         });
@@ -145,26 +148,34 @@ window.ONYX_MODULES.push({
         fetch(path)
             .then(response => response.arrayBuffer())
             .then(array_buffer => {
-                requested_file_data[fileid] = array_buffer;
+                requested_file_data[fileid] = {
+                    name: path,
+                    data: array_buffer,
+                };
 
-                push_event_to_buffer(esp, event_size, 0x09, [ 0x01, fileid, array_buffer.byteLength ]);
+                push_event_to_buffer(esp, event_size, 0x09, [ 0x01, fileid, array_buffer.byteLength, path.length ]);
             })
             .catch((error) => {
-                push_event_to_buffer(esp, event_size, 0x09, [ 0x02, fileid, 0 ]);
+                push_event_to_buffer(esp, event_size, 0x09, [ 0x02, fileid, 0, 0 ]);
             });
     },
 
-    get_requested_file_data(fileid, bufferptr, bufferlen) {
+    get_requested_file_data(fileid, bufferptr, bufferlen, nameptr, namelen) {
         var file_data = requested_file_data[fileid];
         if (file_data == null) return 0;
 
-        if (bufferlen < file_data.byteLength) return 0;
+        if (bufferlen < file_data.data.byteLength) return 0;
 
         let WASM_U8 = new Uint8Array(ONYX_MEMORY.buffer);
-        var u8_data = new Uint8Array(file_data);
+        var u8_data = new Uint8Array(file_data.data);
 
         WASM_U8.set(u8_data, bufferptr);
 
+        if (namelen >= file_data.name.length) {
+            var name_data = new TextEncoder().encode(file_data.name);
+            WASM_U8.set(name_data, nameptr);
+        }
+
         requested_file_data[fileid] = null;
         delete requested_file_data[fileid];
 
index cc78e119d62c589411b48e90a947ee0ec7dfa0f9..5219f7fd75f79990f6711e2c619f4f874f436c0c 100644 (file)
@@ -46,7 +46,7 @@ init :: () {
     gl.enable(gl.BLEND);
     gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
 
-    analyzer_state->init();
+    __initialize(^analyzer_state);
 
     load_background_tile_texture :: () {
         background_tile_texture = gfx.load_texture(32, 32, #file_contents "res/images/background_tile.data", gl.RGB, gl.RGB);
@@ -101,8 +101,6 @@ init :: () {
             };
         }
 
-        analyzer_state->init();
-
         debug_log(.Info, "Successfully loaded colorscheme.", null);
     }
 
@@ -245,10 +243,16 @@ handle_event :: (event: ^events.Event) {
         }
 
         case .FileDropped {
-            debug_log(.Info, "File with size {} and status {} was dropped.\n", event.file.size, event.file.status);
-
             wasm_data := memory.make_slice(u8, event.file.size);
-            events.get_requested_file_data(event.file.file_id, wasm_data);
+            name := memory.make_slice(u8, event.file.name_length, context.temp_allocator);
+            events.get_requested_file_data(event.file.file_id, wasm_data, name);
+
+            debug_log(.Info, "File with size {} and name {} was dropped.\n", event.file.size, name);
+
+            if !string.ends_with(name, ".wasm") {
+                debug_log(.Warning, "A non-WASM file was dropped. Ignoring.", null);
+                break;
+            }
 
             // This transfers ownership of wasm_data to the analyzer_state
             load_wasm_binary(^analyzer_state, wasm_data);
index 1ef66c909d33eeccfd4b5ce2e79d50b631ebd584..e0932ead9065852eab2ba76a76ddfb85ea200cf5 100644 (file)
@@ -9,6 +9,16 @@ use package core
 
 use package core.intrinsics.onyx { __initialize }
 
+
+// I'm honestly not entirely sure what should go in this structure.
+// I want this framework/UI to be fairly portable to other binary file
+// types (ELF, PE), so I want this to be fairly independent from the
+// specifics about how it is being displayed. However, I think it would
+// also be nice to have a single (or couple) structures that can be serialied
+// and saved to store the project session state. I think for the time being I
+// will let this get as gross as possible while adding new features and I'll
+// clean this up later when I have a better idea.
+//                                           - brendanfh 2021/07/15
 Wasm_Analyzer_State :: struct {
     allocator := context.allocator;
 
@@ -16,20 +26,9 @@ Wasm_Analyzer_State :: struct {
     wasm_binary   : wasm.WasmBinary;
     wasm_sections : wasm.WasmSections;
 
-    windows : map.Map(u32, ui.Window_State);
+    window_states : map.Map(u32, ui.Window_State);
 
-    sidebar_window    := ui.Window_State.{ .{0,0}, .{400,400} };
-    sidebar_expansion := 0.0f;
     sidebar_expansion_target := 0.0f;
-
-    init :: (use state: ^Wasm_Analyzer_State) {
-        __initialize(state);
-
-        sidebar_window.border_width = 0;
-        sidebar_window.border_color = .{ 0, 0, 0 };
-        sidebar_window.background_color = config.Colors.dark_background;
-        sidebar_window.active_color     = config.Colors.background;
-    }
 }
 
 load_wasm_binary :: (use state: ^Wasm_Analyzer_State, @transfers_ownership data: [] u8) {
@@ -51,15 +50,23 @@ draw_sidebar :: (use state: ^Wasm_Analyzer_State, y_offset := 32.0f) {
     window_width, window_height := gfx.get_window_size();
     sidebar_window.position = .{    (sidebar_expansion - 1) * sidebar_window.size.x, y_offset };
     sidebar_window.size.y   = ~~window_height - y_offset;
+    sidebar_window.active_color = config.Colors.background;
+    sidebar_window.background_color = config.Colors.dark_background;
+    sidebar_window.border_width = 0;
 
     sidebar_rectangle := ui.Rectangle.{ 0, 0, sidebar_window.size.x, sidebar_window.size.y };
 
     ui.window_start(^sidebar_window);
     defer ui.window_end();
 
+    // since there is only one sidebar, these can be persistant.
+    // Should all the sidebar state just be persistant variables tho??
     #persist x_scroll := 0.0f;
     #persist y_scroll := 0.0f;
 
+    #persist sidebar_window    := ui.Window_State.{ gfx.Vector2.{0,0}, gfx.Vector2.{400,400} };
+    #persist sidebar_expansion := 0.0f;
+
     ui.scrollable_region_start(sidebar_rectangle, ^x_scroll, ^y_scroll);
     defer ui.scrollable_region_stop();
 
@@ -85,3 +92,5 @@ draw_sidebar :: (use state: ^Wasm_Analyzer_State, y_offset := 32.0f) {
     }
 }
 
+draw_windows :: (use state: ^Wasm_Analyzer_State) {
+}