JavascriptRegExpConstructor.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. class JavascriptRegExpConstructor : public RuntimeFunction
  9. {
  10. friend class RegexHelper;
  11. private:
  12. static PropertyId const specialPropertyIds[];
  13. static PropertyId const specialnonEnumPropertyIds[];
  14. static PropertyId const specialEnumPropertyIds[];
  15. static const int NumCtorCaptures = 10;
  16. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(JavascriptRegExpConstructor);
  17. protected:
  18. //To prevent lastMatch from being cleared from cross-site marshalling
  19. DEFINE_VTABLE_CTOR_MEMBER_INIT(JavascriptRegExpConstructor, RuntimeFunction, lastMatch);
  20. public:
  21. JavascriptRegExpConstructor(DynamicType * type);
  22. virtual BOOL HasProperty(PropertyId propertyId) override;
  23. virtual BOOL GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext) override;
  24. virtual BOOL GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext) override;
  25. virtual BOOL GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext) override;
  26. virtual BOOL SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info) override;
  27. virtual BOOL SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info) override;
  28. virtual BOOL InitProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags = PropertyOperation_None, PropertyValueInfo* info = NULL) override;
  29. virtual BOOL DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags) override;
  30. virtual BOOL GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
  31. virtual BOOL GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
  32. virtual BOOL IsEnumerable(PropertyId propertyId) override;
  33. virtual BOOL IsConfigurable(PropertyId propertyId) override;
  34. virtual BOOL GetEnumerator(BOOL enumNonEnumerable, Var* enumerator, ScriptContext * requestContext, bool preferSnapshotSemantics = true, bool enumSymbols = false) override;
  35. BOOL GetSpecialNonEnumerablePropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext);
  36. uint GetSpecialNonEnumerablePropertyCount() const;
  37. PropertyId const * GetSpecialNonEnumerablePropertyIds() const;
  38. BOOL GetSpecialEnumerablePropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext);
  39. uint GetSpecialEnumerablePropertyCount() const;
  40. PropertyId const * GetSpecialEnumerablePropertyIds() const;
  41. virtual BOOL GetSpecialPropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext) override;
  42. virtual uint GetSpecialPropertyCount() const override;
  43. virtual PropertyId const * GetSpecialPropertyIds() const override;
  44. UnifiedRegex::RegexPattern* GetLastPattern() const { return lastPattern; }
  45. private:
  46. bool GetPropertyBuiltIns(PropertyId propertyId, Var* value, BOOL* result);
  47. bool SetPropertyBuiltIns(PropertyId propertyId, Var value, BOOL* result);
  48. void SetLastMatch(UnifiedRegex::RegexPattern* lastPattern, JavascriptString* lastInput, UnifiedRegex::GroupInfo lastMatch);
  49. void EnsureValues();
  50. UnifiedRegex::RegexPattern* lastPattern;
  51. JavascriptString* lastInput;
  52. UnifiedRegex::GroupInfo lastMatch;
  53. bool reset; // true if following fields must be recalculated from above before first use
  54. Var lastParen;
  55. Var lastIndex;
  56. Var index;
  57. Var leftContext;
  58. Var rightContext;
  59. Var captures[NumCtorCaptures];
  60. };
  61. class JavascriptRegExpConstructorProperties
  62. {
  63. public:
  64. static bool IsSpecialProperty(PropertyId id)
  65. {
  66. switch (id)
  67. {
  68. case PropertyIds::input:
  69. case PropertyIds::$_:
  70. case PropertyIds::lastMatch:
  71. case PropertyIds::$Ampersand:
  72. case PropertyIds::lastParen:
  73. case PropertyIds::$Plus:
  74. case PropertyIds::leftContext:
  75. case PropertyIds::$BackTick:
  76. case PropertyIds::rightContext:
  77. case PropertyIds::$Tick:
  78. case PropertyIds::$1:
  79. case PropertyIds::$2:
  80. case PropertyIds::$3:
  81. case PropertyIds::$4:
  82. case PropertyIds::$5:
  83. case PropertyIds::$6:
  84. case PropertyIds::$7:
  85. case PropertyIds::$8:
  86. case PropertyIds::$9:
  87. case PropertyIds::index:
  88. return true;
  89. }
  90. return false;
  91. }
  92. };
  93. } // namespace Js