JSONParser.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #include "JSONScanner.h"
  7. namespace JSON
  8. {
  9. class JSONDeferredParserRootNode;
  10. struct JsonTypeCache
  11. {
  12. const Js::PropertyRecord* propertyRecord;
  13. Js::DynamicType* typeWithoutProperty;
  14. Js::DynamicType* typeWithProperty;
  15. JsonTypeCache* next;
  16. Js::PropertyIndex propertyIndex;
  17. JsonTypeCache(const Js::PropertyRecord* propertyRecord, Js::DynamicType* typeWithoutProperty, Js::DynamicType* typeWithProperty, Js::PropertyIndex propertyIndex) :
  18. propertyRecord(propertyRecord),
  19. typeWithoutProperty(typeWithoutProperty),
  20. typeWithProperty(typeWithProperty),
  21. propertyIndex(propertyIndex),
  22. next(nullptr) {}
  23. static JsonTypeCache* New(ArenaAllocator* allocator,
  24. const Js::PropertyRecord* propertyRecord,
  25. Js::DynamicType* typeWithoutProperty,
  26. Js::DynamicType* typeWithProperty,
  27. Js::PropertyIndex propertyIndex)
  28. {
  29. return Anew(allocator, JsonTypeCache, propertyRecord, typeWithoutProperty, typeWithProperty, propertyIndex);
  30. }
  31. void Update(const Js::PropertyRecord* propertyRecord,
  32. Js::DynamicType* typeWithoutProperty,
  33. Js::DynamicType* typeWithProperty,
  34. Js::PropertyIndex propertyIndex)
  35. {
  36. this->propertyRecord = propertyRecord;
  37. this->typeWithoutProperty = typeWithoutProperty;
  38. this->typeWithProperty = typeWithProperty;
  39. this->propertyIndex = propertyIndex;
  40. }
  41. };
  42. class JSONParser
  43. {
  44. public:
  45. JSONParser(Js::ScriptContext* sc, Js::RecyclableObject* rv) : scriptContext(sc),
  46. reviver(rv), arenaAllocatorObject(nullptr), arenaAllocator(nullptr), typeCacheList(nullptr)
  47. {
  48. };
  49. void Finalizer();
  50. Js::Var Parse(LPCWSTR str, uint length);
  51. Js::Var Parse(Js::JavascriptString* input);
  52. Js::Var Walk(Js::JavascriptString* name, Js::PropertyId id, Js::Var holder, uint32 index = Js::JavascriptArray::InvalidIndex);
  53. private:
  54. tokens Scan()
  55. {
  56. return m_scanner.Scan();
  57. }
  58. Js::Var ParseObject();
  59. void CheckCurrentToken(int tk, int wErr)
  60. {
  61. if (m_token.tk != tk)
  62. m_scanner.ThrowSyntaxError(wErr);
  63. Scan();
  64. }
  65. bool IsCaching()
  66. {
  67. return arenaAllocator != nullptr;
  68. }
  69. Token m_token;
  70. JSONScanner m_scanner;
  71. Js::ScriptContext* scriptContext;
  72. Js::RecyclableObject* reviver;
  73. Js::TempGuestArenaAllocatorObject* arenaAllocatorObject;
  74. ArenaAllocator* arenaAllocator;
  75. typedef JsUtil::BaseDictionary<const Js::PropertyRecord *, JsonTypeCache*, ArenaAllocator, PowerOf2SizePolicy, Js::PropertyRecordStringHashComparer> JsonTypeCacheList;
  76. JsonTypeCacheList* typeCacheList;
  77. static const uint MIN_CACHE_LENGTH = 50; // Use Json type cache only if the JSON string is larger than this constant.
  78. };
  79. } // namespace JSON