| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef vm_JSONParser_h |
| 8 |
#define vm_JSONParser_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 11 |
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS |
| 12 |
#include "mozilla/Maybe.h" // mozilla::{Maybe,Some} |
| 13 |
#include "mozilla/Range.h" // mozilla::Range |
| 14 |
#include "mozilla/RangedPtr.h" // mozilla::RangedPtr |
| 15 |
|
| 16 |
#include <stddef.h> // size_t |
| 17 |
#include <stdint.h> // uint32_t |
| 18 |
#include <utility> // std::move |
| 19 |
|
| 20 |
#include "builtin/ParseRecordObject.h" // js::ParseRecordObject |
| 21 |
#include "ds/IdValuePair.h" // IdValuePair |
| 22 |
#include "gc/GC.h" // AutoSelectGCHeap |
| 23 |
#include "js/GCVector.h" // JS::GCVector |
| 24 |
#include "js/RootingAPI.h" // JS::Handle, JS::MutableHandle, MutableWrappedPtrOperations |
| 25 |
#include "js/Value.h" // JS::Value, JS::BooleanValue, JS::NullValue |
| 26 |
#include "js/Vector.h" // Vector |
| 27 |
#include "util/StringBuilder.h" // JSStringBuilder |
| 28 |
#include "vm/StringType.h" // JSString, JSAtom |
| 29 |
|
| 30 |
struct JSContext; |
| 31 |
class JSTracer; |
| 32 |
|
| 33 |
namespace js { |
| 34 |
|
| 35 |
class FrontendContext; |
| 36 |
|
| 37 |
enum class JSONToken { |
| 38 |
String, |
| 39 |
Number, |
| 40 |
True, |
| 41 |
False, |
| 42 |
Null, |
| 43 |
ArrayOpen, |
| 44 |
ArrayClose, |
| 45 |
ObjectOpen, |
| 46 |
ObjectClose, |
| 47 |
Colon, |
| 48 |
Comma, |
| 49 |
OOM, |
| 50 |
Error |
| 51 |
}; |
| 52 |
|
| 53 |
enum class JSONStringType { PropertyName, LiteralValue }; |
| 54 |
|
| 55 |
template <typename CharT, typename ParserT> |
| 56 |
class MOZ_STACK_CLASS JSONTokenizer { |
| 57 |
public: |
| 58 |
using CharPtr = mozilla::RangedPtr<const CharT>; |
| 59 |
|
| 60 |
using JSONStringBuilder = typename ParserT::JSONStringBuilder; |
| 61 |
|
| 62 |
protected: |
| 63 |
CharPtr sourceStart; |
| 64 |
CharPtr current; |
| 65 |
const CharPtr begin, end; |
| 66 |
|
| 67 |
ParserT* parser = nullptr; |
| 68 |
|
| 69 |
JSONTokenizer(CharPtr sourceStart, CharPtr current, const CharPtr begin, |
| 70 |
const CharPtr end, ParserT* parser) |
| 71 |
: sourceStart(sourceStart), |
| 72 |
current(current), |
| 73 |
begin(begin), |
| 74 |
end(end), |
| 75 |
parser(parser) { |
| 76 |
MOZ_ASSERT(current <= end); |
| 77 |
MOZ_ASSERT(parser); |
| 78 |
} |
| 79 |
|
| 80 |
public: |
| 81 |
JSONTokenizer(CharPtr current, const CharPtr begin, const CharPtr end, |
| 82 |
ParserT* parser) |
| 83 |
: JSONTokenizer(current, current, begin, end, parser) {} |
| 84 |
|
| 85 |
explicit JSONTokenizer(mozilla::Range<const CharT> data, ParserT* parser) |
| 86 |
: JSONTokenizer(data.begin(), data.begin(), data.end(), parser) {} |
| 87 |
|
| 88 |
JSONTokenizer(JSONTokenizer<CharT, ParserT>&& other) noexcept |
| 89 |
: JSONTokenizer(other.sourceStart, other.current, other.begin, other.end, |
| 90 |
other.parser) {} |
| 91 |
|
| 92 |
JSONTokenizer(const JSONTokenizer<CharT, ParserT>& other) = delete; |
| 93 |
void operator=(const JSONTokenizer<CharT, ParserT>& other) = delete; |
| 94 |
|
| 95 |
void fixupParser(ParserT* newParser) { parser = newParser; } |
| 96 |
|
| 97 |
void getTextPosition(uint32_t* column, uint32_t* line); |
| 98 |
|
| 99 |
bool consumeTrailingWhitespaces(); |
| 100 |
|
| 101 |
JSONToken advance(); |
| 102 |
JSONToken advancePropertyName(); |
| 103 |
JSONToken advancePropertyColon(); |
| 104 |
JSONToken advanceAfterProperty(); |
| 105 |
JSONToken advanceAfterObjectOpen(); |
| 106 |
JSONToken advanceAfterArrayElement(); |
| 107 |
|
| 108 |
void unget() { --current; } |
| 109 |
|
| 110 |
#ifdef DEBUG |
| 111 |
bool finished() { return end == current; } |
| 112 |
#endif |
| 113 |
|
| 114 |
JSONToken token(JSONToken t) { |
| 115 |
MOZ_ASSERT(t != JSONToken::String); |
| 116 |
MOZ_ASSERT(t != JSONToken::Number); |
| 117 |
return t; |
| 118 |
} |
| 119 |
|
| 120 |
template <JSONStringType ST> |
| 121 |
JSONToken stringToken(const CharPtr start, size_t length); |
| 122 |
template <JSONStringType ST> |
| 123 |
JSONToken stringToken(JSONStringBuilder& builder); |
| 124 |
|
| 125 |
JSONToken numberToken(double d); |
| 126 |
|
| 127 |
template <JSONStringType ST> |
| 128 |
JSONToken readString(); |
| 129 |
|
| 130 |
JSONToken readNumber(); |
| 131 |
|
| 132 |
void error(const char* msg); |
| 133 |
|
| 134 |
protected: |
| 135 |
inline mozilla::Span<const CharT> getSource() const { |
| 136 |
return mozilla::Span<const CharT>(sourceStart.get(), current.get()); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
// Possible states the parser can be in between values. |
| 141 |
enum class JSONParserState { |
| 142 |
// An array element has just being parsed. |
| 143 |
FinishArrayElement, |
| 144 |
|
| 145 |
// An object property has just been parsed. |
| 146 |
FinishObjectMember, |
| 147 |
|
| 148 |
// At the start of the parse, before any values have been processed. |
| 149 |
JSONValue |
| 150 |
}; |
| 151 |
|
| 152 |
// Character-type-agnostic base class for JSONFullParseHandler. |
| 153 |
// JSONParser is templatized to work on either Latin1 |
| 154 |
// or TwoByte input strings, JSONFullParseHandlerAnyChar holds all state and |
| 155 |
// methods that can be shared between the two encodings. |
| 156 |
class MOZ_STACK_CLASS JSONFullParseHandlerAnyChar { |
| 157 |
public: |
| 158 |
// State related to the parser's current position. At all points in the |
| 159 |
// parse this keeps track of the stack of arrays and objects which have |
| 160 |
// been started but not finished yet. The actual JS object is not |
| 161 |
// allocated until the literal is closed, so that the result can be sized |
| 162 |
// according to its contents and have its type and shape filled in using |
| 163 |
// caches. |
| 164 |
|
| 165 |
// State for an array that is currently being parsed. This includes all |
| 166 |
// elements that have been seen so far. |
| 167 |
using ElementVector = JS::GCVector<JS::Value, 20>; |
| 168 |
|
| 169 |
// State for an object that is currently being parsed. This includes all |
| 170 |
// the key/value pairs that have been seen so far. |
| 171 |
using PropertyVector = IdValueVector; |
| 172 |
|
| 173 |
enum class ParseType { |
| 174 |
// Parsing a string as if by JSON.parse. |
| 175 |
JSONParse, |
| 176 |
// Parsing what may or may not be JSON in a string of eval code. |
| 177 |
// In this case, a failure to parse indicates either syntax that isn't JSON, |
| 178 |
// or syntax that has different semantics in eval code than in JSON. |
| 179 |
AttemptForEval, |
| 180 |
}; |
| 181 |
|
| 182 |
// Stack element for an in progress array or object. |
| 183 |
struct StackEntry { |
| 184 |
ElementVector& elements() { |
| 185 |
MOZ_ASSERT(state == JSONParserState::FinishArrayElement); |
| 186 |
return *static_cast<ElementVector*>(vector); |
| 187 |
} |
| 188 |
|
| 189 |
PropertyVector& properties() { |
| 190 |
MOZ_ASSERT(state == JSONParserState::FinishObjectMember); |
| 191 |
return *static_cast<PropertyVector*>(vector); |
| 192 |
} |
| 193 |
|
| 194 |
explicit StackEntry(JSContext* cx, ElementVector* elements) |
| 195 |
: state(JSONParserState::FinishArrayElement), vector(elements) {} |
| 196 |
|
| 197 |
explicit StackEntry(JSContext* cx, PropertyVector* properties) |
| 198 |
: state(JSONParserState::FinishObjectMember), vector(properties) {} |
| 199 |
|
| 200 |
JSONParserState state; |
| 201 |
|
| 202 |
private: |
| 203 |
void* vector; |
| 204 |
}; |
| 205 |
|
| 206 |
public: |
| 207 |
/* Data members */ |
| 208 |
|
| 209 |
JSContext* cx; |
| 210 |
|
| 211 |
bool reportLineNumbersFromParsedData = false; |
| 212 |
|
| 213 |
mozilla::Maybe<JS::ConstUTF8CharsZ> filename; |
| 214 |
|
| 215 |
JS::Value v; |
| 216 |
|
| 217 |
ParseType parseType = ParseType::JSONParse; |
| 218 |
|
| 219 |
AutoSelectGCHeap gcHeap; |
| 220 |
|
| 221 |
private: |
| 222 |
// Unused element and property vectors for previous in progress arrays and |
| 223 |
// objects. These vectors are not freed until the end of the parse to avoid |
| 224 |
// unnecessary freeing and allocation. |
| 225 |
Vector<ElementVector*, 5> freeElements; |
| 226 |
Vector<PropertyVector*, 5> freeProperties; |
| 227 |
|
| 228 |
public: |
| 229 |
explicit JSONFullParseHandlerAnyChar(JSContext* cx); |
| 230 |
~JSONFullParseHandlerAnyChar(); |
| 231 |
|
| 232 |
// Allow move construction for use with Rooted. |
| 233 |
JSONFullParseHandlerAnyChar(JSONFullParseHandlerAnyChar&& other) noexcept; |
| 234 |
|
| 235 |
JSONFullParseHandlerAnyChar(const JSONFullParseHandlerAnyChar& other) = |
| 236 |
delete; |
| 237 |
void operator=(const JSONFullParseHandlerAnyChar& other) = delete; |
| 238 |
|
| 239 |
JSContext* context() { return cx; } |
| 240 |
|
| 241 |
JS::Value numberValue() const { |
| 242 |
MOZ_ASSERT(v.isNumber()); |
| 243 |
return v; |
| 244 |
} |
| 245 |
|
| 246 |
JS::Value stringValue() const { |
| 247 |
MOZ_ASSERT(v.isString()); |
| 248 |
return v; |
| 249 |
} |
| 250 |
|
| 251 |
JSAtom* atomValue() const { |
| 252 |
JS::Value strval = stringValue(); |
| 253 |
return &strval.toString()->asAtom(); |
| 254 |
} |
| 255 |
|
| 256 |
inline JS::Value booleanValue(bool value) { return JS::BooleanValue(value); } |
| 257 |
inline JS::Value nullValue() { return JS::NullValue(); } |
| 258 |
|
| 259 |
inline bool objectOpen(Vector<StackEntry, 10>& stack, |
| 260 |
PropertyVector** properties); |
| 261 |
inline bool objectPropertyName(Vector<StackEntry, 10>& stack, |
| 262 |
bool* isProtoInEval); |
| 263 |
inline bool finishObjectMember(Vector<StackEntry, 10>& stack, |
| 264 |
JS::Handle<JS::Value> value, |
| 265 |
PropertyVector** properties); |
| 266 |
inline bool finishObject(Vector<StackEntry, 10>& stack, |
| 267 |
JS::MutableHandle<JS::Value> vp, |
| 268 |
PropertyVector* properties); |
| 269 |
|
| 270 |
inline bool arrayOpen(Vector<StackEntry, 10>& stack, |
| 271 |
ElementVector** elements); |
| 272 |
inline bool arrayElement(Vector<StackEntry, 10>& stack, |
| 273 |
JS::Handle<JS::Value> value, |
| 274 |
ElementVector** elements); |
| 275 |
inline bool finishArray(Vector<StackEntry, 10>& stack, |
| 276 |
JS::MutableHandle<JS::Value> vp, |
| 277 |
ElementVector* elements); |
| 278 |
|
| 279 |
inline bool errorReturn() const { |
| 280 |
return parseType == ParseType::AttemptForEval; |
| 281 |
} |
| 282 |
|
| 283 |
inline bool ignoreError() const { |
| 284 |
return parseType == ParseType::AttemptForEval; |
| 285 |
} |
| 286 |
|
| 287 |
inline void freeStackEntry(StackEntry& entry); |
| 288 |
|
| 289 |
void trace(JSTracer* trc); |
| 290 |
}; |
| 291 |
|
| 292 |
template <typename CharT> |
| 293 |
class MOZ_STACK_CLASS JSONFullParseHandler |
| 294 |
: public JSONFullParseHandlerAnyChar { |
| 295 |
using Base = JSONFullParseHandlerAnyChar; |
| 296 |
using CharPtr = mozilla::RangedPtr<const CharT>; |
| 297 |
|
| 298 |
public: |
| 299 |
using ContextT = JSContext; |
| 300 |
|
| 301 |
class JSONStringBuilder { |
| 302 |
public: |
| 303 |
JSStringBuilder buffer; |
| 304 |
|
| 305 |
explicit JSONStringBuilder(JSContext* cx) : buffer(cx) {} |
| 306 |
|
| 307 |
bool append(char16_t c); |
| 308 |
bool append(const CharT* begin, const CharT* end); |
| 309 |
}; |
| 310 |
|
| 311 |
explicit JSONFullParseHandler(JSContext* cx) : Base(cx) {} |
| 312 |
|
| 313 |
JSONFullParseHandler(JSONFullParseHandler&& other) noexcept |
| 314 |
: Base(std::move(other)) {} |
| 315 |
|
| 316 |
JSONFullParseHandler(const JSONFullParseHandler& other) = delete; |
| 317 |
void operator=(const JSONFullParseHandler& other) = delete; |
| 318 |
|
| 319 |
template <JSONStringType ST> |
| 320 |
inline bool setStringValue(CharPtr start, size_t length, |
| 321 |
mozilla::Span<const CharT>&& source); |
| 322 |
template <JSONStringType ST> |
| 323 |
inline bool setStringValue(JSONStringBuilder& builder, |
| 324 |
mozilla::Span<const CharT>&& source); |
| 325 |
inline bool setNumberValue(double d, mozilla::Span<const CharT>&& source); |
| 326 |
inline bool setBooleanValue(bool value, mozilla::Span<const CharT>&& source); |
| 327 |
inline bool setNullValue(mozilla::Span<const CharT>&& source); |
| 328 |
|
| 329 |
void reportError(const char* msg, uint32_t line, uint32_t column); |
| 330 |
}; |
| 331 |
|
| 332 |
template <typename CharT> |
| 333 |
class MOZ_STACK_CLASS JSONReviveHandler : public JSONFullParseHandler<CharT> { |
| 334 |
using CharPtr = mozilla::RangedPtr<const CharT>; |
| 335 |
using Base = JSONFullParseHandler<CharT>; |
| 336 |
|
| 337 |
public: |
| 338 |
using SourceT = mozilla::Span<const CharT>; |
| 339 |
|
| 340 |
using JSONStringBuilder = typename Base::JSONStringBuilder; |
| 341 |
using StackEntry = typename Base::StackEntry; |
| 342 |
using PropertyVector = typename Base::PropertyVector; |
| 343 |
using ElementVector = typename Base::ElementVector; |
| 344 |
|
| 345 |
public: |
| 346 |
explicit JSONReviveHandler(JSContext* cx) : Base(cx), parseRecordStack(cx) {} |
| 347 |
|
| 348 |
JSONReviveHandler(JSONReviveHandler&& other) noexcept |
| 349 |
: Base(std::move(other)), |
| 350 |
parseRecordStack(std::move(other.parseRecordStack)), |
| 351 |
parseRecord(std::move(other.parseRecord)) {} |
| 352 |
|
| 353 |
JSONReviveHandler(const JSONReviveHandler& other) = delete; |
| 354 |
void operator=(const JSONReviveHandler& other) = delete; |
| 355 |
|
| 356 |
JSContext* context() { return this->cx; } |
| 357 |
|
| 358 |
template <JSONStringType ST> |
| 359 |
inline bool setStringValue(CharPtr start, size_t length, SourceT&& source) { |
| 360 |
if (!Base::template setStringValue<ST>(start, length, |
| 361 |
std::forward<SourceT&&>(source))) { |
| 362 |
return false; |
| 363 |
} |
| 364 |
return finishPrimitiveParseRecord(this->v, source); |
| 365 |
} |
| 366 |
|
| 367 |
template <JSONStringType ST> |
| 368 |
inline bool setStringValue(JSONStringBuilder& builder, SourceT&& source) { |
| 369 |
if (!Base::template setStringValue<ST>(builder, |
| 370 |
std::forward<SourceT&&>(source))) { |
| 371 |
return false; |
| 372 |
} |
| 373 |
return finishPrimitiveParseRecord(this->v, source); |
| 374 |
} |
| 375 |
|
| 376 |
inline bool setNumberValue(double d, SourceT&& source) { |
| 377 |
if (!Base::setNumberValue(d, std::forward<SourceT&&>(source))) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
return finishPrimitiveParseRecord(this->v, source); |
| 381 |
} |
| 382 |
|
| 383 |
inline bool setBooleanValue(bool value, SourceT&& source) { |
| 384 |
return finishPrimitiveParseRecord(JS::BooleanValue(value), source); |
| 385 |
} |
| 386 |
inline bool setNullValue(SourceT&& source) { |
| 387 |
return finishPrimitiveParseRecord(JS::NullValue(), source); |
| 388 |
} |
| 389 |
|
| 390 |
inline bool objectOpen(Vector<StackEntry, 10>& stack, |
| 391 |
PropertyVector** properties); |
| 392 |
inline bool finishObjectMember(Vector<StackEntry, 10>& stack, |
| 393 |
JS::Handle<JS::Value> value, |
| 394 |
PropertyVector** properties); |
| 395 |
inline bool finishObject(Vector<StackEntry, 10>& stack, |
| 396 |
JS::MutableHandle<JS::Value> vp, |
| 397 |
PropertyVector* properties); |
| 398 |
|
| 399 |
inline bool arrayOpen(Vector<StackEntry, 10>& stack, |
| 400 |
ElementVector** elements); |
| 401 |
inline bool arrayElement(Vector<StackEntry, 10>& stack, |
| 402 |
JS::Handle<JS::Value> value, |
| 403 |
ElementVector** elements); |
| 404 |
inline bool finishArray(Vector<StackEntry, 10>& stack, |
| 405 |
JS::MutableHandle<JS::Value> vp, |
| 406 |
ElementVector* elements); |
| 407 |
|
| 408 |
void trace(JSTracer* trc); |
| 409 |
|
| 410 |
private: |
| 411 |
inline bool finishMemberParseRecord( |
| 412 |
Handle<JS::PropertyKey> key, |
| 413 |
Handle<ParseRecordObject::EntryMap*> parseEntry); |
| 414 |
inline bool finishCompoundParseRecord( |
| 415 |
const Value& value, Handle<ParseRecordObject::EntryMap*> parseEntry); |
| 416 |
inline bool finishPrimitiveParseRecord(const Value& value, SourceT source); |
| 417 |
|
| 418 |
GCVector<ParseRecordObject::EntryMap*, 10> parseRecordStack; |
| 419 |
|
| 420 |
public: |
| 421 |
ParseRecordObject* parseRecord = nullptr; |
| 422 |
}; |
| 423 |
|
| 424 |
template <typename CharT> |
| 425 |
class MOZ_STACK_CLASS JSONSyntaxParseHandler { |
| 426 |
private: |
| 427 |
using CharPtr = mozilla::RangedPtr<const CharT>; |
| 428 |
|
| 429 |
public: |
| 430 |
/* Types for templatized parser. */ |
| 431 |
|
| 432 |
using ContextT = FrontendContext; |
| 433 |
|
| 434 |
class DummyValue {}; |
| 435 |
|
| 436 |
struct ElementVector {}; |
| 437 |
struct PropertyVector {}; |
| 438 |
|
| 439 |
class JSONStringBuilder { |
| 440 |
public: |
| 441 |
explicit JSONStringBuilder(FrontendContext* fc) {} |
| 442 |
|
| 443 |
bool append(char16_t c) { return true; } |
| 444 |
bool append(const CharT* begin, const CharT* end) { return true; } |
| 445 |
}; |
| 446 |
|
| 447 |
struct StackEntry { |
| 448 |
JSONParserState state; |
| 449 |
}; |
| 450 |
|
| 451 |
public: |
| 452 |
FrontendContext* fc; |
| 453 |
|
| 454 |
/* Public API */ |
| 455 |
|
| 456 |
/* Create a parser for the provided JSON data. */ |
| 457 |
explicit JSONSyntaxParseHandler(FrontendContext* fc) : fc(fc) {} |
| 458 |
|
| 459 |
JSONSyntaxParseHandler(JSONSyntaxParseHandler&& other) noexcept |
| 460 |
: fc(other.fc) {} |
| 461 |
|
| 462 |
JSONSyntaxParseHandler(const JSONSyntaxParseHandler& other) = delete; |
| 463 |
void operator=(const JSONSyntaxParseHandler& other) = delete; |
| 464 |
|
| 465 |
FrontendContext* context() { return fc; } |
| 466 |
|
| 467 |
template <JSONStringType ST> |
| 468 |
inline bool setStringValue(CharPtr start, size_t length, |
| 469 |
mozilla::Span<const CharT>&& source) { |
| 470 |
return true; |
| 471 |
} |
| 472 |
|
| 473 |
template <JSONStringType ST> |
| 474 |
inline bool setStringValue(JSONStringBuilder& builder, |
| 475 |
mozilla::Span<const CharT>&& source) { |
| 476 |
return true; |
| 477 |
} |
| 478 |
|
| 479 |
inline bool setNumberValue(double d, mozilla::Span<const CharT>&& source) { |
| 480 |
return true; |
| 481 |
} |
| 482 |
inline bool setBooleanValue(bool value, mozilla::Span<const CharT>&& source) { |
| 483 |
return true; |
| 484 |
} |
| 485 |
inline bool setNullValue(mozilla::Span<const CharT>&& source) { return true; } |
| 486 |
|
| 487 |
inline DummyValue numberValue() const { return DummyValue(); } |
| 488 |
|
| 489 |
inline DummyValue stringValue() const { return DummyValue(); } |
| 490 |
|
| 491 |
inline DummyValue booleanValue(bool value) { return DummyValue(); } |
| 492 |
inline DummyValue nullValue() { return DummyValue(); } |
| 493 |
|
| 494 |
inline bool objectOpen(Vector<StackEntry, 10>& stack, |
| 495 |
PropertyVector** properties); |
| 496 |
inline bool objectPropertyName(Vector<StackEntry, 10>& stack, |
| 497 |
bool* isProtoInEval) { |
| 498 |
*isProtoInEval = false; |
| 499 |
return true; |
| 500 |
} |
| 501 |
inline bool finishObjectMember(Vector<StackEntry, 10>& stack, |
| 502 |
DummyValue& value, |
| 503 |
PropertyVector** properties) { |
| 504 |
return true; |
| 505 |
} |
| 506 |
inline bool finishObject(Vector<StackEntry, 10>& stack, DummyValue* vp, |
| 507 |
PropertyVector* properties); |
| 508 |
|
| 509 |
inline bool arrayOpen(Vector<StackEntry, 10>& stack, |
| 510 |
ElementVector** elements); |
| 511 |
inline bool arrayElement(Vector<StackEntry, 10>& stack, DummyValue& value, |
| 512 |
ElementVector** elements) { |
| 513 |
return true; |
| 514 |
} |
| 515 |
inline bool finishArray(Vector<StackEntry, 10>& stack, DummyValue* vp, |
| 516 |
ElementVector* elements); |
| 517 |
|
| 518 |
inline bool errorReturn() const { return false; } |
| 519 |
|
| 520 |
inline bool ignoreError() const { return false; } |
| 521 |
|
| 522 |
inline void freeStackEntry(StackEntry& entry) {} |
| 523 |
|
| 524 |
void reportError(const char* msg, uint32_t line, uint32_t column); |
| 525 |
}; |
| 526 |
|
| 527 |
template <typename CharT, typename HandlerT> |
| 528 |
class MOZ_STACK_CLASS JSONPerHandlerParser { |
| 529 |
using ContextT = typename HandlerT::ContextT; |
| 530 |
|
| 531 |
using Tokenizer = JSONTokenizer<CharT, JSONPerHandlerParser<CharT, HandlerT>>; |
| 532 |
|
| 533 |
public: |
| 534 |
using JSONStringBuilder = typename HandlerT::JSONStringBuilder; |
| 535 |
|
| 536 |
public: |
| 537 |
HandlerT handler; |
| 538 |
Tokenizer tokenizer; |
| 539 |
|
| 540 |
// All in progress arrays and objects being parsed, in order from outermost |
| 541 |
// to innermost. |
| 542 |
Vector<typename HandlerT::StackEntry, 10> stack; |
| 543 |
|
| 544 |
public: |
| 545 |
JSONPerHandlerParser(ContextT* context, mozilla::Range<const CharT> data) |
| 546 |
: handler(context), tokenizer(data, this), stack(context) {} |
| 547 |
|
| 548 |
JSONPerHandlerParser(JSONPerHandlerParser&& other) noexcept |
| 549 |
: handler(std::move(other.handler)), |
| 550 |
tokenizer(std::move(other.tokenizer)), |
| 551 |
stack(handler.context()) { |
| 552 |
tokenizer.fixupParser(this); |
| 553 |
} |
| 554 |
|
| 555 |
~JSONPerHandlerParser(); |
| 556 |
|
| 557 |
JSONPerHandlerParser(const JSONPerHandlerParser<CharT, HandlerT>& other) = |
| 558 |
delete; |
| 559 |
void operator=(const JSONPerHandlerParser<CharT, HandlerT>& other) = delete; |
| 560 |
|
| 561 |
template <typename TempValueT, typename ResultSetter> |
| 562 |
inline bool parseImpl(TempValueT& value, ResultSetter setResult); |
| 563 |
|
| 564 |
void outOfMemory(); |
| 565 |
|
| 566 |
void error(const char* msg); |
| 567 |
}; |
| 568 |
|
| 569 |
template <typename CharT> |
| 570 |
class MOZ_STACK_CLASS JSONParser |
| 571 |
: JSONPerHandlerParser<CharT, JSONFullParseHandler<CharT>> { |
| 572 |
using Base = JSONPerHandlerParser<CharT, JSONFullParseHandler<CharT>>; |
| 573 |
|
| 574 |
public: |
| 575 |
using ParseType = JSONFullParseHandlerAnyChar::ParseType; |
| 576 |
|
| 577 |
/* Public API */ |
| 578 |
|
| 579 |
/* Create a parser for the provided JSON data. */ |
| 580 |
JSONParser(JSContext* cx, mozilla::Range<const CharT> data, |
| 581 |
ParseType parseType) |
| 582 |
: Base(cx, data) { |
| 583 |
this->handler.parseType = parseType; |
| 584 |
} |
| 585 |
|
| 586 |
/* Allow move construction for use with Rooted. */ |
| 587 |
JSONParser(JSONParser&& other) noexcept : Base(std::move(other)) {} |
| 588 |
|
| 589 |
JSONParser(const JSONParser& other) = delete; |
| 590 |
void operator=(const JSONParser& other) = delete; |
| 591 |
|
| 592 |
/* |
| 593 |
* Parse the JSON data specified at construction time. If it parses |
| 594 |
* successfully, store the prescribed value in *vp and return true. If an |
| 595 |
* internal error (e.g. OOM) occurs during parsing, return false. |
| 596 |
* Otherwise, if invalid input was specifed but no internal error occurred, |
| 597 |
* behavior depends upon the error handling specified at construction: if |
| 598 |
* error handling is RaiseError then throw a SyntaxError and return false, |
| 599 |
* otherwise return true and set *vp to |undefined|. (JSON syntax can't |
| 600 |
* represent |undefined|, so the JSON data couldn't have specified it.) |
| 601 |
*/ |
| 602 |
bool parse(JS::MutableHandle<JS::Value> vp); |
| 603 |
|
| 604 |
void reportLineNumbersFromParsedData(bool b) { |
| 605 |
this->handler.reportLineNumbersFromParsedData = b; |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Set a filename to be used in error messages. |
| 610 |
* This is optional and only used for error reporting. |
| 611 |
*/ |
| 612 |
void setFilename(JS::ConstUTF8CharsZ filename) { |
| 613 |
this->handler.filename = mozilla::Some(filename); |
| 614 |
} |
| 615 |
|
| 616 |
void trace(JSTracer* trc); |
| 617 |
}; |
| 618 |
|
| 619 |
template <typename CharT> |
| 620 |
class MOZ_STACK_CLASS JSONReviveParser |
| 621 |
: JSONPerHandlerParser<CharT, JSONReviveHandler<CharT>> { |
| 622 |
using Base = JSONPerHandlerParser<CharT, JSONReviveHandler<CharT>>; |
| 623 |
|
| 624 |
public: |
| 625 |
using ParseType = JSONFullParseHandlerAnyChar::ParseType; |
| 626 |
|
| 627 |
/* Public API */ |
| 628 |
|
| 629 |
/* Create a parser for the provided JSON data. */ |
| 630 |
JSONReviveParser(JSContext* cx, mozilla::Range<const CharT> data) |
| 631 |
: Base(cx, data) {} |
| 632 |
|
| 633 |
/* Allow move construction for use with Rooted. */ |
| 634 |
JSONReviveParser(JSONReviveParser&& other) noexcept |
| 635 |
: Base(std::move(other)) {} |
| 636 |
|
| 637 |
JSONReviveParser(const JSONReviveParser& other) = delete; |
| 638 |
void operator=(const JSONReviveParser& other) = delete; |
| 639 |
|
| 640 |
/* |
| 641 |
* Parse the JSON data specified at construction time. If it parses |
| 642 |
* successfully, store the prescribed value in *vp and return true. If an |
| 643 |
* internal error (e.g. OOM) occurs during parsing, return false. |
| 644 |
* Otherwise, if invalid input was specifed but no internal error occurred, |
| 645 |
* behavior depends upon the error handling specified at construction: if |
| 646 |
* error handling is RaiseError then throw a SyntaxError and return false, |
| 647 |
* otherwise return true and set *vp to |undefined|. (JSON syntax can't |
| 648 |
* represent |undefined|, so the JSON data couldn't have specified it.) |
| 649 |
* |
| 650 |
* If it parses successfully, parse information for calling the reviver |
| 651 |
* function is stored in *pro. If this function returns false, *pro will be |
| 652 |
* set to |undefined|. |
| 653 |
*/ |
| 654 |
bool parse(JS::MutableHandle<JS::Value> vp, |
| 655 |
JS::MutableHandle<ParseRecordObject*> pro); |
| 656 |
|
| 657 |
void trace(JSTracer* trc); |
| 658 |
}; |
| 659 |
|
| 660 |
template <typename CharT, typename Wrapper> |
| 661 |
class MutableWrappedPtrOperations<JSONParser<CharT>, Wrapper> |
| 662 |
: public WrappedPtrOperations<JSONParser<CharT>, Wrapper> { |
| 663 |
public: |
| 664 |
bool parse(JS::MutableHandle<JS::Value> vp) { |
| 665 |
return static_cast<Wrapper*>(this)->get().parse(vp); |
| 666 |
} |
| 667 |
void setFilename(JS::ConstUTF8CharsZ filename) { |
| 668 |
static_cast<Wrapper*>(this)->get().setFilename(filename); |
| 669 |
} |
| 670 |
void reportLineNumbersFromParsedData(bool b) { |
| 671 |
static_cast<Wrapper*>(this)->get().reportLineNumbersFromParsedData(b); |
| 672 |
} |
| 673 |
}; |
| 674 |
|
| 675 |
template <typename CharT> |
| 676 |
class MOZ_STACK_CLASS JSONSyntaxParser |
| 677 |
: JSONPerHandlerParser<CharT, JSONSyntaxParseHandler<CharT>> { |
| 678 |
using HandlerT = JSONSyntaxParseHandler<CharT>; |
| 679 |
using Base = JSONPerHandlerParser<CharT, HandlerT>; |
| 680 |
|
| 681 |
public: |
| 682 |
JSONSyntaxParser(FrontendContext* fc, mozilla::Range<const CharT> data) |
| 683 |
: Base(fc, data) {} |
| 684 |
|
| 685 |
JSONSyntaxParser(JSONSyntaxParser<CharT>&& other) noexcept |
| 686 |
: Base(std::move(other)) {} |
| 687 |
|
| 688 |
JSONSyntaxParser(const JSONSyntaxParser& other) = delete; |
| 689 |
void operator=(const JSONSyntaxParser& other) = delete; |
| 690 |
|
| 691 |
bool parse(); |
| 692 |
}; |
| 693 |
|
| 694 |
} /* namespace js */ |
| 695 |
|
| 696 |
#endif /* vm_JSONParser_h */ |