RegExpObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2003, 2007, 2008, 2012 Apple Inc. All Rights Reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #include "config.h"
  21. #include "RegExpObject.h"
  22. #include "ButterflyInlines.h"
  23. #include "CopiedSpaceInlines.h"
  24. #include "Error.h"
  25. #include "ExceptionHelpers.h"
  26. #include "JSArray.h"
  27. #include "JSGlobalObject.h"
  28. #include "JSString.h"
  29. #include "Lexer.h"
  30. #include "Lookup.h"
  31. #include "Operations.h"
  32. #include "RegExpConstructor.h"
  33. #include "RegExpMatchesArray.h"
  34. #include "RegExpPrototype.h"
  35. #include <wtf/PassOwnPtr.h>
  36. #include <wtf/text/StringBuilder.h>
  37. namespace JSC {
  38. static JSValue regExpObjectGlobal(ExecState*, JSValue, PropertyName);
  39. static JSValue regExpObjectIgnoreCase(ExecState*, JSValue, PropertyName);
  40. static JSValue regExpObjectMultiline(ExecState*, JSValue, PropertyName);
  41. static JSValue regExpObjectSource(ExecState*, JSValue, PropertyName);
  42. } // namespace JSC
  43. #include "RegExpObject.lut.h"
  44. namespace JSC {
  45. ASSERT_HAS_TRIVIAL_DESTRUCTOR(RegExpObject);
  46. const ClassInfo RegExpObject::s_info = { "RegExp", &Base::s_info, 0, ExecState::regExpTable, CREATE_METHOD_TABLE(RegExpObject) };
  47. /* Source for RegExpObject.lut.h
  48. @begin regExpTable
  49. global regExpObjectGlobal DontDelete|ReadOnly|DontEnum
  50. ignoreCase regExpObjectIgnoreCase DontDelete|ReadOnly|DontEnum
  51. multiline regExpObjectMultiline DontDelete|ReadOnly|DontEnum
  52. source regExpObjectSource DontDelete|ReadOnly|DontEnum
  53. @end
  54. */
  55. RegExpObject::RegExpObject(JSGlobalObject* globalObject, Structure* structure, RegExp* regExp)
  56. : JSNonFinalObject(globalObject->vm(), structure)
  57. , m_regExp(globalObject->vm(), this, regExp)
  58. , m_lastIndexIsWritable(true)
  59. {
  60. m_lastIndex.setWithoutWriteBarrier(jsNumber(0));
  61. }
  62. void RegExpObject::finishCreation(JSGlobalObject* globalObject)
  63. {
  64. Base::finishCreation(globalObject->vm());
  65. ASSERT(inherits(&s_info));
  66. }
  67. void RegExpObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
  68. {
  69. RegExpObject* thisObject = jsCast<RegExpObject*>(cell);
  70. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  71. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  72. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  73. Base::visitChildren(thisObject, visitor);
  74. visitor.append(&thisObject->m_regExp);
  75. visitor.append(&thisObject->m_lastIndex);
  76. }
  77. bool RegExpObject::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  78. {
  79. if (propertyName == exec->propertyNames().lastIndex) {
  80. RegExpObject* regExp = asRegExpObject(cell);
  81. slot.setValue(regExp, regExp->getLastIndex());
  82. return true;
  83. }
  84. return getStaticValueSlot<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), jsCast<RegExpObject*>(cell), propertyName, slot);
  85. }
  86. bool RegExpObject::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  87. {
  88. if (propertyName == exec->propertyNames().lastIndex) {
  89. RegExpObject* regExp = asRegExpObject(object);
  90. descriptor.setDescriptor(regExp->getLastIndex(), regExp->m_lastIndexIsWritable ? DontDelete | DontEnum : DontDelete | DontEnum | ReadOnly);
  91. return true;
  92. }
  93. return getStaticValueDescriptor<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), jsCast<RegExpObject*>(object), propertyName, descriptor);
  94. }
  95. bool RegExpObject::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
  96. {
  97. if (propertyName == exec->propertyNames().lastIndex)
  98. return false;
  99. return Base::deleteProperty(cell, exec, propertyName);
  100. }
  101. void RegExpObject::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
  102. {
  103. if (mode == IncludeDontEnumProperties)
  104. propertyNames.add(exec->propertyNames().lastIndex);
  105. Base::getOwnNonIndexPropertyNames(object, exec, propertyNames, mode);
  106. }
  107. void RegExpObject::getPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
  108. {
  109. if (mode == IncludeDontEnumProperties)
  110. propertyNames.add(exec->propertyNames().lastIndex);
  111. Base::getPropertyNames(object, exec, propertyNames, mode);
  112. }
  113. static bool reject(ExecState* exec, bool throwException, const char* message)
  114. {
  115. if (throwException)
  116. throwTypeError(exec, ASCIILiteral(message));
  117. return false;
  118. }
  119. bool RegExpObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor, bool shouldThrow)
  120. {
  121. if (propertyName == exec->propertyNames().lastIndex) {
  122. RegExpObject* regExp = asRegExpObject(object);
  123. if (descriptor.configurablePresent() && descriptor.configurable())
  124. return reject(exec, shouldThrow, "Attempting to change configurable attribute of unconfigurable property.");
  125. if (descriptor.enumerablePresent() && descriptor.enumerable())
  126. return reject(exec, shouldThrow, "Attempting to change enumerable attribute of unconfigurable property.");
  127. if (descriptor.isAccessorDescriptor())
  128. return reject(exec, shouldThrow, "Attempting to change access mechanism for an unconfigurable property.");
  129. if (!regExp->m_lastIndexIsWritable) {
  130. if (descriptor.writablePresent() && descriptor.writable())
  131. return reject(exec, shouldThrow, "Attempting to change writable attribute of unconfigurable property.");
  132. if (!sameValue(exec, regExp->getLastIndex(), descriptor.value()))
  133. return reject(exec, shouldThrow, "Attempting to change value of a readonly property.");
  134. return true;
  135. }
  136. if (descriptor.writablePresent() && !descriptor.writable())
  137. regExp->m_lastIndexIsWritable = false;
  138. if (descriptor.value())
  139. regExp->setLastIndex(exec, descriptor.value(), false);
  140. return true;
  141. }
  142. return Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow);
  143. }
  144. JSValue regExpObjectGlobal(ExecState*, JSValue slotBase, PropertyName)
  145. {
  146. return jsBoolean(asRegExpObject(slotBase)->regExp()->global());
  147. }
  148. JSValue regExpObjectIgnoreCase(ExecState*, JSValue slotBase, PropertyName)
  149. {
  150. return jsBoolean(asRegExpObject(slotBase)->regExp()->ignoreCase());
  151. }
  152. JSValue regExpObjectMultiline(ExecState*, JSValue slotBase, PropertyName)
  153. {
  154. return jsBoolean(asRegExpObject(slotBase)->regExp()->multiline());
  155. }
  156. template <typename CharacterType>
  157. static inline void appendLineTerminatorEscape(StringBuilder&, CharacterType);
  158. template <>
  159. inline void appendLineTerminatorEscape<LChar>(StringBuilder& builder, LChar lineTerminator)
  160. {
  161. if (lineTerminator == '\n')
  162. builder.append('n');
  163. else
  164. builder.append('r');
  165. }
  166. template <>
  167. inline void appendLineTerminatorEscape<UChar>(StringBuilder& builder, UChar lineTerminator)
  168. {
  169. if (lineTerminator == '\n')
  170. builder.append('n');
  171. else if (lineTerminator == '\r')
  172. builder.append('r');
  173. else if (lineTerminator == 0x2028)
  174. builder.appendLiteral("u2028");
  175. else
  176. builder.appendLiteral("u2029");
  177. }
  178. template <typename CharacterType>
  179. static inline JSValue regExpObjectSourceInternal(ExecState* exec, String pattern, const CharacterType* characters, unsigned length)
  180. {
  181. bool previousCharacterWasBackslash = false;
  182. bool inBrackets = false;
  183. bool shouldEscape = false;
  184. // 15.10.6.4 specifies that RegExp.prototype.toString must return '/' + source + '/',
  185. // and also states that the result must be a valid RegularExpressionLiteral. '//' is
  186. // not a valid RegularExpressionLiteral (since it is a single line comment), and hence
  187. // source cannot ever validly be "". If the source is empty, return a different Pattern
  188. // that would match the same thing.
  189. if (!length)
  190. return jsNontrivialString(exec, ASCIILiteral("(?:)"));
  191. // early return for strings that don't contain a forwards slash and LineTerminator
  192. for (unsigned i = 0; i < length; ++i) {
  193. CharacterType ch = characters[i];
  194. if (!previousCharacterWasBackslash) {
  195. if (inBrackets) {
  196. if (ch == ']')
  197. inBrackets = false;
  198. } else {
  199. if (ch == '/') {
  200. shouldEscape = true;
  201. break;
  202. }
  203. if (ch == '[')
  204. inBrackets = true;
  205. }
  206. }
  207. if (Lexer<CharacterType>::isLineTerminator(ch)) {
  208. shouldEscape = true;
  209. break;
  210. }
  211. if (previousCharacterWasBackslash)
  212. previousCharacterWasBackslash = false;
  213. else
  214. previousCharacterWasBackslash = ch == '\\';
  215. }
  216. if (!shouldEscape)
  217. return jsString(exec, pattern);
  218. previousCharacterWasBackslash = false;
  219. inBrackets = false;
  220. StringBuilder result;
  221. for (unsigned i = 0; i < length; ++i) {
  222. CharacterType ch = characters[i];
  223. if (!previousCharacterWasBackslash) {
  224. if (inBrackets) {
  225. if (ch == ']')
  226. inBrackets = false;
  227. } else {
  228. if (ch == '/')
  229. result.append('\\');
  230. else if (ch == '[')
  231. inBrackets = true;
  232. }
  233. }
  234. // escape LineTerminator
  235. if (Lexer<CharacterType>::isLineTerminator(ch)) {
  236. if (!previousCharacterWasBackslash)
  237. result.append('\\');
  238. appendLineTerminatorEscape<CharacterType>(result, ch);
  239. } else
  240. result.append(ch);
  241. if (previousCharacterWasBackslash)
  242. previousCharacterWasBackslash = false;
  243. else
  244. previousCharacterWasBackslash = ch == '\\';
  245. }
  246. return jsString(exec, result.toString());
  247. }
  248. JSValue regExpObjectSource(ExecState* exec, JSValue slotBase, PropertyName)
  249. {
  250. String pattern = asRegExpObject(slotBase)->regExp()->pattern();
  251. if (pattern.is8Bit())
  252. return regExpObjectSourceInternal(exec, pattern, pattern.characters8(), pattern.length());
  253. return regExpObjectSourceInternal(exec, pattern, pattern.characters16(), pattern.length());
  254. }
  255. void RegExpObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  256. {
  257. if (propertyName == exec->propertyNames().lastIndex) {
  258. asRegExpObject(cell)->setLastIndex(exec, value, slot.isStrictMode());
  259. return;
  260. }
  261. lookupPut<RegExpObject, JSObject>(exec, propertyName, value, ExecState::regExpTable(exec), jsCast<RegExpObject*>(cell), slot);
  262. }
  263. JSValue RegExpObject::exec(ExecState* exec, JSString* string)
  264. {
  265. if (MatchResult result = match(exec, string))
  266. return RegExpMatchesArray::create(exec, string, regExp(), result);
  267. return jsNull();
  268. }
  269. // Shared implementation used by test and exec.
  270. MatchResult RegExpObject::match(ExecState* exec, JSString* string)
  271. {
  272. RegExp* regExp = this->regExp();
  273. RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
  274. String input = string->value(exec);
  275. VM& vm = exec->vm();
  276. if (!regExp->global())
  277. return regExpConstructor->performMatch(vm, regExp, string, input, 0);
  278. JSValue jsLastIndex = getLastIndex();
  279. unsigned lastIndex;
  280. if (LIKELY(jsLastIndex.isUInt32())) {
  281. lastIndex = jsLastIndex.asUInt32();
  282. if (lastIndex > input.length()) {
  283. setLastIndex(exec, 0);
  284. return MatchResult::failed();
  285. }
  286. } else {
  287. double doubleLastIndex = jsLastIndex.toInteger(exec);
  288. if (doubleLastIndex < 0 || doubleLastIndex > input.length()) {
  289. setLastIndex(exec, 0);
  290. return MatchResult::failed();
  291. }
  292. lastIndex = static_cast<unsigned>(doubleLastIndex);
  293. }
  294. MatchResult result = regExpConstructor->performMatch(vm, regExp, string, input, lastIndex);
  295. setLastIndex(exec, result.end);
  296. return result;
  297. }
  298. } // namespace JSC