boa_engine 0.17.0

Boa is a Javascript lexer, parser and compiler written in Rust. Currently, it has support for some of the language.
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::fmt::Write;

use boa_profiler::Profiler;
use icu_list::{provider::AndListV1Marker, ListFormatter, ListLength};
use icu_locid::Locale;
use icu_provider::DataLocale;

use crate::{
    builtins::{Array, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
    context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
    object::{internal_methods::get_prototype_from_constructor, JsObject, ObjectData},
    property::Attribute,
    realm::Realm,
    string::utf16,
    symbol::JsSymbol,
    Context, JsArgs, JsNativeError, JsResult, JsString, JsValue,
};

use super::{
    locale::{canonicalize_locale_list, resolve_locale, supported_locales},
    options::{get_option, get_options_object, IntlOptions, LocaleMatcher},
    Service,
};

mod options;
pub(crate) use options::*;
pub struct ListFormat {
    locale: Locale,
    typ: ListFormatType,
    style: ListLength,
    formatter: ListFormatter,
}

impl std::fmt::Debug for ListFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ListFormat")
            .field("locale", &self.locale)
            .field("typ", &self.typ)
            .field("style", &self.style)
            .field("formatter", &"ListFormatter")
            .finish()
    }
}

impl Service for ListFormat {
    type LangMarker = AndListV1Marker;

    type LocaleOptions = ();
}

impl IntrinsicObject for ListFormat {
    fn init(realm: &Realm) {
        let _timer = Profiler::global().start_event(Self::NAME, "init");

        BuiltInBuilder::from_standard_constructor::<Self>(realm)
            .static_method(Self::supported_locales_of, "supportedLocalesOf", 1)
            .property(
                JsSymbol::to_string_tag(),
                "Intl.ListFormat",
                Attribute::CONFIGURABLE,
            )
            .method(Self::format, "format", 1)
            .method(Self::format_to_parts, "formatToParts", 1)
            .method(Self::resolved_options, "resolvedOptions", 0)
            .build();
    }

    fn get(intrinsics: &Intrinsics) -> JsObject {
        Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
    }
}

impl BuiltInObject for ListFormat {
    const NAME: &'static str = "ListFormat";
}

impl BuiltInConstructor for ListFormat {
    const LENGTH: usize = 0;

    const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
        StandardConstructors::list_format;

    /// Constructor [`Intl.ListFormat ( [ locales [ , options ] ] )`][spec].
    ///
    /// Constructor for `ListFormat` objects.
    ///
    /// More information:
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma402/#sec-Intl.ListFormat
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat
    fn constructor(
        new_target: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. If NewTarget is undefined, throw a TypeError exception.
        if new_target.is_undefined() {
            return Err(JsNativeError::typ()
                .with_message("cannot call `Intl.ListFormat` constructor without `new`")
                .into());
        }

        let locales = args.get_or_undefined(0);
        let options = args.get_or_undefined(1);

        // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
        let requested_locales = canonicalize_locale_list(locales, context)?;

        // 4. Set options to ? GetOptionsObject(options).
        let options = get_options_object(options)?;

        // 5. Let opt be a new Record.
        // 6. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
        let matcher =
            get_option::<LocaleMatcher>(&options, utf16!("localeMatcher"), false, context)?
                .unwrap_or_default();

        // 7. Set opt.[[localeMatcher]] to matcher.
        // 8. Let localeData be %ListFormat%.[[LocaleData]].
        // 9. Let r be ResolveLocale(%ListFormat%.[[AvailableLocales]], requestedLocales, opt, %ListFormat%.[[RelevantExtensionKeys]], localeData).
        // 10. Set listFormat.[[Locale]] to r.[[locale]].
        let locale = resolve_locale::<Self>(
            &requested_locales,
            &mut IntlOptions {
                matcher,
                ..Default::default()
            },
            context.icu(),
        );

        // 11. Let type be ? GetOption(options, "type", string, « "conjunction", "disjunction", "unit" », "conjunction").
        // 12. Set listFormat.[[Type]] to type.
        let typ = get_option::<ListFormatType>(&options, utf16!("type"), false, context)?
            .unwrap_or_default();

        // 13. Let style be ? GetOption(options, "style", string, « "long", "short", "narrow" », "long").
        // 14. Set listFormat.[[Style]] to style.
        let style = get_option::<ListLength>(&options, utf16!("style"), false, context)?
            .unwrap_or(ListLength::Wide);

        // 15. Let dataLocale be r.[[dataLocale]].
        // 16. Let dataLocaleData be localeData.[[<dataLocale>]].
        // 17. Let dataLocaleTypes be dataLocaleData.[[<type>]].
        // 18. Set listFormat.[[Templates]] to dataLocaleTypes.[[<style>]].

        // 2. Let listFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%ListFormat.prototype%", « [[InitializedListFormat]], [[Locale]], [[Type]], [[Style]], [[Templates]] »).
        let prototype =
            get_prototype_from_constructor(new_target, StandardConstructors::list_format, context)?;
        let list_format = JsObject::from_proto_and_data_with_shared_shape(
            context.root_shape(),
            prototype,
            ObjectData::list_format(Self {
                formatter: context
                    .icu()
                    .provider()
                    .try_new_list_formatter(&DataLocale::from(&locale), typ, style)
                    .map_err(|e| JsNativeError::typ().with_message(e.to_string()))?,
                locale,
                typ,
                style,
            }),
        );

        // 19. Return listFormat.
        Ok(list_format.into())
    }
}

impl ListFormat {
    /// [`Intl.ListFormat.supportedLocalesOf ( locales [ , options ] )`][spec].
    ///
    /// Returns an array containing those of the provided locales that are supported in list
    /// formatting without having to fall back to the runtime's default locale.
    ///
    /// More information:
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma402/#sec-Intl.ListFormat.supportedLocalesOf
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/supportedLocalesOf
    fn supported_locales_of(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        let locales = args.get_or_undefined(0);
        let options = args.get_or_undefined(1);

        // 1. Let availableLocales be %ListFormat%.[[AvailableLocales]].
        // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
        let requested_locales = canonicalize_locale_list(locales, context)?;

        // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
        supported_locales::<<Self as Service>::LangMarker>(&requested_locales, options, context)
            .map(JsValue::from)
    }

    /// [`Intl.ListFormat.prototype.format ( list )`][spec].
    ///
    /// Returns a language-specific formatted string representing the elements of the list.
    ///
    /// More information:
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.format
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/format
    fn format(this: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
        // 1. Let lf be the this value.
        // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
        let lf = this.as_object().map(JsObject::borrow).ok_or_else(|| {
            JsNativeError::typ()
                .with_message("`format` can only be called on a `ListFormat` object")
        })?;
        let lf = lf.as_list_format().ok_or_else(|| {
            JsNativeError::typ()
                .with_message("`format` can only be called on a `ListFormat` object")
        })?;

        // 3. Let stringList be ? StringListFromIterable(list).
        // TODO: support for UTF-16 unpaired surrogates formatting
        let strings = string_list_from_iterable(args.get_or_undefined(0), context)?;

        // 4. Return ! FormatList(lf, stringList).
        Ok(lf
            .formatter
            .format_to_string(strings.into_iter().map(|s| s.to_std_string_escaped()))
            .into())
    }

    /// [`Intl.ListFormat.prototype.formatToParts ( list )`][spec].
    ///
    /// Returns a language-specific formatted string representing the elements of the list.
    ///
    /// More information:
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.formatToParts
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/formatToParts
    fn format_to_parts(
        this: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // TODO: maybe try to move this into icu4x?
        use writeable::{PartsWrite, Writeable};

        #[derive(Debug, Clone)]
        enum Part {
            Literal(String),
            Element(String),
        }

        impl Part {
            const fn typ(&self) -> &'static str {
                match self {
                    Self::Literal(_) => "literal",
                    Self::Element(_) => "element",
                }
            }

            #[allow(clippy::missing_const_for_fn)]
            fn value(self) -> String {
                match self {
                    Self::Literal(s) | Self::Element(s) => s,
                }
            }
        }

        #[derive(Debug, Clone)]
        struct WriteString(String);

        impl Write for WriteString {
            fn write_str(&mut self, s: &str) -> std::fmt::Result {
                self.0.write_str(s)
            }

            fn write_char(&mut self, c: char) -> std::fmt::Result {
                self.0.write_char(c)
            }
        }

        impl PartsWrite for WriteString {
            type SubPartsWrite = Self;

            fn with_part(
                &mut self,
                _part: writeable::Part,
                mut f: impl FnMut(&mut Self::SubPartsWrite) -> std::fmt::Result,
            ) -> std::fmt::Result {
                f(self)
            }
        }

        #[derive(Debug, Clone)]
        struct PartsCollector(Vec<Part>);

        impl Write for PartsCollector {
            fn write_str(&mut self, _: &str) -> std::fmt::Result {
                Ok(())
            }
        }

        impl PartsWrite for PartsCollector {
            type SubPartsWrite = WriteString;

            fn with_part(
                &mut self,
                part: writeable::Part,
                mut f: impl FnMut(&mut Self::SubPartsWrite) -> core::fmt::Result,
            ) -> core::fmt::Result {
                assert!(part.category == "list");
                let mut string = WriteString(String::new());
                f(&mut string)?;
                if !string.0.is_empty() {
                    match part.value {
                        "element" => self.0.push(Part::Element(string.0)),
                        "literal" => self.0.push(Part::Literal(string.0)),
                        _ => unreachable!(),
                    };
                }
                Ok(())
            }
        }

        // 1. Let lf be the this value.
        // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
        let lf = this.as_object().map(JsObject::borrow).ok_or_else(|| {
            JsNativeError::typ()
                .with_message("`formatToParts` can only be called on a `ListFormat` object")
        })?;
        let lf = lf.as_list_format().ok_or_else(|| {
            JsNativeError::typ()
                .with_message("`formatToParts` can only be called on a `ListFormat` object")
        })?;

        // 3. Let stringList be ? StringListFromIterable(list).
        // TODO: support for UTF-16 unpaired surrogates formatting
        let strings = string_list_from_iterable(args.get_or_undefined(0), context)?
            .into_iter()
            .map(|s| s.to_std_string_escaped());

        // 4. Return ! FormatListToParts(lf, stringList).

        // Abstract operation `FormatListToParts ( listFormat, list )`
        // https://tc39.es/ecma402/#sec-formatlisttoparts

        // 1. Let parts be ! CreatePartsFromList(listFormat, list).
        let mut parts = PartsCollector(Vec::new());
        lf.formatter
            .format(strings)
            .write_to_parts(&mut parts)
            .map_err(|e| JsNativeError::typ().with_message(e.to_string()))?;

        // 2. Let result be ! ArrayCreate(0).
        let result = Array::array_create(0, None, context)
            .expect("creating an empty array with default proto must not fail");

        // 3. Let n be 0.
        // 4. For each Record { [[Type]], [[Value]] } part in parts, do
        for (n, part) in parts.0.into_iter().enumerate() {
            // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
            let o = context
                .intrinsics()
                .templates()
                .ordinary_object()
                .create(ObjectData::ordinary(), vec![]);

            // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
            o.create_data_property_or_throw(utf16!("type"), part.typ(), context)
                .expect("operation must not fail per the spec");

            // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
            o.create_data_property_or_throw(utf16!("value"), part.value(), context)
                .expect("operation must not fail per the spec");

            // d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
            result
                .create_data_property_or_throw(n, o, context)
                .expect("operation must not fail per the spec");

            // e. Increment n by 1.
        }

        // 5. Return result.
        Ok(result.into())
    }

    /// [`Intl.ListFormat.prototype.resolvedOptions ( )`][spec].
    ///
    /// Returns a new object with properties reflecting the locale and style formatting options
    /// computed during the construction of the current `Intl.ListFormat` object.
    ///
    /// More information:
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.resolvedoptions
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/resolvedOptions
    fn resolved_options(
        this: &JsValue,
        _: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let lf be the this value.
        // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
        let lf = this.as_object().map(JsObject::borrow).ok_or_else(|| {
            JsNativeError::typ()
                .with_message("`resolvedOptions` can only be called on a `ListFormat` object")
        })?;
        let lf = lf.as_list_format().ok_or_else(|| {
            JsNativeError::typ()
                .with_message("`resolvedOptions` can only be called on a `ListFormat` object")
        })?;

        // 3. Let options be OrdinaryObjectCreate(%Object.prototype%).
        let options = context
            .intrinsics()
            .templates()
            .ordinary_object()
            .create(ObjectData::ordinary(), vec![]);

        // 4. For each row of Table 11, except the header row, in table order, do
        //     a. Let p be the Property value of the current row.
        //     b. Let v be the value of lf's internal slot whose name is the Internal Slot value of the current row.
        //     c. Assert: v is not undefined.
        //     d. Perform ! CreateDataPropertyOrThrow(options, p, v).
        options
            .create_data_property_or_throw(utf16!("locale"), lf.locale.to_string(), context)
            .expect("operation must not fail per the spec");
        options
            .create_data_property_or_throw(
                utf16!("type"),
                match lf.typ {
                    ListFormatType::Conjunction => "conjunction",
                    ListFormatType::Disjunction => "disjunction",
                    ListFormatType::Unit => "unit",
                },
                context,
            )
            .expect("operation must not fail per the spec");
        options
            .create_data_property_or_throw(
                utf16!("style"),
                match lf.style {
                    ListLength::Wide => "long",
                    ListLength::Short => "short",
                    ListLength::Narrow => "narrow",
                    _ => unreachable!(),
                },
                context,
            )
            .expect("operation must not fail per the spec");

        // 5. Return options.
        Ok(options.into())
    }
}

/// Abstract operation [`StringListFromIterable ( iterable )`][spec]
///
/// [spec]: https://tc39.es/ecma402/#sec-createstringlistfromiterable
fn string_list_from_iterable(
    iterable: &JsValue,
    context: &mut Context<'_>,
) -> JsResult<Vec<JsString>> {
    // 1. If iterable is undefined, then
    if iterable.is_undefined() {
        //     a. Return a new empty List.
        return Ok(Vec::new());
    }

    // 2. Let iteratorRecord be ? GetIterator(iterable).
    let mut iterator = iterable.get_iterator(context, None, None)?;

    // 3. Let list be a new empty List.
    let mut list = Vec::new();

    // 4. Let next be true.
    // 5. Repeat, while next is not false,
    //     a. Set next to ? IteratorStep(iteratorRecord).
    //     b. If next is not false, then
    while !iterator.step(context)? {
        let item = iterator.value(context)?;
        //    i. Let nextValue be ? IteratorValue(next).
        //    ii. If Type(nextValue) is not String, then
        let Some(s) = item.as_string().cloned() else {
            //    1. Let error be ThrowCompletion(a newly created TypeError object).
            //    2. Return ? IteratorClose(iteratorRecord, error).
            return Err(iterator
                .close(
                    Err(JsNativeError::typ()
                    .with_message("StringListFromIterable: can only format strings into a list")
                    .into()),
                    context,
                )
                .expect_err("Should return the provided error"));
        };

        //    iii. Append nextValue to the end of the List list.
        list.push(s);
    }

    // 6. Return list.
    Ok(list)
}