summaryrefslogtreecommitdiffstats
path: root/js/src/vm/JSONParser.cpp
blob: 4d387b84ac8bcc7503767a0977d518a23d8070d0 (plain)
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "vm/JSONParser.h"

#include "mozilla/Range.h"
#include "mozilla/RangedPtr.h"
#include "mozilla/Sprintf.h"
#include "mozilla/TextUtils.h"

#include "jsnum.h"

#include "builtin/Array.h"
#include "js/friend/ErrorMessages.h"  // js::GetErrorMessage, JSMSG_*
#include "util/StringBuffer.h"
#include "vm/Realm.h"

#include "vm/NativeObject-inl.h"

using namespace js;

using mozilla::AsciiAlphanumericToNumber;
using mozilla::IsAsciiDigit;
using mozilla::IsAsciiHexDigit;
using mozilla::RangedPtr;

JSONParserBase::~JSONParserBase() {
  for (size_t i = 0; i < stack.length(); i++) {
    if (stack[i].state == FinishArrayElement) {
      js_delete(&stack[i].elements());
    } else {
      js_delete(&stack[i].properties());
    }
  }

  for (size_t i = 0; i < freeElements.length(); i++) {
    js_delete(freeElements[i]);
  }

  for (size_t i = 0; i < freeProperties.length(); i++) {
    js_delete(freeProperties[i]);
  }
}

void JSONParserBase::trace(JSTracer* trc) {
  for (auto& elem : stack) {
    if (elem.state == FinishArrayElement) {
      elem.elements().trace(trc);
    } else {
      elem.properties().trace(trc);
    }
  }
}

template <typename CharT>
void JSONParser<CharT>::getTextPosition(uint32_t* column, uint32_t* line) {
  CharPtr ptr = begin;
  uint32_t col = 1;
  uint32_t row = 1;
  for (; ptr < current; ptr++) {
    if (*ptr == '\n' || *ptr == '\r') {
      ++row;
      col = 1;
      // \r\n is treated as a single newline.
      if (ptr + 1 < current && *ptr == '\r' && *(ptr + 1) == '\n') {
        ++ptr;
      }
    } else {
      ++col;
    }
  }
  *column = col;
  *line = row;
}

template <typename CharT>
void JSONParser<CharT>::error(const char* msg) {
  if (parseType == ParseType::JSONParse) {
    uint32_t column = 1, line = 1;
    getTextPosition(&column, &line);

    const size_t MaxWidth = sizeof("4294967295");
    char columnNumber[MaxWidth];
    SprintfLiteral(columnNumber, "%" PRIu32, column);
    char lineNumber[MaxWidth];
    SprintfLiteral(lineNumber, "%" PRIu32, line);

    JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                              JSMSG_JSON_BAD_PARSE, msg, lineNumber,
                              columnNumber);
  }
}

bool JSONParserBase::errorReturn() {
  return parseType == ParseType::AttemptForEval;
}

template <typename CharT>
template <JSONParserBase::StringType ST>
JSONParserBase::Token JSONParser<CharT>::readString() {
  MOZ_ASSERT(current < end);
  MOZ_ASSERT(*current == '"');

  /*
   * JSONString:
   *   /^"([^\u0000-\u001F"\\]|\\(["/\\bfnrt]|u[0-9a-fA-F]{4}))*"$/
   */

  if (++current == end) {
    error("unterminated string literal");
    return token(Error);
  }

  /*
   * Optimization: if the source contains no escaped characters, create the
   * string directly from the source text.
   */
  CharPtr start = current;
  for (; current < end; current++) {
    if (*current == '"') {
      size_t length = current - start;
      current++;
      JSLinearString* str =
          (ST == JSONParser::PropertyName)
              ? AtomizeChars(cx, start.get(), length)
              : NewStringCopyN<CanGC>(cx, start.get(), length);
      if (!str) {
        return token(OOM);
      }
      return stringToken(str);
    }

    if (*current == '\\') {
      break;
    }

    if (*current <= 0x001F) {
      error("bad control character in string literal");
      return token(Error);
    }
  }

  /*
   * Slow case: string contains escaped characters.  Copy a maximal sequence
   * of unescaped characters into a temporary buffer, then an escaped
   * character, and repeat until the entire string is consumed.
   */
  JSStringBuilder buffer(cx);
  do {
    if (start < current && !buffer.append(start.get(), current.get())) {
      return token(OOM);
    }

    if (current >= end) {
      break;
    }

    char16_t c = *current++;
    if (c == '"') {
      JSLinearString* str = (ST == JSONParser::PropertyName)
                                ? buffer.finishAtom()
                                : buffer.finishString();
      if (!str) {
        return token(OOM);
      }
      return stringToken(str);
    }

    if (c != '\\') {
      --current;
      error("bad character in string literal");
      return token(Error);
    }

    if (current >= end) {
      break;
    }

    switch (*current++) {
      case '"':
        c = '"';
        break;
      case '/':
        c = '/';
        break;
      case '\\':
        c = '\\';
        break;
      case 'b':
        c = '\b';
        break;
      case 'f':
        c = '\f';
        break;
      case 'n':
        c = '\n';
        break;
      case 'r':
        c = '\r';
        break;
      case 't':
        c = '\t';
        break;

      case 'u':
        if (end - current < 4 ||
            !(IsAsciiHexDigit(current[0]) && IsAsciiHexDigit(current[1]) &&
              IsAsciiHexDigit(current[2]) && IsAsciiHexDigit(current[3]))) {
          // Point to the first non-hexadecimal character (which may be
          // missing).
          if (current == end || !IsAsciiHexDigit(current[0])) {
            ;  // already at correct location
          } else if (current + 1 == end || !IsAsciiHexDigit(current[1])) {
            current += 1;
          } else if (current + 2 == end || !IsAsciiHexDigit(current[2])) {
            current += 2;
          } else if (current + 3 == end || !IsAsciiHexDigit(current[3])) {
            current += 3;
          } else {
            MOZ_CRASH("logic error determining first erroneous character");
          }

          error("bad Unicode escape");
          return token(Error);
        }
        c = (AsciiAlphanumericToNumber(current[0]) << 12) |
            (AsciiAlphanumericToNumber(current[1]) << 8) |
            (AsciiAlphanumericToNumber(current[2]) << 4) |
            (AsciiAlphanumericToNumber(current[3]));
        current += 4;
        break;

      default:
        current--;
        error("bad escaped character");
        return token(Error);
    }
    if (!buffer.append(c)) {
      return token(OOM);
    }

    start = current;
    for (; current < end; current++) {
      if (*current == '"' || *current == '\\' || *current <= 0x001F) {
        break;
      }
    }
  } while (current < end);

  error("unterminated string");
  return token(Error);
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::readNumber() {
  MOZ_ASSERT(current < end);
  MOZ_ASSERT(IsAsciiDigit(*current) || *current == '-');

  /*
   * JSONNumber:
   *   /^-?(0|[1-9][0-9]+)(\.[0-9]+)?([eE][\+\-]?[0-9]+)?$/
   */

  bool negative = *current == '-';

  /* -? */
  if (negative && ++current == end) {
    error("no number after minus sign");
    return token(Error);
  }

  const CharPtr digitStart = current;

  /* 0|[1-9][0-9]+ */
  if (!IsAsciiDigit(*current)) {
    error("unexpected non-digit");
    return token(Error);
  }
  if (*current++ != '0') {
    for (; current < end; current++) {
      if (!IsAsciiDigit(*current)) {
        break;
      }
    }
  }

  /* Fast path: no fractional or exponent part. */
  if (current == end ||
      (*current != '.' && *current != 'e' && *current != 'E')) {
    mozilla::Range<const CharT> chars(digitStart.get(), current - digitStart);
    if (chars.length() < strlen("9007199254740992")) {
      // If the decimal number is shorter than the length of 2**53, (the
      // largest number a double can represent with integral precision),
      // parse it using a decimal-only parser.  This comparison is
      // conservative but faster than a fully-precise check.
      double d = ParseDecimalNumber(chars);
      return numberToken(negative ? -d : d);
    }

    double d;
    if (!GetFullInteger(cx, digitStart.get(), current.get(), 10,
                        IntegerSeparatorHandling::None, &d)) {
      return token(OOM);
    }
    return numberToken(negative ? -d : d);
  }

  /* (\.[0-9]+)? */
  if (current < end && *current == '.') {
    if (++current == end) {
      error("missing digits after decimal point");
      return token(Error);
    }
    if (!IsAsciiDigit(*current)) {
      error("unterminated fractional number");
      return token(Error);
    }
    while (++current < end) {
      if (!IsAsciiDigit(*current)) {
        break;
      }
    }
  }

  /* ([eE][\+\-]?[0-9]+)? */
  if (current < end && (*current == 'e' || *current == 'E')) {
    if (++current == end) {
      error("missing digits after exponent indicator");
      return token(Error);
    }
    if (*current == '+' || *current == '-') {
      if (++current == end) {
        error("missing digits after exponent sign");
        return token(Error);
      }
    }
    if (!IsAsciiDigit(*current)) {
      error("exponent part is missing a number");
      return token(Error);
    }
    while (++current < end) {
      if (!IsAsciiDigit(*current)) {
        break;
      }
    }
  }

  double d;
  if (!FullStringToDouble(cx, digitStart.get(), current.get(), &d)) {
    return token(OOM);
  }
  return numberToken(negative ? -d : d);
}

static inline bool IsJSONWhitespace(char16_t c) {
  return c == '\t' || c == '\r' || c == '\n' || c == ' ';
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::advance() {
  while (current < end && IsJSONWhitespace(*current)) {
    current++;
  }
  if (current >= end) {
    error("unexpected end of data");
    return token(Error);
  }

  switch (*current) {
    case '"':
      return readString<LiteralValue>();

    case '-':
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      return readNumber();

    case 't':
      if (end - current < 4 || current[1] != 'r' || current[2] != 'u' ||
          current[3] != 'e') {
        error("unexpected keyword");
        return token(Error);
      }
      current += 4;
      return token(True);

    case 'f':
      if (end - current < 5 || current[1] != 'a' || current[2] != 'l' ||
          current[3] != 's' || current[4] != 'e') {
        error("unexpected keyword");
        return token(Error);
      }
      current += 5;
      return token(False);

    case 'n':
      if (end - current < 4 || current[1] != 'u' || current[2] != 'l' ||
          current[3] != 'l') {
        error("unexpected keyword");
        return token(Error);
      }
      current += 4;
      return token(Null);

    case '[':
      current++;
      return token(ArrayOpen);
    case ']':
      current++;
      return token(ArrayClose);

    case '{':
      current++;
      return token(ObjectOpen);
    case '}':
      current++;
      return token(ObjectClose);

    case ',':
      current++;
      return token(Comma);

    case ':':
      current++;
      return token(Colon);

    default:
      error("unexpected character");
      return token(Error);
  }
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::advanceAfterObjectOpen() {
  MOZ_ASSERT(current[-1] == '{');

  while (current < end && IsJSONWhitespace(*current)) {
    current++;
  }
  if (current >= end) {
    error("end of data while reading object contents");
    return token(Error);
  }

  if (*current == '"') {
    return readString<PropertyName>();
  }

  if (*current == '}') {
    current++;
    return token(ObjectClose);
  }

  error("expected property name or '}'");
  return token(Error);
}

template <typename CharT>
static inline void AssertPastValue(const RangedPtr<const CharT> current) {
  /*
   * We're past an arbitrary JSON value, so the previous character is
   * *somewhat* constrained, even if this assertion is pretty broad.  Don't
   * knock it till you tried it: this assertion *did* catch a bug once.
   */
  MOZ_ASSERT((current[-1] == 'l' && current[-2] == 'l' && current[-3] == 'u' &&
              current[-4] == 'n') ||
             (current[-1] == 'e' && current[-2] == 'u' && current[-3] == 'r' &&
              current[-4] == 't') ||
             (current[-1] == 'e' && current[-2] == 's' && current[-3] == 'l' &&
              current[-4] == 'a' && current[-5] == 'f') ||
             current[-1] == '}' || current[-1] == ']' || current[-1] == '"' ||
             IsAsciiDigit(current[-1]));
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::advanceAfterArrayElement() {
  AssertPastValue(current);

  while (current < end && IsJSONWhitespace(*current)) {
    current++;
  }
  if (current >= end) {
    error("end of data when ',' or ']' was expected");
    return token(Error);
  }

  if (*current == ',') {
    current++;
    return token(Comma);
  }

  if (*current == ']') {
    current++;
    return token(ArrayClose);
  }

  error("expected ',' or ']' after array element");
  return token(Error);
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::advancePropertyName() {
  MOZ_ASSERT(current[-1] == ',');

  while (current < end && IsJSONWhitespace(*current)) {
    current++;
  }
  if (current >= end) {
    error("end of data when property name was expected");
    return token(Error);
  }

  if (*current == '"') {
    return readString<PropertyName>();
  }

  error("expected double-quoted property name");
  return token(Error);
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::advancePropertyColon() {
  MOZ_ASSERT(current[-1] == '"');

  while (current < end && IsJSONWhitespace(*current)) {
    current++;
  }
  if (current >= end) {
    error("end of data after property name when ':' was expected");
    return token(Error);
  }

  if (*current == ':') {
    current++;
    return token(Colon);
  }

  error("expected ':' after property name in object");
  return token(Error);
}

template <typename CharT>
JSONParserBase::Token JSONParser<CharT>::advanceAfterProperty() {
  AssertPastValue(current);

  while (current < end && IsJSONWhitespace(*current)) {
    current++;
  }
  if (current >= end) {
    error("end of data after property value in object");
    return token(Error);
  }

  if (*current == ',') {
    current++;
    return token(Comma);
  }

  if (*current == '}') {
    current++;
    return token(ObjectClose);
  }

  error("expected ',' or '}' after property value in object");
  return token(Error);
}

inline bool JSONParserBase::finishObject(MutableHandleValue vp,
                                         PropertyVector& properties) {
  MOZ_ASSERT(&properties == &stack.back().properties());

  JSObject* obj = NewPlainObjectWithProperties(
      cx, properties.begin(), properties.length(), GenericObject);
  if (!obj) {
    return false;
  }

  vp.setObject(*obj);
  if (!freeProperties.append(&properties)) {
    return false;
  }
  stack.popBack();
  return true;
}

inline bool JSONParserBase::finishArray(MutableHandleValue vp,
                                        ElementVector& elements) {
  MOZ_ASSERT(&elements == &stack.back().elements());

  ArrayObject* obj =
      NewDenseCopiedArray(cx, elements.length(), elements.begin());
  if (!obj) {
    return false;
  }

  vp.setObject(*obj);
  if (!freeElements.append(&elements)) {
    return false;
  }
  stack.popBack();
  return true;
}

template <typename CharT>
bool JSONParser<CharT>::parse(MutableHandleValue vp) {
  RootedValue value(cx);
  MOZ_ASSERT(stack.empty());

  vp.setUndefined();

  Token token;
  ParserState state = JSONValue;
  while (true) {
    switch (state) {
      case FinishObjectMember: {
        PropertyVector& properties = stack.back().properties();
        properties.back().value = value;

        token = advanceAfterProperty();
        if (token == ObjectClose) {
          if (!finishObject(&value, properties)) {
            return false;
          }
          break;
        }
        if (token != Comma) {
          if (token == OOM) {
            return false;
          }
          if (token != Error) {
            error(
                "expected ',' or '}' after property-value pair in object "
                "literal");
          }
          return errorReturn();
        }
        token = advancePropertyName();
        /* FALL THROUGH */
      }

      JSONMember:
        if (token == String) {
          jsid id = AtomToId(atomValue());
          if (parseType == ParseType::AttemptForEval) {
            // In |JSON.parse|, "__proto__" is a property like any other and may
            // appear multiple times. In object literal syntax, "__proto__" is
            // prototype mutation and can appear at most once. |JSONParser| only
            // supports the former semantics, so if this parse attempt is for
            // |eval|, return true (without reporting an error) to indicate the
            // JSON parse attempt was unsuccessful.
            if (id == NameToId(cx->names().proto)) {
              return true;
            }
          }
          PropertyVector& properties = stack.back().properties();
          if (!properties.emplaceBack(id)) {
            return false;
          }
          token = advancePropertyColon();
          if (token != Colon) {
            MOZ_ASSERT(token == Error);
            return errorReturn();
          }
          goto JSONValue;
        }
        if (token == OOM) {
          return false;
        }
        if (token != Error) {
          error("property names must be double-quoted strings");
        }
        return errorReturn();

      case FinishArrayElement: {
        ElementVector& elements = stack.back().elements();
        if (!elements.append(value.get())) {
          return false;
        }
        token = advanceAfterArrayElement();
        if (token == Comma) {
          goto JSONValue;
        }
        if (token == ArrayClose) {
          if (!finishArray(&value, elements)) {
            return false;
          }
          break;
        }
        MOZ_ASSERT(token == Error);
        return errorReturn();
      }

      JSONValue:
      case JSONValue:
        token = advance();
      JSONValueSwitch:
        switch (token) {
          case String:
            value = stringValue();
            break;
          case Number:
            value = numberValue();
            break;
          case True:
            value = BooleanValue(true);
            break;
          case False:
            value = BooleanValue(false);
            break;
          case Null:
            value = NullValue();
            break;

          case ArrayOpen: {
            ElementVector* elements;
            if (!freeElements.empty()) {
              elements = freeElements.popCopy();
              elements->clear();
            } else {
              elements = cx->new_<ElementVector>(cx);
              if (!elements) {
                return false;
              }
            }
            if (!stack.append(elements)) {
              js_delete(elements);
              return false;
            }

            token = advance();
            if (token == ArrayClose) {
              if (!finishArray(&value, *elements)) {
                return false;
              }
              break;
            }
            goto JSONValueSwitch;
          }

          case ObjectOpen: {
            PropertyVector* properties;
            if (!freeProperties.empty()) {
              properties = freeProperties.popCopy();
              properties->clear();
            } else {
              properties = cx->new_<PropertyVector>(cx);
              if (!properties) {
                return false;
              }
            }
            if (!stack.append(properties)) {
              js_delete(properties);
              return false;
            }

            token = advanceAfterObjectOpen();
            if (token == ObjectClose) {
              if (!finishObject(&value, *properties)) {
                return false;
              }
              break;
            }
            goto JSONMember;
          }

          case ArrayClose:
          case ObjectClose:
          case Colon:
          case Comma:
            // Move the current pointer backwards so that the position
            // reported in the error message is correct.
            --current;
            error("unexpected character");
            return errorReturn();

          case OOM:
            return false;

          case Error:
            return errorReturn();
        }
        break;
    }

    if (stack.empty()) {
      break;
    }
    state = stack.back().state;
  }

  for (; current < end; current++) {
    if (!IsJSONWhitespace(*current)) {
      error("unexpected non-whitespace character after JSON data");
      return errorReturn();
    }
  }

  MOZ_ASSERT(end == current);
  MOZ_ASSERT(stack.empty());

  vp.set(value);
  return true;
}

template class js::JSONParser<Latin1Char>;
template class js::JSONParser<char16_t>;