RegExpConstructor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
  4. * Copyright (C) 2009 Torch Mobile, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include "config.h"
  22. #include "RegExpConstructor.h"
  23. #include "Error.h"
  24. #include "Operations.h"
  25. #include "RegExpMatchesArray.h"
  26. #include "RegExpPrototype.h"
  27. namespace JSC {
  28. static JSValue regExpConstructorInput(ExecState*, JSValue, PropertyName);
  29. static JSValue regExpConstructorMultiline(ExecState*, JSValue, PropertyName);
  30. static JSValue regExpConstructorLastMatch(ExecState*, JSValue, PropertyName);
  31. static JSValue regExpConstructorLastParen(ExecState*, JSValue, PropertyName);
  32. static JSValue regExpConstructorLeftContext(ExecState*, JSValue, PropertyName);
  33. static JSValue regExpConstructorRightContext(ExecState*, JSValue, PropertyName);
  34. static JSValue regExpConstructorDollar1(ExecState*, JSValue, PropertyName);
  35. static JSValue regExpConstructorDollar2(ExecState*, JSValue, PropertyName);
  36. static JSValue regExpConstructorDollar3(ExecState*, JSValue, PropertyName);
  37. static JSValue regExpConstructorDollar4(ExecState*, JSValue, PropertyName);
  38. static JSValue regExpConstructorDollar5(ExecState*, JSValue, PropertyName);
  39. static JSValue regExpConstructorDollar6(ExecState*, JSValue, PropertyName);
  40. static JSValue regExpConstructorDollar7(ExecState*, JSValue, PropertyName);
  41. static JSValue regExpConstructorDollar8(ExecState*, JSValue, PropertyName);
  42. static JSValue regExpConstructorDollar9(ExecState*, JSValue, PropertyName);
  43. static void setRegExpConstructorInput(ExecState*, JSObject*, JSValue);
  44. static void setRegExpConstructorMultiline(ExecState*, JSObject*, JSValue);
  45. } // namespace JSC
  46. #include "RegExpConstructor.lut.h"
  47. namespace JSC {
  48. const ClassInfo RegExpConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::regExpConstructorTable, CREATE_METHOD_TABLE(RegExpConstructor) };
  49. /* Source for RegExpConstructor.lut.h
  50. @begin regExpConstructorTable
  51. input regExpConstructorInput None
  52. $_ regExpConstructorInput DontEnum
  53. multiline regExpConstructorMultiline None
  54. $* regExpConstructorMultiline DontEnum
  55. lastMatch regExpConstructorLastMatch DontDelete|ReadOnly
  56. $& regExpConstructorLastMatch DontDelete|ReadOnly|DontEnum
  57. lastParen regExpConstructorLastParen DontDelete|ReadOnly
  58. $+ regExpConstructorLastParen DontDelete|ReadOnly|DontEnum
  59. leftContext regExpConstructorLeftContext DontDelete|ReadOnly
  60. $` regExpConstructorLeftContext DontDelete|ReadOnly|DontEnum
  61. rightContext regExpConstructorRightContext DontDelete|ReadOnly
  62. $' regExpConstructorRightContext DontDelete|ReadOnly|DontEnum
  63. $1 regExpConstructorDollar1 DontDelete|ReadOnly
  64. $2 regExpConstructorDollar2 DontDelete|ReadOnly
  65. $3 regExpConstructorDollar3 DontDelete|ReadOnly
  66. $4 regExpConstructorDollar4 DontDelete|ReadOnly
  67. $5 regExpConstructorDollar5 DontDelete|ReadOnly
  68. $6 regExpConstructorDollar6 DontDelete|ReadOnly
  69. $7 regExpConstructorDollar7 DontDelete|ReadOnly
  70. $8 regExpConstructorDollar8 DontDelete|ReadOnly
  71. $9 regExpConstructorDollar9 DontDelete|ReadOnly
  72. @end
  73. */
  74. RegExpConstructor::RegExpConstructor(JSGlobalObject* globalObject, Structure* structure, RegExpPrototype* regExpPrototype)
  75. : InternalFunction(globalObject, structure)
  76. , m_cachedResult(globalObject->vm(), this, regExpPrototype->regExp())
  77. , m_multiline(false)
  78. {
  79. }
  80. void RegExpConstructor::finishCreation(ExecState* exec, RegExpPrototype* regExpPrototype)
  81. {
  82. Base::finishCreation(exec->vm(), Identifier(exec, "RegExp").string());
  83. ASSERT(inherits(&s_info));
  84. // ECMA 15.10.5.1 RegExp.prototype
  85. putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly);
  86. // no. of arguments for constructor
  87. putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(2), ReadOnly | DontDelete | DontEnum);
  88. }
  89. void RegExpConstructor::destroy(JSCell* cell)
  90. {
  91. static_cast<RegExpConstructor*>(cell)->RegExpConstructor::~RegExpConstructor();
  92. }
  93. void RegExpConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
  94. {
  95. RegExpConstructor* thisObject = jsCast<RegExpConstructor*>(cell);
  96. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  97. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  98. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  99. Base::visitChildren(thisObject, visitor);
  100. thisObject->m_cachedResult.visitChildren(visitor);
  101. }
  102. JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i)
  103. {
  104. RegExpMatchesArray* array = m_cachedResult.lastResult(exec, this);
  105. if (i < array->length()) {
  106. JSValue result = JSValue(array).get(exec, i);
  107. ASSERT(result.isString() || result.isUndefined());
  108. if (!result.isUndefined())
  109. return result;
  110. }
  111. return jsEmptyString(exec);
  112. }
  113. JSValue RegExpConstructor::getLastParen(ExecState* exec)
  114. {
  115. RegExpMatchesArray* array = m_cachedResult.lastResult(exec, this);
  116. unsigned length = array->length();
  117. if (length > 1) {
  118. JSValue result = JSValue(array).get(exec, length - 1);
  119. ASSERT(result.isString() || result.isUndefined());
  120. if (!result.isUndefined())
  121. return result;
  122. }
  123. return jsEmptyString(exec);
  124. }
  125. JSValue RegExpConstructor::getLeftContext(ExecState* exec)
  126. {
  127. return m_cachedResult.lastResult(exec, this)->leftContext(exec);
  128. }
  129. JSValue RegExpConstructor::getRightContext(ExecState* exec)
  130. {
  131. return m_cachedResult.lastResult(exec, this)->rightContext(exec);
  132. }
  133. bool RegExpConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  134. {
  135. return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), jsCast<RegExpConstructor*>(cell), propertyName, slot);
  136. }
  137. bool RegExpConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  138. {
  139. return getStaticValueDescriptor<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), jsCast<RegExpConstructor*>(object), propertyName, descriptor);
  140. }
  141. JSValue regExpConstructorDollar1(ExecState* exec, JSValue slotBase, PropertyName)
  142. {
  143. return asRegExpConstructor(slotBase)->getBackref(exec, 1);
  144. }
  145. JSValue regExpConstructorDollar2(ExecState* exec, JSValue slotBase, PropertyName)
  146. {
  147. return asRegExpConstructor(slotBase)->getBackref(exec, 2);
  148. }
  149. JSValue regExpConstructorDollar3(ExecState* exec, JSValue slotBase, PropertyName)
  150. {
  151. return asRegExpConstructor(slotBase)->getBackref(exec, 3);
  152. }
  153. JSValue regExpConstructorDollar4(ExecState* exec, JSValue slotBase, PropertyName)
  154. {
  155. return asRegExpConstructor(slotBase)->getBackref(exec, 4);
  156. }
  157. JSValue regExpConstructorDollar5(ExecState* exec, JSValue slotBase, PropertyName)
  158. {
  159. return asRegExpConstructor(slotBase)->getBackref(exec, 5);
  160. }
  161. JSValue regExpConstructorDollar6(ExecState* exec, JSValue slotBase, PropertyName)
  162. {
  163. return asRegExpConstructor(slotBase)->getBackref(exec, 6);
  164. }
  165. JSValue regExpConstructorDollar7(ExecState* exec, JSValue slotBase, PropertyName)
  166. {
  167. return asRegExpConstructor(slotBase)->getBackref(exec, 7);
  168. }
  169. JSValue regExpConstructorDollar8(ExecState* exec, JSValue slotBase, PropertyName)
  170. {
  171. return asRegExpConstructor(slotBase)->getBackref(exec, 8);
  172. }
  173. JSValue regExpConstructorDollar9(ExecState* exec, JSValue slotBase, PropertyName)
  174. {
  175. return asRegExpConstructor(slotBase)->getBackref(exec, 9);
  176. }
  177. JSValue regExpConstructorInput(ExecState*, JSValue slotBase, PropertyName)
  178. {
  179. return asRegExpConstructor(slotBase)->input();
  180. }
  181. JSValue regExpConstructorMultiline(ExecState*, JSValue slotBase, PropertyName)
  182. {
  183. return jsBoolean(asRegExpConstructor(slotBase)->multiline());
  184. }
  185. JSValue regExpConstructorLastMatch(ExecState* exec, JSValue slotBase, PropertyName)
  186. {
  187. return asRegExpConstructor(slotBase)->getBackref(exec, 0);
  188. }
  189. JSValue regExpConstructorLastParen(ExecState* exec, JSValue slotBase, PropertyName)
  190. {
  191. return asRegExpConstructor(slotBase)->getLastParen(exec);
  192. }
  193. JSValue regExpConstructorLeftContext(ExecState* exec, JSValue slotBase, PropertyName)
  194. {
  195. return asRegExpConstructor(slotBase)->getLeftContext(exec);
  196. }
  197. JSValue regExpConstructorRightContext(ExecState* exec, JSValue slotBase, PropertyName)
  198. {
  199. return asRegExpConstructor(slotBase)->getRightContext(exec);
  200. }
  201. void RegExpConstructor::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  202. {
  203. lookupPut<RegExpConstructor, InternalFunction>(exec, propertyName, value, ExecState::regExpConstructorTable(exec), jsCast<RegExpConstructor*>(cell), slot);
  204. }
  205. void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, JSValue value)
  206. {
  207. asRegExpConstructor(baseObject)->setInput(exec, value.toString(exec));
  208. }
  209. void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, JSValue value)
  210. {
  211. asRegExpConstructor(baseObject)->setMultiline(value.toBoolean(exec));
  212. }
  213. // ECMA 15.10.4
  214. JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, bool callAsConstructor)
  215. {
  216. JSValue arg0 = args.at(0);
  217. JSValue arg1 = args.at(1);
  218. if (arg0.inherits(&RegExpObject::s_info)) {
  219. if (!arg1.isUndefined())
  220. return throwError(exec, createTypeError(exec, ASCIILiteral("Cannot supply flags when constructing one RegExp from another.")));
  221. // If called as a function, this just returns the first argument (see 15.10.3.1).
  222. if (callAsConstructor) {
  223. RegExp* regExp = static_cast<RegExpObject*>(asObject(arg0))->regExp();
  224. return RegExpObject::create(exec, globalObject, globalObject->regExpStructure(), regExp);
  225. }
  226. return asObject(arg0);
  227. }
  228. String pattern = arg0.isUndefined() ? String("") : arg0.toString(exec)->value(exec);
  229. if (exec->hadException())
  230. return 0;
  231. RegExpFlags flags = NoFlags;
  232. if (!arg1.isUndefined()) {
  233. flags = regExpFlags(arg1.toString(exec)->value(exec));
  234. if (exec->hadException())
  235. return 0;
  236. if (flags == InvalidFlags)
  237. return throwError(exec, createSyntaxError(exec, ASCIILiteral("Invalid flags supplied to RegExp constructor.")));
  238. }
  239. RegExp* regExp = RegExp::create(exec->vm(), pattern, flags);
  240. if (!regExp->isValid())
  241. return throwError(exec, createSyntaxError(exec, regExp->errorMessage()));
  242. return RegExpObject::create(exec, exec->lexicalGlobalObject(), globalObject->regExpStructure(), regExp);
  243. }
  244. static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
  245. {
  246. ArgList args(exec);
  247. return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, true));
  248. }
  249. ConstructType RegExpConstructor::getConstructData(JSCell*, ConstructData& constructData)
  250. {
  251. constructData.native.function = constructWithRegExpConstructor;
  252. return ConstructTypeHost;
  253. }
  254. // ECMA 15.10.3
  255. static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
  256. {
  257. ArgList args(exec);
  258. return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
  259. }
  260. CallType RegExpConstructor::getCallData(JSCell*, CallData& callData)
  261. {
  262. callData.native.function = callRegExpConstructor;
  263. return CallTypeHost;
  264. }
  265. } // namespace JSC