From 7c738c24ec29dd71d2dbe20abd602b5986aa1512 Mon Sep 17 00:00:00 2001 From: mars Date: Tue, 15 Nov 2022 21:48:07 -0700 Subject: [PATCH] Add protocol names to host-side panel binding --- src/backend/mod.rs | 11 ++++++----- src/backend/wasmtime.rs | 8 +++++--- src/lib.rs | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index ca0333f..b1a5859 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -36,10 +36,11 @@ pub trait Instance { /// Binds script data to a Canary panel. /// /// To "bind" a Canary panel to a Canary script, this function must be - /// called. It passes the ID of a panel to the script, plus an - /// initialization message, and the script returns an integer as - /// userdata. All panel events will be identified to the script with this - /// userdata as the first argument. + /// called. It passes the ID of a panel to the script, the name of the + /// protocol that this panel will be using, plus an initialization + /// message, and the script returns an integer as userdata. All panel + /// events will be identified to the script with this userdata as the first + /// argument. /// /// The intended usecase for this userdata is to contain a pointer. A /// Canary script can allocate some high-level object in memory, and when @@ -47,7 +48,7 @@ pub trait Instance { /// userdata. Then, when the runtime calls back into the script, the /// userdata will be reinterpreted as a pointer and a method can be called /// on that object in memory. - fn bind_panel(&self, panel: PanelId, msg: Vec) -> u32; + fn bind_panel(&self, panel: PanelId, protocol: String, msg: Vec) -> u32; fn update(&self, panel_ud: u32, dt: f32); diff --git a/src/backend/wasmtime.rs b/src/backend/wasmtime.rs index 73057d8..b6a82b9 100644 --- a/src/backend/wasmtime.rs +++ b/src/backend/wasmtime.rs @@ -61,7 +61,7 @@ impl Backend for WasmtimeBackend { pub struct WasmtimeInstance { store: Mutex, - bind_panel: wasmtime::TypedFunc<(u32, u32), u32>, + bind_panel: wasmtime::TypedFunc<(u32, u32, u32), u32>, update: wasmtime::TypedFunc<(u32, f32), ()>, draw: wasmtime::TypedFunc, on_resize: wasmtime::TypedFunc<(u32, f32, f32), ()>, @@ -189,11 +189,13 @@ impl WasmtimeInstance { } impl Instance for WasmtimeInstance { - fn bind_panel(&self, panel: PanelId, msg: Vec) -> u32 { + fn bind_panel(&self, panel: PanelId, protocol: String, msg: Vec) -> u32 { let mut store = self.store.lock(); + let protocol = store.data().message_new(protocol.as_bytes().to_vec()); let msg = store.data().message_new(msg); - let args = (panel.0 as u32, msg); + let args = (panel.0 as u32, protocol, msg); let data = self.bind_panel.call(store.deref_mut(), args).unwrap(); + store.data().message_free(protocol); store.data().message_free(msg); data } diff --git a/src/lib.rs b/src/lib.rs index 81c5b98..cb9a03c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,10 +45,10 @@ pub struct Script { } impl Script { - pub fn create_panel(&mut self, msg: Vec) -> anyhow::Result { + pub fn create_panel(&mut self, protocol: String, msg: Vec) -> anyhow::Result { let id = PanelId(self.next_panel); self.next_panel += 1; - let userdata = self.instance.bind_panel(id, msg); + let userdata = self.instance.bind_panel(id, protocol, msg); Ok(Panel { instance: self.instance.clone(), id,