From 1182f41f993e3b32570d1f9547dcb87fb9fe262c Mon Sep 17 00:00:00 2001 From: Caedo Date: Tue, 15 Aug 2023 14:56:04 +0200 Subject: [PATCH 1/2] Fix issues with js events not being initialized properly --- vendor/wasm/js/events.odin | 13 +++++++++++++ vendor/wasm/js/runtime.js | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/vendor/wasm/js/events.odin b/vendor/wasm/js/events.odin index f14d7054e..c9a11c691 100644 --- a/vendor/wasm/js/events.odin +++ b/vendor/wasm/js/events.odin @@ -1,6 +1,8 @@ //+build js wasm32, js wasm64p32 package wasm_js_interface +import "core:runtime" + foreign import dom_lib "odin_dom" Event_Kind :: enum u32 { @@ -343,7 +345,18 @@ do_event_callback :: proc(user_data: rawptr, callback: proc(e: Event)) { user_data = user_data, callback = callback, } + + init_event_raw(&event) + + if event.kind == .Key_Up || event.kind == .Key_Down || event.kind == .Key_Press { + key := transmute(^runtime.Raw_String) &event.key.key + key.data = &event.key._key_buf[0] + + code := transmute(^runtime.Raw_String) &event.key.code + code.data = &event.key._code_buf[0] + } + callback(event) } } \ No newline at end of file diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index bcc7e2051..353bc521d 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -99,6 +99,11 @@ class WasmMemoryInterface { storeF64(addr, value) { this.mem.setFloat64(addr, value, true); } storeInt(addr, value) { this.mem.setInt32 (addr, value, true); } storeUint(addr, value) { this.mem.setUint32 (addr, value, true); } + + storeString(addr, value) { + const bytes = this.loadBytes(addr, value.length); + new TextEncoder("utf-8").encodeInto(value, bytes); + } }; class WebGLInterface { @@ -1384,6 +1389,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { wmi.storeUint(off(W), event_temp_data.id_ptr); wmi.storeUint(off(W), event_temp_data.id_len); + wmi.storeUint(off(W), 0); // padding wmi.storeF64(off(8), e.timeStamp*1e-3); @@ -1417,8 +1423,14 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { wmi.storeI16(off(2), e.button); wmi.storeU16(off(2), e.buttons); } else if (e instanceof KeyboardEvent) { - let keyOffset = off(W*2, W); - let codeOffet = off(W*2, W); + // NOTE: we set string data pointers on the + // native side, so skip those for now and + // set only string length + const keyPtr = off(W); + wmi.storeI32(off(W), e.key.length); + const codePtr = off(W); + wmi.storeI32(off(W), e.code.length); + wmi.storeU8(off(1), e.location); wmi.storeU8(off(1), !!e.ctrlKey); @@ -1427,6 +1439,9 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { wmi.storeU8(off(1), !!e.metaKey); wmi.storeU8(off(1), !!e.repeat); + + wmi.storeString(off(16, 1), e.key); + wmi.storeString(off(16, 1), e.code); } else if (e instanceof WheelEvent) { wmi.storeF64(off(8), e.deltaX); wmi.storeF64(off(8), e.deltaY); From b185d9e70151b6e47282cbb6a730a0ae48ea778b Mon Sep 17 00:00:00 2001 From: Caedo Date: Wed, 16 Aug 2023 20:33:17 +0200 Subject: [PATCH 2/2] Remove pointer magic when creating strings --- vendor/wasm/js/events.odin | 11 ++++------- vendor/wasm/js/runtime.js | 14 +++++++------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/vendor/wasm/js/events.odin b/vendor/wasm/js/events.odin index c9a11c691..7c5dc39d6 100644 --- a/vendor/wasm/js/events.odin +++ b/vendor/wasm/js/events.odin @@ -1,8 +1,6 @@ //+build js wasm32, js wasm64p32 package wasm_js_interface -import "core:runtime" - foreign import dom_lib "odin_dom" Event_Kind :: enum u32 { @@ -235,6 +233,8 @@ Event :: struct { repeat: bool, + _key_len: int, + _code_len: int, _key_buf: [KEYBOARD_MAX_KEY_SIZE]byte, _code_buf: [KEYBOARD_MAX_KEY_SIZE]byte, }, @@ -350,11 +350,8 @@ do_event_callback :: proc(user_data: rawptr, callback: proc(e: Event)) { init_event_raw(&event) if event.kind == .Key_Up || event.kind == .Key_Down || event.kind == .Key_Press { - key := transmute(^runtime.Raw_String) &event.key.key - key.data = &event.key._key_buf[0] - - code := transmute(^runtime.Raw_String) &event.key.code - code.data = &event.key._code_buf[0] + event.key.key = string(event.key._key_buf[:event.key._key_len]) + event.key.code = string(event.key._code_buf[:event.key._code_len]) } callback(event) diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index 353bc521d..5980f8c6d 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1423,13 +1423,11 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { wmi.storeI16(off(2), e.button); wmi.storeU16(off(2), e.buttons); } else if (e instanceof KeyboardEvent) { - // NOTE: we set string data pointers on the - // native side, so skip those for now and - // set only string length - const keyPtr = off(W); - wmi.storeI32(off(W), e.key.length); - const codePtr = off(W); - wmi.storeI32(off(W), e.code.length); + // Note: those strigs are constructed + // on the native side from buffers that + // are filled later, so skip them + const keyPtr = off(W*2, W); + const codePtr = off(W*2, W); wmi.storeU8(off(1), e.location); @@ -1440,6 +1438,8 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { wmi.storeU8(off(1), !!e.repeat); + wmi.storeI32(off(W), e.key.length) + wmi.storeI32(off(W), e.code.length) wmi.storeString(off(16, 1), e.key); wmi.storeString(off(16, 1), e.code); } else if (e instanceof WheelEvent) {