LazyJSONString.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace Js
  7. {
  8. struct JSONObjectProperty;
  9. struct JSONProperty;
  10. struct JSONArray;
  11. enum class JSONContentType : uint8
  12. {
  13. Array,
  14. Object,
  15. Undefined,
  16. Null,
  17. True,
  18. False,
  19. Number,
  20. String
  21. };
  22. typedef SListCounted<JSONObjectProperty, Recycler> JSONObject;
  23. struct JSONNumberData
  24. {
  25. Field(Var) value;
  26. Field(JavascriptString*) string;
  27. };
  28. struct JSONProperty
  29. {
  30. Field(JSONContentType) type;
  31. union
  32. {
  33. Field(JSONNumberData) numericValue;
  34. Field(JavascriptString*) stringValue;
  35. Field(JSONObject*) obj;
  36. Field(JSONArray*) arr;
  37. };
  38. JSONProperty()
  39. {
  40. memset(this, 0, sizeof(JSONProperty));
  41. }
  42. JSONProperty(const JSONProperty& other)
  43. {
  44. // Copy the full struct and use "Field(Var)" to identify write barrier
  45. // policy as the struct contains Vars
  46. CopyArray<JSONProperty, Field(Var)>(this, 1, &other, 1);
  47. }
  48. };
  49. struct JSONObjectProperty
  50. {
  51. Field(JavascriptString*) propertyName;
  52. Field(JSONProperty) propertyValue;
  53. JSONObjectProperty() : propertyName(nullptr), propertyValue()
  54. {
  55. }
  56. JSONObjectProperty(const JSONObjectProperty& other) :
  57. propertyName(other.propertyName),
  58. propertyValue(other.propertyValue)
  59. {
  60. }
  61. };
  62. struct JSONArray
  63. {
  64. Field(uint32) length;
  65. Field(JSONProperty) arr[];
  66. };
  67. class LazyJSONString : public JavascriptString
  68. {
  69. private:
  70. Field(charcount_t) gapLength;
  71. Field(JSONProperty*) jsonContent;
  72. Field(const char16*) gap;
  73. DynamicObject* ReconstructObject(_In_ JSONObject* valueList) const;
  74. JavascriptArray* ReconstructArray(_In_ JSONArray* valueArray) const;
  75. Var ReconstructVar(_In_ JSONProperty* content) const;
  76. static const WCHAR escapeMap[128];
  77. public:
  78. static const BYTE escapeMapCount[128];
  79. protected:
  80. DEFINE_VTABLE_CTOR(LazyJSONString, JavascriptString);
  81. public:
  82. LazyJSONString(_In_ JSONProperty* content, charcount_t length, _In_opt_ const char16* gap, charcount_t gapLength, _In_ StaticType* type);
  83. Var TryParse() const;
  84. // Tells if the string has a gap with characters that might impact JSON.parse
  85. bool HasComplexGap() const;
  86. const char16* GetSz() override sealed;
  87. virtual VTableValue DummyVirtualFunctionToHinderLinkerICF()
  88. {
  89. return VTableValue::VtableLazyJSONString;
  90. }
  91. }; // class LazyJSONString
  92. template <> bool VarIsImpl<LazyJSONString>(RecyclableObject* obj);
  93. } // namespace Js