RegExpConstructor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Esprima;
  4. using Jint.Collections;
  5. using Jint.Native.Function;
  6. using Jint.Native.Object;
  7. using Jint.Native.Symbol;
  8. using Jint.Runtime;
  9. using Jint.Runtime.Descriptors;
  10. using Jint.Runtime.Interop;
  11. namespace Jint.Native.RegExp
  12. {
  13. public sealed class RegExpConstructor : FunctionInstance, IConstructor
  14. {
  15. private static readonly JsString _functionName = new JsString("RegExp");
  16. internal RegExpConstructor(
  17. Engine engine,
  18. Realm realm,
  19. FunctionPrototype functionPrototype,
  20. ObjectPrototype objectPrototype)
  21. : base(engine, realm, _functionName, FunctionThisMode.Global)
  22. {
  23. _prototype = functionPrototype;
  24. PrototypeObject = new RegExpPrototype(engine, realm, this, objectPrototype);
  25. _length = new PropertyDescriptor(2, PropertyFlag.Configurable);
  26. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  27. }
  28. protected override void Initialize()
  29. {
  30. var symbols = new SymbolDictionary(1)
  31. {
  32. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", (thisObj, _) => thisObj, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  33. };
  34. SetSymbols(symbols);
  35. }
  36. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  37. {
  38. return Construct(arguments, thisObject);
  39. }
  40. public ObjectInstance Construct(JsValue[] arguments)
  41. {
  42. return Construct(arguments, this);
  43. }
  44. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments, newTarget);
  45. /// <summary>
  46. /// https://tc39.es/ecma262/#sec-regexp-pattern-flags
  47. /// </summary>
  48. private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  49. {
  50. var pattern = arguments.At(0);
  51. var flags = arguments.At(1);
  52. var patternIsRegExp = pattern.IsRegExp();
  53. if (newTarget.IsUndefined())
  54. {
  55. newTarget = this;
  56. if (patternIsRegExp && flags.IsUndefined())
  57. {
  58. var patternConstructor = pattern.Get(CommonProperties.Constructor);
  59. if (ReferenceEquals(newTarget, patternConstructor))
  60. {
  61. return (ObjectInstance) pattern;
  62. }
  63. }
  64. }
  65. JsValue p;
  66. JsValue f;
  67. if (pattern is RegExpInstance regExpInstance)
  68. {
  69. p = regExpInstance.Source;
  70. f = flags.IsUndefined() ? regExpInstance.Flags : flags;
  71. }
  72. else if (patternIsRegExp)
  73. {
  74. p = pattern.Get(RegExpPrototype.PropertySource);
  75. f = flags.IsUndefined() ? pattern.Get(RegExpPrototype.PropertyFlags) : flags;
  76. }
  77. else
  78. {
  79. p = pattern;
  80. f = flags;
  81. }
  82. var r = RegExpAlloc(newTarget);
  83. return RegExpInitialize(r, p, f);
  84. }
  85. private ObjectInstance RegExpInitialize(RegExpInstance r, JsValue pattern, JsValue flags)
  86. {
  87. var p = pattern.IsUndefined() ? "" : TypeConverter.ToString(pattern);
  88. if (string.IsNullOrEmpty(p))
  89. {
  90. p = "(?:)";
  91. }
  92. var f = flags.IsUndefined() ? "" : TypeConverter.ToString(flags);
  93. try
  94. {
  95. var scanner = new Scanner("/" + p + "/" + flags, new ParserOptions { AdaptRegexp = true });
  96. // seems valid
  97. r.Value = scanner.ParseRegex(p, f);
  98. var timeout = _engine.Options.Constraints.RegexTimeout;
  99. if (timeout.Ticks > 0)
  100. {
  101. r.Value = r.Value != null ? new Regex(r.Value.ToString(), r.Value.Options, timeout) : null;
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. ExceptionHelper.ThrowSyntaxError(_realm, ex.Message);
  107. }
  108. r.Flags = f;
  109. r.Source = p;
  110. RegExpInitialize(r);
  111. return r;
  112. }
  113. private RegExpInstance RegExpAlloc(JsValue newTarget)
  114. {
  115. var r = OrdinaryCreateFromConstructor(
  116. newTarget,
  117. static intrinsics => intrinsics.RegExp.PrototypeObject,
  118. static (Engine engine, Realm _, object _) => new RegExpInstance(engine));
  119. return r;
  120. }
  121. public RegExpInstance Construct(Regex regExp, string source, string flags)
  122. {
  123. var r = new RegExpInstance(Engine);
  124. r._prototype = PrototypeObject;
  125. r.Flags = flags;
  126. r.Source = source;
  127. var timeout = _engine.Options.Constraints.RegexTimeout;
  128. if (timeout.Ticks > 0)
  129. {
  130. r.Value = regExp != null ? new Regex(regExp.ToString(), regExp.Options, timeout) : null;
  131. }
  132. else
  133. {
  134. r.Value = regExp;
  135. }
  136. RegExpInitialize(r);
  137. return r;
  138. }
  139. private static void RegExpInitialize(RegExpInstance r)
  140. {
  141. r.SetOwnProperty(RegExpInstance.PropertyLastIndex, new PropertyDescriptor(0, PropertyFlag.OnlyWritable));
  142. }
  143. public RegExpPrototype PrototypeObject { get; private set; }
  144. }
  145. }