JavascriptRegExpConstructor.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 DeleteProperty(JavascriptString *propertyNameString, PropertyOperationFlags flags) override;
  31. virtual BOOL GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
  32. virtual BOOL GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
  33. virtual BOOL IsEnumerable(PropertyId propertyId) override;
  34. virtual BOOL IsConfigurable(PropertyId propertyId) override;
  35. virtual BOOL GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext* requestContext, ForInCache * forInCache = nullptr) override;
  36. BOOL GetSpecialNonEnumerablePropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext);
  37. uint GetSpecialNonEnumerablePropertyCount() const;
  38. PropertyId const * GetSpecialNonEnumerablePropertyIds() const;
  39. BOOL GetSpecialEnumerablePropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext);
  40. uint GetSpecialEnumerablePropertyCount() const;
  41. PropertyId const * GetSpecialEnumerablePropertyIds() const;
  42. virtual BOOL GetSpecialPropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext) override;
  43. virtual uint GetSpecialPropertyCount() const override;
  44. virtual PropertyId const * GetSpecialPropertyIds() const override;
  45. UnifiedRegex::RegexPattern* GetLastPattern() const { return lastPattern; }
  46. private:
  47. bool GetPropertyBuiltIns(PropertyId propertyId, Var* value, BOOL* result);
  48. bool SetPropertyBuiltIns(PropertyId propertyId, Var value, BOOL* result);
  49. void SetLastMatch(UnifiedRegex::RegexPattern* lastPattern, JavascriptString* lastInput, UnifiedRegex::GroupInfo lastMatch);
  50. void EnsureValues();
  51. Field(UnifiedRegex::RegexPattern*) lastPattern;
  52. Field(JavascriptString*) lastInput;
  53. Field(UnifiedRegex::GroupInfo) lastMatch;
  54. Field(bool) reset; // true if following fields must be recalculated from above before first use
  55. Field(Var) lastParen;
  56. Field(Var) lastIndex;
  57. Field(Var) index;
  58. Field(Var) leftContext;
  59. Field(Var) rightContext;
  60. Field(Var) captures[NumCtorCaptures];
  61. };
  62. class JavascriptRegExpConstructorProperties
  63. {
  64. public:
  65. static bool IsSpecialProperty(PropertyId id)
  66. {
  67. switch (id)
  68. {
  69. case PropertyIds::input:
  70. case PropertyIds::$_:
  71. case PropertyIds::lastMatch:
  72. case PropertyIds::$Ampersand:
  73. case PropertyIds::lastParen:
  74. case PropertyIds::$Plus:
  75. case PropertyIds::leftContext:
  76. case PropertyIds::$BackTick:
  77. case PropertyIds::rightContext:
  78. case PropertyIds::$Tick:
  79. case PropertyIds::$1:
  80. case PropertyIds::$2:
  81. case PropertyIds::$3:
  82. case PropertyIds::$4:
  83. case PropertyIds::$5:
  84. case PropertyIds::$6:
  85. case PropertyIds::$7:
  86. case PropertyIds::$8:
  87. case PropertyIds::$9:
  88. case PropertyIds::index:
  89. return true;
  90. }
  91. return false;
  92. }
  93. };
  94. } // namespace Js