RegExpConstructor.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. namespace Jint.Native.RegExp
  7. {
  8. public sealed class RegExpConstructor : FunctionInstance, IConstructor
  9. {
  10. public RegExpConstructor(Engine engine)
  11. : base(engine, null, null, false)
  12. {
  13. }
  14. public static RegExpConstructor CreateRegExpConstructor(Engine engine)
  15. {
  16. var obj = new RegExpConstructor(engine);
  17. obj.Extensible = true;
  18. // The value of the [[Prototype]] internal property of the RegExp constructor is the Function prototype object
  19. obj.Prototype = engine.Function.PrototypeObject;
  20. obj.PrototypeObject = RegExpPrototype.CreatePrototypeObject(engine, obj);
  21. obj.FastAddProperty("length", 2, false, false, false);
  22. // The initial value of RegExp.prototype is the RegExp prototype object
  23. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  24. return obj;
  25. }
  26. public void Configure()
  27. {
  28. }
  29. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  30. {
  31. var pattern = arguments.At(0);
  32. var flags = arguments.At(1);
  33. if (pattern != Undefined.Instance && flags == Undefined.Instance && TypeConverter.ToObject(Engine, pattern).Class == "Regex")
  34. {
  35. return pattern;
  36. }
  37. return Construct(arguments);
  38. }
  39. /// <summary>
  40. /// http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5
  41. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.4
  42. /// </summary>
  43. /// <param name="arguments"></param>
  44. /// <returns></returns>
  45. public ObjectInstance Construct(JsValue[] arguments)
  46. {
  47. string p;
  48. string f;
  49. var pattern = arguments.At(0);
  50. var flags = arguments.At(1);
  51. var r = pattern.TryCast<RegExpInstance>();
  52. if (flags == Undefined.Instance && r != null)
  53. {
  54. return r;
  55. }
  56. else if (flags != Undefined.Instance && r != null)
  57. {
  58. throw new JavaScriptException(Engine.TypeError);
  59. }
  60. else
  61. {
  62. if (pattern == Undefined.Instance)
  63. {
  64. p = "";
  65. }
  66. else
  67. {
  68. p = TypeConverter.ToString(pattern);
  69. }
  70. f = flags != Undefined.Instance ? TypeConverter.ToString(flags) : "";
  71. }
  72. r = new RegExpInstance(Engine);
  73. r.Prototype = PrototypeObject;
  74. r.Extensible = true;
  75. var options = ParseOptions(r, f);
  76. try
  77. {
  78. r.Value = new Regex(p, options);
  79. }
  80. catch (Exception e)
  81. {
  82. throw new JavaScriptException(Engine.SyntaxError, e.Message);
  83. }
  84. string s;
  85. s = p;
  86. if (System.String.IsNullOrEmpty(s))
  87. {
  88. s = "(?:)";
  89. }
  90. r.Flags = f;
  91. r.Source = s;
  92. r.FastAddProperty("global", r.Global, false, false, false);
  93. r.FastAddProperty("ignoreCase", r.IgnoreCase, false, false, false);
  94. r.FastAddProperty("multiline", r.Multiline, false, false, false);
  95. r.FastAddProperty("source", r.Source, false, false, false);
  96. r.FastAddProperty("lastIndex", 0, true, false, false);
  97. return r;
  98. }
  99. public RegExpInstance Construct(string regExp)
  100. {
  101. var r = new RegExpInstance(Engine);
  102. r.Prototype = PrototypeObject;
  103. r.Extensible = true;
  104. if (regExp[0] != '/')
  105. {
  106. throw new JavaScriptException(Engine.SyntaxError, "Regexp should start with slash");
  107. }
  108. var lastSlash = regExp.LastIndexOf('/');
  109. // Unescape escaped forward slashes (\/)
  110. var pattern = regExp.Substring(1, lastSlash - 1).Replace("\\/", "/");
  111. var flags = regExp.Substring(lastSlash + 1);
  112. var options = ParseOptions(r, flags);
  113. try
  114. {
  115. if((RegexOptions.Multiline & options) == RegexOptions.Multiline)
  116. {
  117. // Replace all non-escaped $ occurences by \r?$
  118. // c.f. http://programmaticallyspeaking.com/regular-expression-multiline-mode-whats-a-newline.html
  119. int index = 0;
  120. var newPattern = pattern;
  121. while((index = newPattern.IndexOf("$", index)) != -1)
  122. {
  123. if(index > 0 && newPattern[index - 1] != '\\')
  124. {
  125. newPattern = newPattern.Substring(0, index) + @"\r?" + newPattern.Substring(index);
  126. index += 4;
  127. }
  128. }
  129. r.Value = new Regex(newPattern, options);
  130. }
  131. else
  132. {
  133. r.Value = new Regex(pattern, options);
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. throw new JavaScriptException(Engine.SyntaxError, e.Message);
  139. }
  140. r.Flags = flags;
  141. r.Source = System.String.IsNullOrEmpty(pattern) ? "(?:)" : pattern;
  142. r.FastAddProperty("global", r.Global, false, false, false);
  143. r.FastAddProperty("ignoreCase", r.IgnoreCase, false, false, false);
  144. r.FastAddProperty("multiline", r.Multiline, false, false, false);
  145. r.FastAddProperty("source", r.Source, false, false, false);
  146. r.FastAddProperty("lastIndex", 0, true, false, false);
  147. return r;
  148. }
  149. private RegexOptions ParseOptions(RegExpInstance r, string flags)
  150. {
  151. for (int k = 0; k < flags.Length; k++)
  152. {
  153. var c = flags[k];
  154. if (c == 'g')
  155. {
  156. if (r.Global)
  157. {
  158. throw new JavaScriptException(Engine.SyntaxError);
  159. }
  160. r.Global = true;
  161. }
  162. else if (c == 'i')
  163. {
  164. if (r.IgnoreCase)
  165. {
  166. throw new JavaScriptException(Engine.SyntaxError);
  167. }
  168. r.IgnoreCase = true;
  169. }
  170. else if (c == 'm')
  171. {
  172. if (r.Multiline)
  173. {
  174. throw new JavaScriptException(Engine.SyntaxError);
  175. }
  176. r.Multiline = true;
  177. }
  178. else
  179. {
  180. throw new JavaScriptException(Engine.SyntaxError);
  181. }
  182. }
  183. var options = RegexOptions.ECMAScript;
  184. if (r.Multiline)
  185. {
  186. options = options | RegexOptions.Multiline;
  187. }
  188. if (r.IgnoreCase)
  189. {
  190. options = options | RegexOptions.IgnoreCase;
  191. }
  192. return options;
  193. }
  194. public RegExpPrototype PrototypeObject { get; private set; }
  195. }
  196. }