Skip to main content

pipa/host/
context.rs

1use crate::object::native::NativeFunc;
2use crate::runtime::atom::Atom;
3use crate::runtime::context::JSContext;
4use crate::value::JSValue;
5
6pub trait HostContext {
7    fn register_host_object<T: HostObject>(&mut self, name: &str, obj: T);
8    fn register_native_func(&mut self, name: &str, func: NativeFunc);
9    fn create_dom_node(&mut self, node: DomNodeHandle) -> JSValue;
10}
11
12pub struct DomNodeHandle(u64);
13
14impl DomNodeHandle {
15    pub fn new(id: u64) -> Self {
16        DomNodeHandle(id)
17    }
18
19    pub fn id(&self) -> u64 {
20        self.0
21    }
22}
23
24pub trait HostObject: 'static {
25    fn get(&self, ctx: &JSContext, prop: Atom) -> JSValue;
26    fn set(&mut self, ctx: &mut JSContext, prop: Atom, value: JSValue) -> Result<(), ()>;
27    fn call(&mut self, ctx: &mut JSContext, args: &[JSValue]) -> JSValue;
28    fn construct(&mut self, ctx: &mut JSContext, args: &[JSValue]) -> JSValue;
29}