pipa-js 0.1.7

A fast, minimal ES2023 JavaScript runtime built in Rust.
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
use crate::object::array_obj::JSArrayObject;
use crate::object::object::JSObject;
use crate::runtime::context::JSContext;
use crate::util::memchr::memchr2;
use crate::value::JSValue;

#[inline(always)]
fn find_string_end(input: &[u8], pos: usize) -> usize {
    memchr2(b'"', b'\\', &input[pos..])
        .map(|o| pos + o)
        .unwrap_or(input.len())
}

#[inline(always)]
fn skip_ws_bulk(input: &[u8], mut pos: usize) -> usize {
    let len = input.len();
    loop {
        if pos >= len {
            return len;
        }
        match input[pos] {
            b' ' | b'\t' | b'\r' | b'\n' => pos += 1,
            _ => return pos,
        }
    }
}

pub struct JsonParser<'a> {
    input: &'a [u8],
    pos: usize,
}

impl<'a> JsonParser<'a> {
    pub fn new(input: &'a str) -> Self {
        JsonParser {
            input: input.as_bytes(),
            pos: 0,
        }
    }

    #[inline]
    fn peek(&self) -> Option<u8> {
        self.input.get(self.pos).copied()
    }

    #[inline]
    fn advance(&mut self) -> Option<u8> {
        let ch = self.input.get(self.pos).copied();
        self.pos += 1;
        ch
    }

    #[inline]
    fn skip_whitespace(&mut self) {
        self.pos = skip_ws_bulk(self.input, self.pos);
    }

    #[inline]
    fn expect_byte(&mut self, expected: u8) -> Result<(), String> {
        match self.advance() {
            Some(b) if b == expected => Ok(()),
            Some(b) => Err(format!(
                "JSON: expected '{}', got '{}' at pos {}",
                expected as char,
                b as char,
                self.pos - 1
            )),
            None => Err("JSON: unexpected end of input".to_string()),
        }
    }

    pub fn parse_value(&mut self, ctx: &mut JSContext) -> Result<JSValue, String> {
        self.skip_whitespace();
        let ch = self.peek().ok_or("JSON: unexpected end of input")?;
        match ch {
            b'"' => self.parse_string(ctx),
            b'{' => self.parse_object(ctx),
            b'[' => self.parse_array(ctx),
            b't' => self.parse_literal(b"true", JSValue::bool(true)),
            b'f' => self.parse_literal(b"false", JSValue::bool(false)),
            b'n' => self.parse_literal(b"null", JSValue::null()),
            b'-' | b'0'..=b'9' => self.parse_number(),
            _ => Err(format!(
                "JSON: unexpected character '{}' at pos {}",
                ch as char, self.pos
            )),
        }
    }

    pub fn parse_root(&mut self, ctx: &mut JSContext) -> Result<JSValue, String> {
        let val = self.parse_value(ctx)?;
        self.skip_whitespace();
        if self.pos != self.input.len() {
            return Err(format!(
                "JSON: unexpected trailing content at pos {}",
                self.pos
            ));
        }
        Ok(val)
    }

    fn parse_literal(&mut self, expected: &[u8], value: JSValue) -> Result<JSValue, String> {
        let end = self.pos + expected.len();
        if end <= self.input.len() && &self.input[self.pos..end] == expected {
            self.pos = end;
            Ok(value)
        } else {
            Err(format!("JSON: invalid literal at pos {}", self.pos))
        }
    }

    fn parse_number(&mut self) -> Result<JSValue, String> {
        let start = self.pos;

        if self.peek() == Some(b'-') {
            self.pos += 1;
        }

        if self.peek() == Some(b'0') {
            self.pos += 1;

            if let Some(b'0'..=b'9') = self.peek() {
                return Err("JSON: leading zeros not allowed".to_string());
            }
        } else {
            while let Some(b'0'..=b'9') = self.peek() {
                self.pos += 1;
            }
        }

        let has_fraction;
        let has_exponent;

        if self.peek() == Some(b'.') {
            self.pos += 1;
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err("JSON: expected digit after decimal point".to_string());
            }
            while let Some(b'0'..=b'9') = self.peek() {
                self.pos += 1;
            }
            has_fraction = true;
        } else {
            has_fraction = false;
        }

        if matches!(self.peek(), Some(b'e') | Some(b'E')) {
            self.pos += 1;
            if matches!(self.peek(), Some(b'+') | Some(b'-')) {
                self.pos += 1;
            }
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err("JSON: expected digit in exponent".to_string());
            }
            while let Some(b'0'..=b'9') = self.peek() {
                self.pos += 1;
            }
            has_exponent = true;
        } else {
            has_exponent = false;
        }

        if !has_fraction && !has_exponent {
            let bytes = &self.input[start..self.pos];
            let (negative, digits) = if bytes[0] == b'-' {
                (true, &bytes[1..])
            } else {
                (false, bytes)
            };

            if digits.len() <= 18 {
                let mut n: u64 = 0;
                for &b in digits {
                    n = n * 10 + (b - b'0') as u64;
                }
                let i = if negative {
                    if n == 0 {
                        return Ok(JSValue::new_float(-0.0f64));
                    }
                    if n <= i64::MAX as u64 + 1 {
                        n.wrapping_neg() as i64
                    } else {
                        let s = unsafe { std::str::from_utf8_unchecked(bytes) };
                        let f: f64 = s.parse().map_err(|_| "JSON: invalid number".to_string())?;
                        return Ok(JSValue::new_float(f));
                    }
                } else if n <= i64::MAX as u64 {
                    n as i64
                } else {
                    let s = unsafe { std::str::from_utf8_unchecked(bytes) };
                    let f: f64 = s.parse().map_err(|_| "JSON: invalid number".to_string())?;
                    return Ok(JSValue::new_float(f));
                };
                return Ok(JSValue::new_int(i));
            }

            let num_str = unsafe { std::str::from_utf8_unchecked(&self.input[start..self.pos]) };
            if let Ok(i) = num_str.parse::<i64>() {
                return Ok(JSValue::new_int(i));
            }
            let f: f64 = num_str
                .parse()
                .map_err(|_| "JSON: invalid number".to_string())?;
            return Ok(JSValue::new_float(f));
        }

        let num_str = unsafe { std::str::from_utf8_unchecked(&self.input[start..self.pos]) };
        let f: f64 = num_str
            .parse()
            .map_err(|_| "JSON: invalid number".to_string())?;
        Ok(JSValue::new_float(f))
    }

    fn parse_string(&mut self, ctx: &mut JSContext) -> Result<JSValue, String> {
        self.expect_byte(b'"')?;
        let start = self.pos;

        let first_special = find_string_end(self.input, self.pos);

        if first_special < self.input.len() && self.input[first_special] == b'"' {
            let slice = &self.input[start..first_special];
            for &b in slice {
                if b < 0x20 {
                    return Err(format!(
                        "JSON: unescaped control character U+{:04X} at pos {}",
                        b, start
                    ));
                }
            }
            let s = unsafe { std::str::from_utf8_unchecked(slice) };
            let atom = ctx.intern(s);
            self.pos = first_special + 1;
            return Ok(JSValue::new_string(atom));
        }

        let mut buf = if first_special > start {
            let slice = &self.input[start..first_special];
            for &b in slice {
                if b < 0x20 {
                    return Err(format!(
                        "JSON: unescaped control character U+{:04X} at pos {}",
                        b, start
                    ));
                }
            }
            let s = unsafe { std::str::from_utf8_unchecked(slice) };
            s.to_string()
        } else {
            String::new()
        };
        self.pos = first_special;

        loop {
            let b = self.advance().ok_or("JSON: unterminated string")?;
            match b {
                b'"' => {
                    let atom = ctx.intern(&buf);
                    return Ok(JSValue::new_string(atom));
                }
                b'\\' => {
                    let escaped = self.advance().ok_or("JSON: unterminated escape")?;
                    match escaped {
                        b'"' => buf.push('"'),
                        b'\\' => buf.push('\\'),
                        b'/' => buf.push('/'),
                        b'b' => buf.push('\x08'),
                        b'f' => buf.push('\x0c'),
                        b'n' => buf.push('\n'),
                        b'r' => buf.push('\r'),
                        b't' => buf.push('\t'),
                        b'u' => {
                            let hex = self.parse_hex_escape()?;
                            buf.push(hex);
                        }
                        _ => return Err(format!("JSON: invalid escape '\\{}'", escaped as char)),
                    }

                    let next_special = find_string_end(self.input, self.pos);
                    let span = &self.input[self.pos..next_special];
                    for &b in span {
                        if b < 0x20 {
                            return Err(format!(
                                "JSON: unescaped control character U+{:04X} at pos {}",
                                b, self.pos
                            ));
                        }
                    }

                    buf.push_str(unsafe { std::str::from_utf8_unchecked(span) });
                    self.pos = next_special;
                }
                _ => unreachable!(
                    "find_string_end guarantees self.pos stops at '\"' or '\\', got 0x{:02X}",
                    b
                ),
            }
        }
    }

    fn parse_hex_escape(&mut self) -> Result<char, String> {
        let mut code = 0u32;
        for _ in 0..4 {
            let b = self.advance().ok_or("JSON: unterminated unicode escape")?;
            let digit = match b {
                b'0'..=b'9' => (b - b'0') as u32,
                b'a'..=b'f' => (b - b'a') as u32 + 10,
                b'A'..=b'F' => (b - b'A') as u32 + 10,
                _ => return Err(format!("JSON: invalid hex digit '{}'", b as char)),
            };
            code = (code << 4) | digit;
        }

        if (0xD800..=0xDBFF).contains(&code) {
            if self.advance() != Some(b'\\') || self.advance() != Some(b'u') {
                return Err("JSON: expected low surrogate after high surrogate".to_string());
            }
            let mut low = 0u32;
            for _ in 0..4 {
                let b = self
                    .advance()
                    .ok_or("JSON: unterminated surrogate escape")?;
                let digit = match b {
                    b'0'..=b'9' => (b - b'0') as u32,
                    b'a'..=b'f' => (b - b'a') as u32 + 10,
                    b'A'..=b'F' => (b - b'A') as u32 + 10,
                    _ => {
                        return Err(format!(
                            "JSON: invalid hex digit in surrogate '{}'",
                            b as char
                        ));
                    }
                };
                low = (low << 4) | digit;
            }
            if !(0xDC00..=0xDFFF).contains(&low) {
                return Err("JSON: invalid low surrogate".to_string());
            }
            let combined = 0x10000 + ((code - 0xD800) << 10) + (low - 0xDC00);
            char::from_u32(combined)
                .ok_or("JSON: invalid codepoint from surrogate pair".to_string())
        } else {
            char::from_u32(code).ok_or("JSON: invalid unicode codepoint".to_string())
        }
    }

    fn parse_array(&mut self, ctx: &mut JSContext) -> Result<JSValue, String> {
        self.expect_byte(b'[')?;
        self.skip_whitespace();

        let mut elements = Vec::new();

        if self.peek() != Some(b']') {
            loop {
                self.skip_whitespace();
                let val = self.parse_value(ctx)?;
                elements.push(val);
                self.skip_whitespace();
                match self.peek() {
                    Some(b',') => {
                        self.pos += 1;
                    }
                    Some(b']') => break,
                    _ => return Err("JSON: expected ',' or ']' in array".to_string()),
                }
            }
        }
        self.expect_byte(b']')?;

        let len = elements.len();
        let mut arr = JSArrayObject::from_elements(elements);
        if let Some(proto_ptr) = ctx.get_array_prototype() {
            arr.header.set_prototype_raw(proto_ptr);
        }
        let len_atom = ctx.common_atoms.length;
        arr.header.set(len_atom, JSValue::new_int(len as i64));

        let ptr = Box::into_raw(Box::new(arr)) as usize;
        ctx.runtime_mut().gc_heap_mut().track_array(ptr);
        Ok(JSValue::new_object(ptr))
    }

    fn parse_object(&mut self, ctx: &mut JSContext) -> Result<JSValue, String> {
        self.expect_byte(b'{')?;
        self.skip_whitespace();

        let mut obj = JSObject::new();
        if let Some(obj_proto_ptr) = ctx.get_object_prototype() {
            obj.prototype = Some(obj_proto_ptr);
        }

        if self.peek() != Some(b'}') {
            loop {
                self.skip_whitespace();

                if self.peek() != Some(b'"') {
                    return Err("JSON: expected string key in object".to_string());
                }
                let key_val = self.parse_string(ctx)?;
                let key_atom = key_val.get_atom();

                self.skip_whitespace();
                self.expect_byte(b':')?;

                self.skip_whitespace();
                let val = self.parse_value(ctx)?;
                obj.set(key_atom, val);

                self.skip_whitespace();
                match self.peek() {
                    Some(b',') => {
                        self.pos += 1;
                    }
                    Some(b'}') => break,
                    _ => return Err("JSON: expected ',' or '}' in object".to_string()),
                }
            }
        }
        self.expect_byte(b'}')?;

        let ptr = Box::into_raw(Box::new(obj)) as usize;
        Ok(JSValue::new_object(ptr))
    }
}