pub trait NativeHost {
type Guest: NativeGuest;
// Required methods
fn guest(&self) -> &Self::Guest;
fn guest_mut(&mut self) -> &mut Self::Guest;
// Provided method
fn invoke_retained_callback(
&mut self,
callback: &RetainedCallback<<Self::Guest as NativeGuest>::RealmId, <Self::Guest as NativeGuest>::CallbackId>,
arguments: &[<Self::Guest as NativeGuest>::Value],
) -> Result<<Self::Guest as NativeGuest>::Value, NativeCallbackError<<Self::Guest as NativeGuest>::Error>> { ... }
}Expand description
A host-side owner that can dispatch calls to a native guest.
Required Associated Types§
Sourcetype Guest: NativeGuest
type Guest: NativeGuest
The guest implementation owned by this host.
Required Methods§
Provided Methods§
Sourcefn invoke_retained_callback(
&mut self,
callback: &RetainedCallback<<Self::Guest as NativeGuest>::RealmId, <Self::Guest as NativeGuest>::CallbackId>,
arguments: &[<Self::Guest as NativeGuest>::Value],
) -> Result<<Self::Guest as NativeGuest>::Value, NativeCallbackError<<Self::Guest as NativeGuest>::Error>>
fn invoke_retained_callback( &mut self, callback: &RetainedCallback<<Self::Guest as NativeGuest>::RealmId, <Self::Guest as NativeGuest>::CallbackId>, arguments: &[<Self::Guest as NativeGuest>::Value], ) -> Result<<Self::Guest as NativeGuest>::Value, NativeCallbackError<<Self::Guest as NativeGuest>::Error>>
Dispatch a typed call to a retained callback.
Rejects mismatched realm or generation metadata before entering the guest. The guest implementation remains responsible for rejecting an unknown callback identifier before callback code runs.
Examples found in repository?
examples/native_document.rs (line 117)
102 fn execute_source(
103 &mut self,
104 doc: &mut BaseDocument,
105 source: &str,
106 _: Option<&Url>,
107 kind: &ScriptKind,
108 ) -> Result<(), Self::Error> {
109 // This fixture has one precompiled entry. Unknown input is rejected,
110 // never interpreted or reported as successful JavaScript execution.
111 if source != "paint()" || *kind != ScriptKind::Classic {
112 return Err("unknown compiled fixture");
113 }
114 self.trace.borrow_mut().push("execute");
115 let seed = *self.storage.get("seed").ok_or("storage was not seeded")?;
116 let callback = self.retain_callback(&7)?;
117 let result = NativeHost::invoke_retained_callback(self, &callback, &[seed])
118 .map_err(|_| "compiled call failed")?;
119 self.release_callback(&callback)?;
120 if !self.omit_write {
121 let subject = doc
122 .query_selector("#subject")
123 .map_err(|_| "invalid selector")?
124 .ok_or("missing subject")?;
125 let text = *doc
126 .get_node(subject)
127 .ok_or("missing node")?
128 .children
129 .first()
130 .ok_or("missing text")?;
131 doc.mutate().set_node_text(text, &result.to_string());
132 }
133 Ok(())
134 }More examples
examples/native_host.rs (line 114)
96fn main() {
97 let mut guest = Guest {
98 scope: RealmScope::new(Rc::new(1), 1),
99 root: None,
100 intrinsic: None,
101 calls: 0,
102 };
103 guest
104 .register_intrinsic("increment", 1, |_, arguments| {
105 arguments
106 .first()
107 .copied()
108 .map(|value| value + 1)
109 .ok_or("missing argument")
110 })
111 .expect("register compiled intrinsic");
112 let callback = guest.retain_callback(&7).expect("retain callback");
113 assert_eq!(
114 NativeHost::invoke_retained_callback(&mut guest, &callback, &[5]),
115 Ok(6)
116 );
117 let foreign = RetainedCallback::new(Rc::new(2), 1, Rc::clone(callback.callback()));
118 assert_eq!(
119 NativeHost::invoke_retained_callback(&mut guest, &foreign, &[5]),
120 Err(NativeCallbackError::StaleCallback)
121 );
122 assert_eq!(
123 guest.calls, 1,
124 "foreign scope must not enter guest dispatch"
125 );
126 guest.release_callback(&callback).expect("release callback");
127 assert!(NativeHost::invoke_retained_callback(&mut guest, &callback.clone(), &[5]).is_err());
128 assert_eq!(guest.calls, 1, "release must revoke cloned tokens");
129 let next = guest.retain_callback(&7).expect("retain next callback");
130 guest.scope = RealmScope::new(Rc::clone(guest.scope.realm()), 2);
131 assert_eq!(
132 NativeHost::invoke_retained_callback(&mut guest, &next, &[5]),
133 Err(NativeCallbackError::StaleCallback)
134 );
135 assert_eq!(
136 guest.calls, 1,
137 "stale generation must not enter guest dispatch"
138 );
139 println!("typed native dispatch, root release, realm and generation checks passed");
140}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".