1use alloc::rc::Rc;
9use alloc::string::String;
10use alloc::vec::Vec;
11use core::any::Any;
12use core::cell::RefCell;
13
14use super::interp::{Interp, JsRuntime, Scope};
15use super::value::{HostRef, ObjKind, ObjRef, Value};
16
17pub enum HostProbe {
19 Skip,
21 Own(Value),
23 Proto(Option<ObjRef>),
25}
26
27pub enum HostOwn {
29 Generic,
31 Absent,
33 Accessor(Option<Value>, Option<Value>),
35 Data(Value),
37}
38
39pub enum HostKeys {
41 Generic,
43 Internal,
45 Entries(Vec<(String, String)>),
47}
48
49pub type HostDefineFn =
50 fn(&mut Interp, HostRef, &str, Option<Value>, Option<Value>, Option<Value>) -> bool;
51pub type HostConstructElementFn =
52 fn(&mut Interp, &Value, &[Value], &Value) -> Option<Result<Value, Value>>;
53
54pub struct HostHooks {
56 pub get: fn(&mut Interp, &ObjRef, HostRef, &str) -> Option<Value>,
58 pub set: fn(&mut Interp, &ObjRef, HostRef, &str, Value) -> Option<Value>,
60 pub delete: fn(&mut Interp, HostRef, &str) -> Option<bool>,
62 pub keys: fn(&Interp, HostRef) -> HostKeys,
64 pub iterate: fn(&Interp, HostRef, bool) -> Option<Vec<Value>>,
66 pub resolves_on_read: fn(HostRef) -> bool,
68 pub proto: fn(&Interp, HostRef) -> Option<Option<ObjRef>>,
71 pub set_proto: fn(&mut Interp, HostRef, &ObjRef) -> bool,
73 pub probe: fn(&Interp, HostRef, &str, bool) -> HostProbe,
75 pub own: fn(&Interp, HostRef, &str) -> HostOwn,
77 pub define: HostDefineFn,
79 pub describe: fn(&Interp, HostRef) -> Option<String>,
81 pub class_name: fn(HostRef) -> (&'static str, &'static str),
83 pub construct_element: HostConstructElementFn,
85 pub gc_cleanup: fn(&Interp),
87 pub after_call: fn(&mut Interp),
89 pub new_interp_state: fn(&dyn Any) -> Rc<dyn Any>,
91 pub init_runtime: fn(&mut JsRuntime),
93 pub teardown_runtime: fn(u64, &dyn Any, &mut Vec<Value>),
95 pub current_base_url: fn(&Rc<RefCell<Scope>>) -> Option<String>,
97 pub has_ready_async_io: fn(&dyn Any) -> bool,
99 pub has_pending_async_io: fn(&dyn Any) -> bool,
101 pub pump_async_io: fn(&mut Interp) -> bool,
103 pub after_eval: fn(&mut Interp),
105 pub after_pump: fn(&mut Interp),
107 pub special_get: fn(&mut Interp, &ObjRef, &str) -> Option<Value>,
109 pub special_set: fn(&mut Interp, &ObjRef, &str, &Value) -> bool,
111}
112
113pub static NULL_HOST_HOOKS: HostHooks = HostHooks {
115 get: |_, _, _, _| None,
116 set: |_, _, _, _, v| Some(v),
117 delete: |_, _, _| None,
118 keys: |_, _| HostKeys::Generic,
119 iterate: |_, _, _| None,
120 resolves_on_read: |_| false,
121 proto: |_, _| None,
122 set_proto: |_, _, _| false,
123 probe: |_, _, _, _| HostProbe::Skip,
124 own: |_, _, _| HostOwn::Generic,
125 define: |_, _, _, _, _, _| false,
126 describe: |_, _| None,
127 class_name: |_| ("host", "host object"),
128 construct_element: |_, _, _, _| None,
129 gc_cleanup: |_| {},
130 after_call: |_| {},
131 new_interp_state: |_| Rc::new(()),
132 init_runtime: |_| {},
133 teardown_runtime: |_, _, _| {},
134 current_base_url: |_| None,
135 has_ready_async_io: |_| false,
136 has_pending_async_io: |_| false,
137 pump_async_io: |_| false,
138 after_eval: |_| {},
139 after_pump: |_| {},
140 special_get: |_, _, _| None,
141 special_set: |_, _, _, _| false,
142};
143
144impl Interp {
145 #[inline]
147 pub(crate) fn host_get(&mut self, o: &ObjRef, h: HostRef, key: &str) -> Option<Value> {
148 (self.host.get)(self, o, h, key)
149 }
150}
151
152impl Interp {
153 #[inline]
155 pub(crate) fn host_proto_of(&self, kind: &ObjKind) -> Option<Option<ObjRef>> {
156 if matches!(kind, ObjKind::RegExpObj(_)) {
157 return Some(super::builtins::global_ctor_proto(self, "RegExp"));
159 }
160 kind.host_ref().and_then(|h| (self.host.proto)(self, h))
161 }
162}
163
164impl Interp {
165 #[inline]
167 pub(crate) fn host_keys_of(&self, kind: &ObjKind) -> HostKeys {
168 match kind {
169 ObjKind::HostRef(h) => (self.host.keys)(self, *h),
170 ObjKind::Iter | ObjKind::GenReturn => HostKeys::Internal,
171 _ => HostKeys::Generic,
172 }
173 }
174}