Add protocol names to host-side panel binding

This commit is contained in:
mars 2022-11-15 21:48:07 -07:00
parent e5f9985bb9
commit 7c738c24ec
3 changed files with 13 additions and 10 deletions

View File

@ -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<u8>) -> u32;
fn bind_panel(&self, panel: PanelId, protocol: String, msg: Vec<u8>) -> u32;
fn update(&self, panel_ud: u32, dt: f32);

View File

@ -61,7 +61,7 @@ impl Backend for WasmtimeBackend {
pub struct WasmtimeInstance {
store: Mutex<Store>,
bind_panel: wasmtime::TypedFunc<(u32, u32), u32>,
bind_panel: wasmtime::TypedFunc<(u32, u32, u32), u32>,
update: wasmtime::TypedFunc<(u32, f32), ()>,
draw: wasmtime::TypedFunc<u32, ()>,
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<u8>) -> u32 {
fn bind_panel(&self, panel: PanelId, protocol: String, msg: Vec<u8>) -> 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
}

View File

@ -45,10 +45,10 @@ pub struct Script {
}
impl Script {
pub fn create_panel(&mut self, msg: Vec<u8>) -> anyhow::Result<Panel> {
pub fn create_panel(&mut self, protocol: String, msg: Vec<u8>) -> anyhow::Result<Panel> {
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,