node-js 0.1.14

JavaScript as a fusevm frontend: a lexer/parser and compiler to fusevm::Chunk on a JsHost object heap, with no bespoke VM or JIT
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
505
506
507
//! `Number.prototype.toLocaleString` / `BigInt.prototype.toLocaleString` with
//! their `options` argument, in the `en-US` shape.
//!
//! This is the ECMA-402 `Intl.NumberFormat` pipeline restricted to what needs no
//! locale data beyond en-US: the digit options (`minimumIntegerDigits`,
//! `minimum`/`maximumFractionDigits`, `minimum`/`maximumSignificantDigits`),
//! `useGrouping`, and the `decimal`/`percent`/`currency` styles. The `locales`
//! argument is still ignored (there is no ICU here, so every locale formats as
//! en-US — see BUGS.md). `signDisplay` and `currencyDisplay: "code"` are
//! honored; `notation`, the `name`/`narrowSymbol` currency displays and the
//! `unit` style's unit label are not modelled.
//!
//! Rounding is ICU's: the number's SHORTEST round-trip decimal form is rounded
//! half away from zero (`halfExpand`), so `(1.005).toLocaleString('en-US',
//! {maximumFractionDigits: 2})` is `1.01` even though the binary value is just
//! below 1.005 — which is where this differs from `toFixed`.

use crate::host::{self, with_host};
use fusevm::Value;

/// The currencies whose en-US rendering is not "the ISO code, a no-break space,
/// two fraction digits": `(code, symbol, fraction digits, NBSP after symbol)`.
///
/// GENERATED from node v26.10.0 (`Intl.supportedValuesOf('currency')`, each
/// formatted with `formatToParts`); any code absent here — including a
/// well-formed code ICU does not know, like `XYZ` — renders as `XYZ 1.00`.
const CURRENCIES: &[(&str, &str, usize, bool)] = &[
    ("AFN", "AFN", 0, true),
    ("ALL", "ALL", 0, true),
    ("AUD", "A$", 2, false),
    ("BHD", "BHD", 3, true),
    ("BIF", "BIF", 0, true),
    ("BRL", "R$", 2, false),
    ("CAD", "CA$", 2, false),
    ("CLP", "CLP", 0, true),
    ("CNY", "CN¥", 2, false),
    ("COP", "COP", 0, true),
    ("DJF", "DJF", 0, true),
    ("EUR", "€", 2, false),
    ("GBP", "£", 2, false),
    ("GNF", "GNF", 0, true),
    ("HKD", "HK$", 2, false),
    ("HUF", "HUF", 0, true),
    ("IDR", "IDR", 0, true),
    ("ILS", "₪", 2, false),
    ("INR", "₹", 2, false),
    ("IQD", "IQD", 0, true),
    ("IRR", "IRR", 0, true),
    ("ISK", "ISK", 0, true),
    ("JOD", "JOD", 3, true),
    ("JPY", "¥", 0, false),
    ("KMF", "KMF", 0, true),
    ("KPW", "KPW", 0, true),
    ("KRW", "₩", 0, false),
    ("KWD", "KWD", 3, true),
    ("LAK", "LAK", 0, true),
    ("LBP", "LBP", 0, true),
    ("LYD", "LYD", 3, true),
    ("MGA", "MGA", 0, true),
    ("MMK", "MMK", 0, true),
    ("MXN", "MX$", 2, false),
    ("NZD", "NZ$", 2, false),
    ("OMR", "OMR", 3, true),
    ("PHP", "₱", 2, false),
    ("PKR", "PKR", 0, true),
    ("PYG", "PYG", 0, true),
    ("RWF", "RWF", 0, true),
    ("SLL", "SLL", 0, true),
    ("SOS", "SOS", 0, true),
    ("SYP", "SYP", 0, true),
    ("TND", "TND", 3, true),
    ("TWD", "NT$", 2, false),
    ("UGX", "UGX", 0, true),
    ("USD", "$", 2, false),
    ("VND", "₫", 0, false),
    ("VUV", "VUV", 0, true),
    ("XAF", "FCFA", 0, true),
    ("XCD", "EC$", 2, false),
    ("XCG", "Cg.", 2, true),
    ("XOF", "F\u{202f}CFA", 0, true),
    ("XPF", "CFPF", 0, true),
    ("YER", "YER", 0, true),
];

/// The value being formatted. A BigInt is formatted from its exact digits.
pub enum Num<'a> {
    Float(f64),
    /// The magnitude's decimal digits and whether it is negative.
    BigInt(&'a str, bool),
}

enum Style {
    Decimal,
    Percent,
    /// The prefix as rendered (`$`, `CHF\u{a0}`).
    Currency(String),
}

#[derive(PartialEq)]
enum SignDisplay {
    Auto,
    Always,
    ExceptZero,
    Negative,
    Never,
}

enum Rounding {
    Fraction { min: usize, max: usize },
    Significant { min: usize, max: usize },
}

#[derive(PartialEq)]
enum Grouping {
    Always,
    Min2,
    Off,
}

struct Format {
    style: Style,
    sign: SignDisplay,
    min_int: usize,
    rounding: Rounding,
    grouping: Grouping,
}

/// A non-negative decimal `0.d1d2…dn × 10^point`: `digits` has no leading
/// zero (empty for zero), and `point` is how many of them precede the decimal
/// point (negative or past the end for very small / very large values).
struct Decimal {
    digits: Vec<u8>,
    point: i64,
}

impl Decimal {
    /// The shortest round-trip decimal of a finite, non-negative `x`.
    fn from_f64(x: f64) -> Decimal {
        if x == 0.0 {
            return Decimal {
                digits: Vec::new(),
                point: 0,
            };
        }
        // `{:e}` is Rust's shortest round-trip form: `1.005e0`, `1e21`.
        let s = format!("{x:e}");
        let (mant, exp) = s.split_once('e').expect("{:e} has an exponent");
        let exp: i64 = exp.parse().expect("integer exponent");
        let digits: Vec<u8> = mant
            .bytes()
            .filter(u8::is_ascii_digit)
            .map(|b| b - b'0')
            .collect();
        let mut d = Decimal {
            digits,
            point: exp + 1,
        };
        d.trim();
        d
    }

    fn from_digits(s: &str) -> Decimal {
        let digits: Vec<u8> = s.bytes().map(|b| b - b'0').collect();
        let mut d = Decimal {
            point: digits.len() as i64,
            digits,
        };
        d.trim();
        d
    }

    /// Drop leading and trailing zero digits, keeping the value.
    fn trim(&mut self) {
        let lead = self.digits.iter().take_while(|&&b| b == 0).count();
        self.digits.drain(..lead);
        self.point -= lead as i64;
        while self.digits.last() == Some(&0) {
            self.digits.pop();
        }
        if self.digits.is_empty() {
            self.point = 0;
        }
    }

    /// Keep the first `keep` digits, rounding half away from zero on the rest.
    fn round_to(&mut self, keep: i64) {
        let n = self.digits.len() as i64;
        if keep >= n {
            return;
        }
        if keep < 0 {
            self.digits.clear();
            self.point = 0;
            return;
        }
        let up = self.digits[keep as usize] >= 5;
        self.digits.truncate(keep as usize);
        if up {
            let mut i = self.digits.len();
            loop {
                if i == 0 {
                    self.digits.insert(0, 1);
                    self.point += 1;
                    break;
                }
                i -= 1;
                if self.digits[i] == 9 {
                    self.digits[i] = 0;
                } else {
                    self.digits[i] += 1;
                    break;
                }
            }
        }
        self.trim();
    }

    /// Multiply by 100 (the percent style), exactly.
    fn times_100(&mut self) {
        if !self.digits.is_empty() {
            self.point += 2;
        }
    }

    /// `(integer digits, fraction digits)` with no padding.
    fn split(&self) -> (String, String) {
        let digit = |i: i64| -> char {
            if i >= 0 && (i as usize) < self.digits.len() {
                (b'0' + self.digits[i as usize]) as char
            } else {
                '0'
            }
        };
        let int: String = if self.point > 0 {
            (0..self.point).map(digit).collect()
        } else {
            String::new()
        };
        let frac_len = self.digits.len() as i64 - self.point;
        let frac: String = (self.point..self.point + frac_len.max(0))
            .map(digit)
            .collect();
        (int, frac)
    }
}

/// V8's RangeError for an option value outside its allowed set.
fn out_of_range(value: &str, name: &str) -> String {
    host::range_error(&format!(
        "Value {value} out of range for Number.prototype.toLocaleString options property {name}"
    ))
}

/// `GetOption` for a string option restricted to `allowed`.
fn enum_option(opts: &Value, name: &str, allowed: &[&str]) -> Result<Option<String>, String> {
    match string_option(opts, name)? {
        Some(s) if !allowed.contains(&s.as_str()) => Err(out_of_range(&s, name)),
        other => Ok(other),
    }
}

/// `GetNumberOption`: `undefined` → `None`; otherwise `ToNumber`, a RangeError
/// outside `[lo, hi]` (or NaN), then `floor`.
fn digit_option(opts: &Value, name: &str, lo: f64, hi: f64) -> Result<Option<usize>, String> {
    let v = crate::builtins::get_property(opts, name)?;
    if matches!(v, Value::Undef) {
        return Ok(None);
    }
    let n = host::to_number_value(&v)?;
    if n.is_nan() || n < lo || n > hi {
        return Err(host::range_error(&format!("{name} value is out of range.")));
    }
    Ok(Some(n.floor() as usize))
}

/// `GetOption` for a string-valued option, `None` when absent.
fn string_option(opts: &Value, name: &str) -> Result<Option<String>, String> {
    let v = crate::builtins::get_property(opts, name)?;
    if matches!(v, Value::Undef) {
        return Ok(None);
    }
    Ok(Some(to_str(&v)?))
}

fn read_format(opts: &Value) -> Result<Format, String> {
    if with_host(|h| h.is_null(opts)) {
        return Err(host::type_error(
            "Number.prototype.toLocaleString called on null or undefined",
        ));
    }
    // A primitive `options` is `ToObject`ed in the spec, and a wrapper owns none
    // of these keys, so it formats exactly as `undefined` does.
    let is_object = matches!(opts, Value::Obj(_)) && !with_host(|h| host::is_primitive(h, opts));
    if !is_object {
        return Ok(Format {
            style: Style::Decimal,
            sign: SignDisplay::Auto,
            min_int: 1,
            rounding: Rounding::Fraction { min: 0, max: 3 },
            grouping: Grouping::Always,
        });
    }
    // Read in `InitializeNumberFormat` order, so the first invalid option is
    // the one reported: style, currency, currencyDisplay, the digit options,
    // useGrouping, signDisplay.
    let style_name = enum_option(opts, "style", &["decimal", "percent", "currency", "unit"])?
        .unwrap_or_else(|| "decimal".into());
    // A malformed code is refused whatever the style (`IsWellFormedCurrencyCode`).
    let currency = string_option(opts, "currency")?;
    if let Some(code) = &currency {
        if code.len() != 3 || !code.bytes().all(|b| b.is_ascii_alphabetic()) {
            return Err(host::range_error(&format!(
                "Invalid currency code : {code}"
            )));
        }
    }
    let display = enum_option(
        opts,
        "currencyDisplay",
        &["code", "symbol", "narrowSymbol", "name"],
    )?;
    let (style, currency_digits) = match style_name.as_str() {
        "percent" => (Style::Percent, None),
        "currency" => {
            let Some(code) = currency else {
                return Err(host::type_error(
                    "Currency code is required with currency style.",
                ));
            };
            let code = code.to_ascii_uppercase();
            let known = CURRENCIES.iter().find(|(c, ..)| *c == code);
            let digits = known.map(|(_, _, d, _)| *d).unwrap_or(2);
            // `code` always spells the ISO code; `symbol` (and, not modelled,
            // `narrowSymbol`/`name`) uses the en-US symbol table.
            let prefix = match (display.as_deref(), known) {
                (Some("code"), _) | (_, None) => format!("{code}\u{a0}"),
                (_, Some((_, sym, _, spaced))) => {
                    let gap = if *spaced { "\u{a0}" } else { "" };
                    format!("{sym}{gap}")
                }
            };
            (Style::Currency(prefix), Some(digits))
        }
        // `unit` needs per-unit display data; its number formats as decimal.
        _ => (Style::Decimal, None),
    };
    let min_int = digit_option(opts, "minimumIntegerDigits", 1.0, 21.0)?.unwrap_or(1);
    let min_frac = digit_option(opts, "minimumFractionDigits", 0.0, 100.0)?;
    let max_frac = digit_option(opts, "maximumFractionDigits", 0.0, 100.0)?;
    // `SetNumberFormatDigitOptions`: significant digits win when either is set.
    let min_sig = digit_option(opts, "minimumSignificantDigits", 1.0, 21.0)?;
    let max_sig = digit_option(opts, "maximumSignificantDigits", 1.0, 21.0)?;
    let rounding = if min_sig.is_some() || max_sig.is_some() {
        let min = min_sig.unwrap_or(1);
        let max = max_sig.unwrap_or(21);
        if min > max {
            return Err(host::range_error(
                "maximumSignificantDigits value is out of range.",
            ));
        }
        Rounding::Significant { min, max }
    } else {
        let (min_default, max_default) = match (&style, currency_digits) {
            (_, Some(d)) => (d, d),
            (Style::Percent, _) => (0, 0),
            _ => (0, 3),
        };
        let (min, max) = match (min_frac, max_frac) {
            (None, None) => (min_default, max_default),
            (Some(mn), None) => (mn, mn.max(max_default)),
            (None, Some(mx)) => (min_default.min(mx), mx),
            (Some(mn), Some(mx)) => {
                if mn > mx {
                    return Err(host::range_error(
                        "maximumFractionDigits value is out of range.",
                    ));
                }
                (mn, mx)
            }
        };
        Rounding::Fraction { min, max }
    };
    // `GetBooleanOrStringNumberFormatOption`: `true` is "always", any other
    // falsy value turns grouping off, and the strings "true"/"false" select
    // the default ("auto", which groups in en-US).
    let grouping_v = crate::builtins::get_property(opts, "useGrouping")?;
    let grouping = match grouping_v {
        Value::Undef | Value::Bool(true) => Grouping::Always,
        ref v if !with_host(|h| h.truthy(v)) => Grouping::Off,
        ref v => match to_str(v)?.as_str() {
            "min2" => Grouping::Min2,
            "always" | "auto" | "true" | "false" => Grouping::Always,
            other => return Err(out_of_range(other, "useGrouping")),
        },
    };
    let sign = match enum_option(
        opts,
        "signDisplay",
        &["auto", "never", "always", "exceptZero", "negative"],
    )?
    .as_deref()
    {
        Some("never") => SignDisplay::Never,
        Some("always") => SignDisplay::Always,
        Some("exceptZero") => SignDisplay::ExceptZero,
        Some("negative") => SignDisplay::Negative,
        _ => SignDisplay::Auto,
    };
    Ok(Format {
        style,
        sign,
        min_int,
        rounding,
        grouping,
    })
}

fn group(int: &str, grouping: &Grouping) -> String {
    if *grouping == Grouping::Off || (*grouping == Grouping::Min2 && int.len() < 5) {
        return int.to_string();
    }
    let mut out = String::with_capacity(int.len() + int.len() / 3);
    for (i, c) in int.chars().enumerate() {
        if i > 0 && (int.len() - i) % 3 == 0 {
            out.push(',');
        }
        out.push(c);
    }
    out
}

/// Format `x` the way `x.toLocaleString(locales, opts)` does under en-US.
pub fn to_locale_string(x: Num, opts: &Value) -> Result<String, String> {
    let fmt = read_format(opts)?;
    let (neg, mut dec, special) = match x {
        Num::Float(f) if f.is_nan() => (false, Decimal::from_digits(""), Some("NaN")),
        Num::Float(f) if f.is_infinite() => (f < 0.0, Decimal::from_digits(""), Some("∞")),
        Num::Float(f) => (f.is_sign_negative(), Decimal::from_f64(f.abs()), None),
        Num::BigInt(digits, neg) => (neg, Decimal::from_digits(digits), None),
    };
    let body = if let Some(text) = special {
        text.to_string()
    } else {
        if matches!(fmt.style, Style::Percent) {
            dec.times_100();
        }
        let (min_frac_shown, min_sig) = match fmt.rounding {
            Rounding::Fraction { min, max } => {
                dec.round_to(dec.point + max as i64);
                (min, None)
            }
            Rounding::Significant { min, max } => {
                dec.round_to(max as i64);
                (0, Some(min))
            }
        };
        let (mut int, mut frac) = dec.split();
        if int.len() < fmt.min_int {
            int = format!("{}{int}", "0".repeat(fmt.min_int - int.len()));
        }
        let mut min_frac = min_frac_shown;
        if let Some(min_sig) = min_sig {
            // Pad to `minimumSignificantDigits`, counted from the first
            // non-zero digit; a zero counts its units digit as the one it has.
            let sig_int = int.trim_start_matches('0').len();
            let lead_frac_zeros = if sig_int == 0 {
                frac.bytes().take_while(|&b| b == b'0').count()
            } else {
                0
            };
            let have = (sig_int + frac.len() - lead_frac_zeros).max(1);
            min_frac = frac.len() + min_sig.saturating_sub(have);
        }
        while frac.len() < min_frac {
            frac.push('0');
        }
        let grouped = group(&int, &fmt.grouping);
        if frac.is_empty() {
            grouped
        } else {
            format!("{grouped}.{frac}")
        }
    };
    // The sign is decided on the ROUNDED value: `-0.0001` rounds to zero, and
    // only `auto`/`always` still show its minus.
    let zero = special.is_none() && dec.digits.is_empty();
    let nan = special == Some("NaN");
    let sign = match fmt.sign {
        SignDisplay::Never => "",
        SignDisplay::ExceptZero if zero || nan => "",
        SignDisplay::Negative if zero => "",
        _ if neg => "-",
        SignDisplay::Always | SignDisplay::ExceptZero => "+",
        _ => "",
    };
    Ok(match fmt.style {
        Style::Decimal => format!("{sign}{body}"),
        Style::Percent => format!("{sign}{body}%"),
        Style::Currency(prefix) => format!("{sign}{prefix}{body}"),
    })
}

/// `ToString` of an option value, running a user `toString`.
fn to_str(v: &Value) -> Result<String, String> {
    let s = host::to_string_value(v)?;
    Ok(with_host(|h| h.str_of(&s)))
}