1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use std::borrow::Cow;

use crate::{
    builtins::{
        error::ErrorObject, map::ordered_map::OrderedMap, promise::PromiseState,
        set::ordered_set::OrderedSet, Array, Promise,
    },
    property::PropertyDescriptor,
    string::utf16,
    JsError, JsString,
};

use super::{fmt, Display, HashSet, JsValue};

/// This object is used for displaying a `Value`.
#[derive(Debug, Clone, Copy)]
pub struct ValueDisplay<'value> {
    pub(super) value: &'value JsValue,
    pub(super) internals: bool,
}

impl ValueDisplay<'_> {
    /// Display internal information about value.
    ///
    /// By default this is `false`.
    #[inline]
    #[must_use]
    pub const fn internals(mut self, yes: bool) -> Self {
        self.internals = yes;
        self
    }
}

/// A helper macro for printing objects
/// Can be used to print both properties and internal slots
/// All of the overloads take:
/// - The object to be printed
/// - The function with which to print
/// - The indentation for the current level (for nested objects)
/// - A `HashSet` with the addresses of the already printed objects for the current branch
///      (used to avoid infinite loops when there are cyclic deps)
macro_rules! print_obj_value {
    (all of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr) => {
        {
            let mut internals = print_obj_value!(internals of $obj, $display_fn, $indent, $encounters);
            let mut props = print_obj_value!(props of $obj, $display_fn, $indent, $encounters, true);

            props.reserve(internals.len());
            props.append(&mut internals);

            props
        }
    };
    (internals of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr) => {
        {
            let object = $obj.borrow();
            if let Some(object) = object.prototype() {
                vec![format!(
                    "{:>width$}: {}",
                    "__proto__",
                    $display_fn(&object.clone().into(), $encounters, $indent.wrapping_add(4), true),
                    width = $indent,
                )]
            } else {
                vec![format!(
                    "{:>width$}: {}",
                    "__proto__",
                    JsValue::Null.display(),
                    width = $indent,
                )]
            }
        }
    };
    (props of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr, $print_internals:expr) => {
        {let mut keys: Vec<_> = $obj.borrow().properties().index_property_keys().map(crate::property::PropertyKey::from).collect();
        keys.extend($obj.borrow().properties().shape.keys());
        let mut result = Vec::default();
        for key in keys {
            let val = $obj.borrow().properties().get(&key).expect("There should be a value");
            if val.is_data_descriptor() {
                let v = &val.expect_value();
                result.push(format!(
                    "{:>width$}: {}",
                    key,
                    $display_fn(v, $encounters, $indent.wrapping_add(4), $print_internals),
                    width = $indent,
                ));
            } else {
               let display = match (val.set().is_some(), val.get().is_some()) {
                    (true, true) => "Getter & Setter",
                    (true, false) => "Setter",
                    (false, true) => "Getter",
                    _ => "No Getter/Setter"
                };
               result.push(format!("{:>width$}: {}", key, display, width = $indent));
            }
        }
        result}
    };
}

pub(crate) fn log_string_from(x: &JsValue, print_internals: bool, print_children: bool) -> String {
    match x {
        // We don't want to print private (compiler) or prototype properties
        JsValue::Object(ref v) => {
            // Can use the private "type" field of an Object to match on
            // which type of Object it represents for special printing
            let v_bor = v.borrow();
            if let Some(s) = v_bor.downcast_ref::<JsString>() {
                format!("String {{ \"{}\" }}", s.to_std_string_escaped())
            } else if let Some(b) = v_bor.downcast_ref::<bool>() {
                format!("Boolean {{ {b} }}")
            } else if let Some(r) = v_bor.downcast_ref::<f64>() {
                if r.is_sign_negative() && *r == 0.0 {
                    "Number { -0 }".to_string()
                } else {
                    let mut buffer = ryu_js::Buffer::new();
                    format!("Number {{ {} }}", buffer.format(*r))
                }
            } else if v_bor.is::<Array>() {
                let len = v_bor
                    .properties()
                    .get(&utf16!("length").into())
                    .expect("array object must have 'length' property")
                    // FIXME: handle accessor descriptors
                    .expect_value()
                    .as_number()
                    .map(|n| n as i32)
                    .unwrap_or_default();

                if print_children {
                    if len == 0 {
                        return String::from("[]");
                    }

                    let arr = (0..len)
                        .map(|i| {
                            // Introduce recursive call to stringify any objects
                            // which are part of the Array

                            // FIXME: handle accessor descriptors
                            if let Some(value) = v_bor
                                .properties()
                                .get(&i.into())
                                .and_then(|x| x.value().cloned())
                            {
                                log_string_from(&value, print_internals, false)
                            } else {
                                String::from("<empty>")
                            }
                        })
                        .collect::<Vec<String>>()
                        .join(", ");

                    format!("[ {arr} ]")
                } else {
                    format!("Array({len})")
                }
            } else if let Some(map) = v_bor.downcast_ref::<OrderedMap<JsValue>>() {
                let size = map.len();
                if size == 0 {
                    return String::from("Map(0)");
                }

                if print_children {
                    let mappings = map
                        .iter()
                        .map(|(key, value)| {
                            let key = log_string_from(key, print_internals, false);
                            let value = log_string_from(value, print_internals, false);
                            format!("{key} → {value}")
                        })
                        .collect::<Vec<String>>()
                        .join(", ");
                    format!("Map {{ {mappings} }}")
                } else {
                    format!("Map({size})")
                }
            } else if let Some(set) = v_bor.downcast_ref::<OrderedSet>() {
                let size = set.len();

                if size == 0 {
                    return String::from("Set(0)");
                }

                if print_children {
                    let entries = set
                        .iter()
                        .map(|value| log_string_from(value, print_internals, false))
                        .collect::<Vec<String>>()
                        .join(", ");
                    format!("Set {{ {entries} }}")
                } else {
                    format!("Set({size})")
                }
            } else if v_bor.is::<ErrorObject>() {
                drop(v_bor);
                let name: Cow<'static, str> = v
                    .get_property(&utf16!("name").into())
                    .as_ref()
                    .and_then(PropertyDescriptor::value)
                    .map_or_else(
                        || "<error>".into(),
                        |v| {
                            v.as_string()
                                .map_or_else(
                                    || v.display().to_string(),
                                    JsString::to_std_string_escaped,
                                )
                                .into()
                        },
                    );
                let message = v
                    .get_property(&utf16!("message").into())
                    .as_ref()
                    .and_then(PropertyDescriptor::value)
                    .map(|v| {
                        v.as_string().map_or_else(
                            || v.display().to_string(),
                            JsString::to_std_string_escaped,
                        )
                    })
                    .unwrap_or_default();
                if name.is_empty() {
                    message
                } else if message.is_empty() {
                    name.to_string()
                } else {
                    format!("{name}: {message}")
                }
            } else if let Some(promise) = v_bor.downcast_ref::<Promise>() {
                format!(
                    "Promise {{ {} }}",
                    match promise.state() {
                        PromiseState::Pending => Cow::Borrowed("<pending>"),
                        PromiseState::Fulfilled(val) => Cow::Owned(val.display().to_string()),
                        PromiseState::Rejected(reason) => Cow::Owned(format!(
                            "<rejected> {}",
                            JsError::from_opaque(reason.clone())
                        )),
                    }
                )
            } else {
                x.display_obj(print_internals)
            }
        }
        _ => x.display().to_string(),
    }
}

impl JsValue {
    /// A helper function for specifically printing object values
    #[must_use]
    pub fn display_obj(&self, print_internals: bool) -> String {
        // A simple helper for getting the address of a value
        // TODO: Find a more general place for this, as it can be used in other situations as well
        fn address_of<T: ?Sized>(t: &T) -> usize {
            let my_ptr: *const T = t;
            my_ptr.cast::<()>() as usize
        }

        fn display_obj_internal(
            data: &JsValue,
            encounters: &mut HashSet<usize>,
            indent: usize,
            print_internals: bool,
        ) -> String {
            if let JsValue::Object(ref v) = *data {
                // The in-memory address of the current object
                let addr = address_of(v.as_ref());

                // We need not continue if this object has already been
                // printed up the current chain
                if encounters.contains(&addr) {
                    return String::from("[Cycle]");
                }

                // Mark the current object as encountered
                encounters.insert(addr);

                let result = if print_internals {
                    print_obj_value!(all of v, display_obj_internal, indent, encounters).join(",\n")
                } else {
                    print_obj_value!(props of v, display_obj_internal, indent, encounters, print_internals)
                        .join(",\n")
                };

                // If the current object is referenced in a different branch,
                // it will not cause an infinite printing loop, so it is safe to be printed again
                encounters.remove(&addr);

                let closing_indent = String::from_utf8(vec![b' '; indent.wrapping_sub(4)])
                    .expect("Could not create the closing brace's indentation string");

                format!("{{\n{result}\n{closing_indent}}}")
            } else {
                // Every other type of data is printed with the display method
                data.display().to_string()
            }
        }

        // We keep track of which objects we have encountered by keeping their
        // in-memory address in this set
        let mut encounters = HashSet::new();

        display_obj_internal(self, &mut encounters, 4, print_internals)
    }
}

impl Display for ValueDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.value {
            JsValue::Null => write!(f, "null"),
            JsValue::Undefined => write!(f, "undefined"),
            JsValue::Boolean(v) => write!(f, "{v}"),
            JsValue::Symbol(ref symbol) => {
                write!(f, "{}", symbol.descriptive_string().to_std_string_escaped())
            }
            JsValue::String(ref v) => write!(f, "\"{}\"", v.to_std_string_escaped()),
            JsValue::Rational(v) => format_rational(*v, f),
            JsValue::Object(_) => {
                write!(f, "{}", log_string_from(self.value, self.internals, true))
            }
            JsValue::Integer(v) => write!(f, "{v}"),
            JsValue::BigInt(ref num) => write!(f, "{num}n"),
        }
    }
}

/// This is different from the ECMAScript compliant number to string, in the printing of `-0`.
///
/// This function prints `-0` as `-0` instead of positive `0` as the specification says.
/// This is done to make it easer for the user of the REPL to identify what is a `-0` vs `0`,
/// since the REPL is not bound to the ECMAScript specification we can do this.
fn format_rational(v: f64, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    if v.is_sign_negative() && v == 0.0 {
        f.write_str("-0")
    } else {
        let mut buffer = ryu_js::Buffer::new();
        write!(f, "{}", buffer.format(v))
    }
}