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 <wchar.h>
  7. #include "JSONScanner.h"
  8. namespace JSON
  9. {
  10. class JSONDeferredParserRootNode;
  11. struct JsonTypeCache
  12. {
  13. const Js::PropertyRecord* propertyRecord;
  14. Js::DynamicType* typeWithoutProperty;
  15. Js::DynamicType* typeWithProperty;
  16. JsonTypeCache* next;
  17. Js::PropertyIndex propertyIndex;
  18. JsonTypeCache(const Js::PropertyRecord* propertyRecord, Js::DynamicType* typeWithoutProperty, Js::DynamicType* typeWithProperty, Js::PropertyIndex propertyIndex) :
  19. propertyRecord(propertyRecord),
  20. typeWithoutProperty(typeWithoutProperty),
  21. typeWithProperty(typeWithProperty),
  22. propertyIndex(propertyIndex),
  23. next(nullptr) {}
  24. static JsonTypeCache* JsonTypeCache::New(ArenaAllocator* allocator,
  25. const Js::PropertyRecord* propertyRecord,
  26. Js::DynamicType* typeWithoutProperty,
  27. Js::DynamicType* typeWithProperty,
  28. Js::PropertyIndex propertyIndex)
  29. {
  30. return Anew(allocator, JsonTypeCache, propertyRecord, typeWithoutProperty, typeWithProperty, propertyIndex);
  31. }
  32. void Update(const Js::PropertyRecord* propertyRecord,
  33. Js::DynamicType* typeWithoutProperty,
  34. Js::DynamicType* typeWithProperty,
  35. Js::PropertyIndex propertyIndex)
  36. {
  37. this->propertyRecord = propertyRecord;
  38. this->typeWithoutProperty = typeWithoutProperty;
  39. this->typeWithProperty = typeWithProperty;
  40. this->propertyIndex = propertyIndex;
  41. }
  42. };
  43. class JSONParser
  44. {
  45. public:
  46. JSONParser(Js::ScriptContext* sc, Js::RecyclableObject* rv) : scriptContext(sc),
  47. reviver(rv), arenaAllocatorObject(nullptr), arenaAllocator(nullptr), typeCacheList(nullptr)
  48. {
  49. };
  50. Js::Var Parse(LPCWSTR str, int 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. void Finalizer();
  54. private:
  55. tokens Scan()
  56. {
  57. return m_scanner.Scan();
  58. }
  59. Js::Var ParseObject();
  60. void CheckCurrentToken(int tk, int wErr)
  61. {
  62. if (m_token.tk != tk)
  63. Js::JavascriptError::ThrowSyntaxError(scriptContext, wErr);
  64. Scan();
  65. }
  66. bool IsCaching()
  67. {
  68. return arenaAllocator != nullptr;
  69. }
  70. Token m_token;
  71. JSONScanner m_scanner;
  72. Js::ScriptContext* scriptContext;
  73. Js::RecyclableObject* reviver;
  74. Js::TempGuestArenaAllocatorObject* arenaAllocatorObject;
  75. ArenaAllocator* arenaAllocator;
  76. typedef JsUtil::BaseDictionary<const Js::PropertyRecord *, JsonTypeCache*, ArenaAllocator, PowerOf2SizePolicy, Js::PropertyRecordStringHashComparer> JsonTypeCacheList;
  77. JsonTypeCacheList* typeCacheList;
  78. static const int MIN_CACHE_LENGTH = 50; // Use Json type cache only if the JSON string is larger than this constant.
  79. };
  80. } // namespace JSON