nsh-js 0.8.0

QuickJS host: runtime setup, host APIs, op decoding and bytecode.
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
//! `TextEncoder` and `TextDecoder` (Encoding Standard). Decoding knows
//! UTF-8, UTF-16LE / BE and windows-1252 (the labels of latin1 and ASCII
//! too); streaming decodes keep a sequence cut between chunks.

use super::{
    Class, Key, Object, Spec, data, instance,
    js::{self, Ctx, R, Raw, V, native, trace},
};

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Encoding {
    Utf8,
    Utf16Le,
    Utf16Be,
    Windows1252,
}

impl Encoding {
    /// The encoding a label names (WHATWG labels, ASCII case-insensitive).
    pub(crate) fn from_label(label: &str) -> Option<Encoding> {
        let label = label
            .trim_matches(|c: char| c.is_ascii_whitespace())
            .to_ascii_lowercase();
        Some(match label.as_str() {
            "unicode-1-1-utf-8" | "unicode11utf8" | "unicode20utf8" | "utf-8" | "utf8"
            | "x-unicode20utf8" => Encoding::Utf8,
            "csunicode" | "iso-10646-ucs-2" | "ucs-2" | "unicode" | "unicodefeff" | "utf-16"
            | "utf-16le" => Encoding::Utf16Le,
            "unicodefffe" | "utf-16be" => Encoding::Utf16Be,
            "ansi_x3.4-1968" | "ascii" | "cp1252" | "cp819" | "csisolatin1" | "ibm819"
            | "iso-8859-1" | "iso-ir-100" | "iso8859-1" | "iso88591" | "iso_8859-1"
            | "iso_8859-1:1987" | "l1" | "latin1" | "us-ascii" | "windows-1252" | "x-cp1252" => {
                Encoding::Windows1252
            }
            _ => return None,
        })
    }

    pub(crate) fn name(self) -> &'static str {
        match self {
            Encoding::Utf8 => "utf-8",
            Encoding::Utf16Le => "utf-16le",
            Encoding::Utf16Be => "utf-16be",
            Encoding::Windows1252 => "windows-1252",
        }
    }
}

/// windows-1252's 0x80..0x9F (the rest is Latin-1).
const W1252: [u16; 32] = [
    0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039,
    0x0152, 0x008D, 0x017D, 0x008F, 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
    0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178,
];

/// A decoder's state between `decode(…, { stream: true })` calls.
pub(crate) struct Decoder {
    pub(crate) encoding: Encoding,
    pub(crate) fatal: bool,
    pub(crate) ignore_bom: bool,
    /// Bytes of a sequence cut at the end of the last chunk.
    pending: Vec<u8>,
    /// The start of the stream was seen (its BOM handled).
    started: bool,
}

/// Decoding failed (`fatal`).
pub(crate) struct Malformed;

impl Decoder {
    pub(crate) fn new(encoding: Encoding, fatal: bool, ignore_bom: bool) -> Decoder {
        Decoder {
            encoding,
            fatal,
            ignore_bom,
            pending: Vec::new(),
            started: false,
        }
    }

    /// Decodes `input`; `stream`: more follows (a cut sequence waits).
    pub(crate) fn decode(&mut self, input: &[u8], stream: bool) -> Result<String, Malformed> {
        let mut bytes = std::mem::take(&mut self.pending);
        bytes.extend_from_slice(input);
        let mut start = 0;
        if !self.started && !bytes.is_empty() {
            let bom: &[u8] = match self.encoding {
                Encoding::Utf8 => b"\xEF\xBB\xBF",
                Encoding::Utf16Le => b"\xFF\xFE",
                Encoding::Utf16Be => b"\xFE\xFF",
                Encoding::Windows1252 => b"",
            };
            if !bom.is_empty() && bytes.len() < bom.len() && bom.starts_with(&bytes) && stream {
                // Not yet known whether this is a BOM.
                self.pending = bytes;
                return Ok(String::new());
            }
            self.started = true;
            if !self.ignore_bom && !bom.is_empty() && bytes.starts_with(bom) {
                start = bom.len();
            }
        }
        let bytes = &bytes[start..];
        let out = match self.encoding {
            Encoding::Utf8 => self.utf8(bytes, stream),
            Encoding::Utf16Le | Encoding::Utf16Be => self.utf16(bytes, stream),
            Encoding::Windows1252 => Ok(bytes
                .iter()
                .map(|&b| match b {
                    0x80..=0x9F => {
                        char::from_u32(u32::from(W1252[(b - 0x80) as usize])).unwrap_or('\u{FFFD}')
                    }
                    _ => char::from(b),
                })
                .collect()),
        };
        if !stream {
            self.started = false;
            self.pending.clear();
        }
        out
    }

    fn utf8(&mut self, bytes: &[u8], stream: bool) -> Result<String, Malformed> {
        let mut end = bytes.len();
        if stream {
            // A sequence cut at the end waits for the next chunk.
            if let Err(e) = std::str::from_utf8(bytes)
                && e.error_len().is_none()
            {
                end = e.valid_up_to();
                self.pending = bytes[end..].to_vec();
            }
        }
        let bytes = &bytes[..end];
        match std::str::from_utf8(bytes) {
            Ok(s) => Ok(s.to_owned()),
            Err(_) if self.fatal => Err(Malformed),
            // Rust replaces maximal subparts, as the Encoding Standard does.
            Err(_) => Ok(String::from_utf8_lossy(bytes).into_owned()),
        }
    }

    fn utf16(&mut self, bytes: &[u8], stream: bool) -> Result<String, Malformed> {
        let le = self.encoding == Encoding::Utf16Le;
        let units = bytes.len() / 2;
        let unit = |i: usize| {
            let (a, b) = (bytes[i * 2], bytes[i * 2 + 1]);
            if le {
                u16::from_le_bytes([a, b])
            } else {
                u16::from_be_bytes([a, b])
            }
        };
        let mut codes: Vec<u16> = (0..units).map(unit).collect();
        let mut cut = bytes.len() % 2;
        // A high surrogate at the end may pair with the next chunk.
        if stream
            && let Some(&last) = codes.last()
            && (0xD800..0xDC00).contains(&last)
        {
            codes.pop();
            cut += 2;
        }
        if stream {
            self.pending = bytes[bytes.len() - cut..].to_vec();
        }
        let mut out = String::with_capacity(codes.len());
        for r in char::decode_utf16(codes.iter().copied()) {
            match r {
                Ok(c) => out.push(c),
                Err(_) if self.fatal => return Err(Malformed),
                Err(_) => out.push('\u{FFFD}'),
            }
        }
        if !stream && cut > 0 {
            if self.fatal {
                return Err(Malformed);
            }
            out.push('\u{FFFD}');
        }
        Ok(out)
    }
}

trace!(Decoder {});

impl Object for Decoder {}

/// `TextEncoder` has no state.
struct Encoder;

trace!(Encoder {});

impl Object for Encoder {}

native!(
    fn encoder_constructor(cx, this, args) {
        instance(cx, this, Class::TextEncoder, Encoder)
    }
);

native!(
    fn encoder_encoding(cx, this, args) {
        data::<Encoder>(cx, this, "TextEncoder")?;
        js::string(cx, "utf-8")
    }
);

native!(
    fn encoder_encode(cx, this, args) {
        data::<Encoder>(cx, this, "TextEncoder")?;
        let s = js::opt_string(cx, args.get(0))?.unwrap_or_default();
        js::uint8_array(cx, s.as_bytes())
    }
);

native!(
    fn encoder_encode_into(cx, this, args) {
        data::<Encoder>(cx, this, "TextEncoder")?;
        let s = js::to_string(cx, args.get(0))?;
        let dest = args.get(1);
        // Bindgen types C enums as `u32`, but as `i32` with MSVC, where the
        // cast does nothing.
        #[allow(clippy::unnecessary_cast)]
        // SAFETY: a typed array's kind, read inline.
        if unsafe { rquickjs::qjs::JS_GetTypedArrayType(dest) }
            != rquickjs::qjs::JSTypedArrayEnum_JS_TYPED_ARRAY_UINT8 as i32
        {
            return Err(js::type_error(cx, "encodeInto needs a Uint8Array"));
        }
        // SAFETY: written before any JS runs.
        let out = unsafe { js::bytes_mut(cx, dest) }.unwrap_or_default();
        let (mut read, mut written) = (0usize, 0usize);
        for ch in s.chars() {
            let n = ch.len_utf8();
            if written + n > out.len() {
                break;
            }
            ch.encode_utf8(&mut out[written..written + n]);
            written += n;
            read += ch.len_utf16();
        }
        let r = js::new_object(cx)?;
        js::define(cx, r.raw(), "read", V::num(read as f64), true)?;
        js::define(cx, r.raw(), "written", V::num(written as f64), true)?;
        Ok(r)
    }
);

/// The encoding, `fatal` and `ignoreBOM` of a decoder's arguments.
pub(crate) fn decoder_options(cx: Ctx, label: Raw, options: Raw) -> R<Decoder> {
    let label = js::opt_string(cx, label)?.unwrap_or_else(|| "utf-8".to_owned());
    let encoding = Encoding::from_label(&label).ok_or_else(|| {
        js::range_error(cx, &format!("The encoding \"{label}\" is not supported"))
    })?;
    let (fatal, ignore_bom) = if js::is_object(options) {
        let f = js::get(cx, options, "fatal")?;
        let i = js::get(cx, options, "ignoreBOM")?;
        (js::to_bool(cx, f.raw()), js::to_bool(cx, i.raw()))
    } else if js::is_nullish(options) {
        (false, false)
    } else {
        return Err(js::type_error(cx, "TextDecoder options must be an object"));
    };
    Ok(Decoder::new(encoding, fatal, ignore_bom))
}

native!(
    fn decoder_constructor(cx, this, args) {
        let d = decoder_options(cx, args.get(0), args.get(1))?;
        instance(cx, this, Class::TextDecoder, d)
    }
);

fn decoder<'a>(cx: Ctx, v: Raw) -> R<std::cell::RefMut<'a, Decoder>> {
    data::<Decoder>(cx, v, "TextDecoder")
}

native!(
    fn decoder_encoding(cx, this, args) {
        let name = decoder(cx, this)?.encoding.name();
        js::string(cx, name)
    }
);

native!(
    fn decoder_fatal(cx, this, args) {
        Ok(V::bool(decoder(cx, this)?.fatal))
    }
);

native!(
    fn decoder_ignore_bom(cx, this, args) {
        Ok(V::bool(decoder(cx, this)?.ignore_bom))
    }
);

native!(
    fn decoder_decode(cx, this, args) {
        let options = args.get(1);
        let stream = if js::is_object(options) {
            let s = js::get(cx, options, "stream")?;
            js::to_bool(cx, s.raw())
        } else {
            false
        };
        let input = args.get(0);
        // Read before borrowing the decoder (a getter may run JS).
        let bytes = if js::is_undefined(input) {
            Vec::new()
        } else {
            js::buffer_source(cx, input).ok_or_else(|| {
                js::type_error(cx, "The provided value is not an ArrayBuffer or ArrayBufferView")
            })?
        };
        let decoded = decoder(cx, this)?.decode(&bytes, stream);
        match decoded {
            Ok(s) => js::string(cx, &s),
            Err(Malformed) => Err(js::type_error(cx, "The encoded data was not valid")),
        }
    }
);

pub(crate) const SPECS: &[Spec] = &[
    Spec {
        class: Class::TextEncoder,
        name: c"TextEncoder",
        parent: None,
        constructor: Some((encoder_constructor, 0)),
        methods: &[
            (Key::Name(c"encode"), encoder_encode, 0),
            (Key::Name(c"encodeInto"), encoder_encode_into, 2),
        ],
        accessors: &[(c"encoding", encoder_encoding, None)],
        statics: &[],
        constants: &[],
        global: true,
        ts: crate::web::ClassTs {
            doc: "",
            head: "",
            members: r#"readonly encoding: "utf-8";
encode(input?: string): Uint8Array;
encodeInto(source: string, destination: Uint8Array): { read: number; written: number };"#,
        }
        .declared(),
    },
    Spec {
        class: Class::TextDecoder,
        name: c"TextDecoder",
        parent: None,
        constructor: Some((decoder_constructor, 0)),
        methods: &[(Key::Name(c"decode"), decoder_decode, 0)],
        accessors: &[
            (c"encoding", decoder_encoding, None),
            (c"fatal", decoder_fatal, None),
            (c"ignoreBOM", decoder_ignore_bom, None),
        ],
        statics: &[],
        constants: &[],
        global: true,
        ts: crate::web::ClassTs {
            doc: "UTF-8, UTF-16LE / BE and windows-1252 (with the labels of latin1 and ASCII).",
            head: "",
            members: r#"constructor(label?: string, options?: { fatal?: boolean; ignoreBOM?: boolean });
readonly encoding: string;
readonly fatal: boolean;
readonly ignoreBOM: boolean;
decode(input?: BufferSource, options?: { stream?: boolean }): string;"#,
        }
        .declared(),
    },
];

/// The TypeScript types of this file's classes (`globals.d.ts`).
pub(crate) const TYPES: &str = r#"type BufferSource = ArrayBuffer | ArrayBufferView;
"#;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn streaming_keeps_cut_sequences() {
        let mut d = Decoder::new(Encoding::Utf8, false, false);
        let bytes = "\u{FEFF}é👋".as_bytes();
        let mut out = String::new();
        for b in bytes {
            out.push_str(&d.decode(&[*b], true).ok().unwrap());
        }
        out.push_str(&d.decode(&[], false).ok().unwrap());
        assert_eq!(out, "é👋");
        // Cut at the end of a non-streaming call: a replacement.
        assert_eq!(d.decode(b"a\xF0\x9F", false).ok().unwrap(), "a\u{FFFD}");
        let mut le = Decoder::new(Encoding::Utf16Le, false, false);
        let units: Vec<u8> = "👋x".encode_utf16().flat_map(u16::to_le_bytes).collect();
        let (a, b) = units.split_at(3);
        let s = le.decode(a, true).ok().unwrap() + &le.decode(b, false).ok().unwrap();
        assert_eq!(s, "👋x");
        let mut w = Decoder::new(Encoding::from_label(" Latin1 ").unwrap(), false, false);
        assert_eq!(w.decode(b"\x80\xE9", false).ok().unwrap(), "ێ");
        assert!(
            Decoder::new(Encoding::Utf8, true, false)
                .decode(b"\xFF", false)
                .is_err()
        );
    }
}