quickjs_jit_stdlib/llrt/llrt_utils/
class.rs1use rquickjs::{
6 atom::PredefinedAtom, class::JsClass, object::Accessor, object::Property, prelude::This, Array,
7 Class, Ctx, Function, Object, Result, Symbol, Value,
8};
9
10use super::{
11 object::ObjectExt,
12 primordials::{BasePrimordials, Primordial},
13 result::OptionExt,
14};
15
16pub static CUSTOM_INSPECT_SYMBOL_DESCRIPTION: &str = "llrt.inspect.custom";
17
18#[derive(Clone, Copy)]
20pub enum IterKind {
21 Keys,
22 Values,
23 Entries,
24}
25
26pub fn iterator_result<'js>(
28 ctx: &Ctx<'js>,
29 kind: IterKind,
30 entry: Option<(Value<'js>, Value<'js>)>,
31) -> Result<Object<'js>> {
32 let obj = Object::new(ctx.clone())?;
33 match entry {
34 Some((key, value)) => {
35 obj.set(PredefinedAtom::Done, false)?;
36 match kind {
37 IterKind::Keys => obj.set(PredefinedAtom::Value, key)?,
38 IterKind::Values => obj.set(PredefinedAtom::Value, value)?,
39 IterKind::Entries => {
40 let entry = Array::new(ctx.clone())?;
41 entry.set(0, key)?;
42 entry.set(1, value)?;
43 obj.set(PredefinedAtom::Value, entry)?;
44 }
45 }
46 }
47 None => obj.set(PredefinedAtom::Done, true)?,
48 }
49 Ok(obj)
50}
51
52pub fn live_iterator<'js, C>(ctx: &Ctx<'js>, iter: C) -> Result<Class<'js, C>>
58where
59 C: JsClass<'js> + 'js,
60{
61 let instance = Class::<C>::instance(ctx.clone(), iter)?;
62 if let Some(proto) = Class::<C>::prototype(ctx)? {
63 let iterator_proto = &BasePrimordials::get(ctx)?.prototype_iterator;
64 if proto.get_prototype().as_ref() != Some(iterator_proto) {
65 proto.set_prototype(Some(iterator_proto))?;
66 let next_fn: Function = proto.get(PredefinedAtom::Next)?;
67 proto.prop(
68 PredefinedAtom::Next,
69 Property::from(next_fn)
70 .writable()
71 .enumerable()
72 .configurable(),
73 )?;
74 }
75 }
76 Ok(instance)
77}
78
79pub fn get_class_name(value: &Value) -> Result<Option<String>> {
80 value
81 .get_optional::<_, Object>(PredefinedAtom::Constructor)?
82 .and_then_ok(|ctor| ctor.get_optional::<_, String>(PredefinedAtom::Name))
83}
84
85#[inline(always)]
86pub fn get_class<'js, C>(provided: &Value<'js>) -> Result<Option<Class<'js, C>>>
87where
88 C: JsClass<'js>,
89{
90 if provided
91 .as_object()
92 .map(|p| p.instance_of::<C>())
93 .unwrap_or_default()
94 {
95 return Ok(Some(Class::<C>::from_value(provided)?));
96 }
97 Ok(None)
98}
99
100pub trait CustomInspectExtension<'js> {
101 fn define_with_custom_inspect(globals: &Object<'js>) -> Result<()>;
102}
103
104pub trait CustomInspect<'js>
105where
106 Self: JsClass<'js>,
107{
108 fn custom_inspect(&self, ctx: Ctx<'js>) -> Result<Object<'js>>;
109}
110
111impl<'js, C> CustomInspectExtension<'js> for Class<'js, C>
112where
113 C: JsClass<'js> + CustomInspect<'js> + 'js,
114{
115 fn define_with_custom_inspect(globals: &Object<'js>) -> Result<()> {
116 Self::define(globals)?;
117 let custom_inspect_symbol =
118 Symbol::new_global(globals.ctx().clone(), CUSTOM_INSPECT_SYMBOL_DESCRIPTION)?;
119 if let Some(proto) = Class::<C>::prototype(globals.ctx())? {
120 proto.prop(
121 custom_inspect_symbol,
122 Accessor::from(|this: This<Class<'js, C>>, ctx| this.borrow().custom_inspect(ctx)),
123 )?;
124 }
125 Ok(())
126 }
127}