Arguments.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2001 Peter Kelly (pmk@post.com)
  4. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  5. * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
  6. * Copyright (C) 2007 Maks Orlovich
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public License
  19. * along with this library; see the file COPYING.LIB. If not, write to
  20. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. *
  23. */
  24. #include "config.h"
  25. #include "Arguments.h"
  26. #include "JSActivation.h"
  27. #include "JSFunction.h"
  28. #include "JSGlobalObject.h"
  29. #include "Operations.h"
  30. using namespace std;
  31. namespace JSC {
  32. const ClassInfo Arguments::s_info = { "Arguments", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(Arguments) };
  33. void Arguments::visitChildren(JSCell* cell, SlotVisitor& visitor)
  34. {
  35. Arguments* thisObject = jsCast<Arguments*>(cell);
  36. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  37. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  38. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  39. JSObject::visitChildren(thisObject, visitor);
  40. if (thisObject->m_registerArray)
  41. visitor.appendValues(thisObject->m_registerArray.get(), thisObject->m_numArguments);
  42. visitor.append(&thisObject->m_callee);
  43. visitor.append(&thisObject->m_activation);
  44. }
  45. void Arguments::destroy(JSCell* cell)
  46. {
  47. static_cast<Arguments*>(cell)->Arguments::~Arguments();
  48. }
  49. void Arguments::copyToArguments(ExecState* exec, CallFrame* callFrame, uint32_t length)
  50. {
  51. if (UNLIKELY(m_overrodeLength)) {
  52. length = min(get(exec, exec->propertyNames().length).toUInt32(exec), length);
  53. for (unsigned i = 0; i < length; i++)
  54. callFrame->setArgument(i, get(exec, i));
  55. return;
  56. }
  57. ASSERT(length == this->length(exec));
  58. for (size_t i = 0; i < length; ++i) {
  59. if (JSValue value = tryGetArgument(i))
  60. callFrame->setArgument(i, value);
  61. else
  62. callFrame->setArgument(i, get(exec, i));
  63. }
  64. }
  65. void Arguments::fillArgList(ExecState* exec, MarkedArgumentBuffer& args)
  66. {
  67. if (UNLIKELY(m_overrodeLength)) {
  68. unsigned length = get(exec, exec->propertyNames().length).toUInt32(exec);
  69. for (unsigned i = 0; i < length; i++)
  70. args.append(get(exec, i));
  71. return;
  72. }
  73. uint32_t length = this->length(exec);
  74. for (size_t i = 0; i < length; ++i) {
  75. if (JSValue value = tryGetArgument(i))
  76. args.append(value);
  77. else
  78. args.append(get(exec, i));
  79. }
  80. }
  81. bool Arguments::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned i, PropertySlot& slot)
  82. {
  83. Arguments* thisObject = jsCast<Arguments*>(cell);
  84. if (JSValue value = thisObject->tryGetArgument(i)) {
  85. slot.setValue(value);
  86. return true;
  87. }
  88. return JSObject::getOwnPropertySlot(thisObject, exec, Identifier(exec, String::number(i)), slot);
  89. }
  90. void Arguments::createStrictModeCallerIfNecessary(ExecState* exec)
  91. {
  92. if (m_overrodeCaller)
  93. return;
  94. m_overrodeCaller = true;
  95. PropertyDescriptor descriptor;
  96. descriptor.setAccessorDescriptor(globalObject()->throwTypeErrorGetterSetter(exec), DontEnum | DontDelete | Accessor);
  97. methodTable()->defineOwnProperty(this, exec, exec->propertyNames().caller, descriptor, false);
  98. }
  99. void Arguments::createStrictModeCalleeIfNecessary(ExecState* exec)
  100. {
  101. if (m_overrodeCallee)
  102. return;
  103. m_overrodeCallee = true;
  104. PropertyDescriptor descriptor;
  105. descriptor.setAccessorDescriptor(globalObject()->throwTypeErrorGetterSetter(exec), DontEnum | DontDelete | Accessor);
  106. methodTable()->defineOwnProperty(this, exec, exec->propertyNames().callee, descriptor, false);
  107. }
  108. bool Arguments::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  109. {
  110. Arguments* thisObject = jsCast<Arguments*>(cell);
  111. unsigned i = propertyName.asIndex();
  112. if (JSValue value = thisObject->tryGetArgument(i)) {
  113. RELEASE_ASSERT(i < PropertyName::NotAnIndex);
  114. slot.setValue(value);
  115. return true;
  116. }
  117. if (propertyName == exec->propertyNames().length && LIKELY(!thisObject->m_overrodeLength)) {
  118. slot.setValue(jsNumber(thisObject->m_numArguments));
  119. return true;
  120. }
  121. if (propertyName == exec->propertyNames().callee && LIKELY(!thisObject->m_overrodeCallee)) {
  122. if (!thisObject->m_isStrictMode) {
  123. slot.setValue(thisObject->m_callee.get());
  124. return true;
  125. }
  126. thisObject->createStrictModeCalleeIfNecessary(exec);
  127. }
  128. if (propertyName == exec->propertyNames().caller && thisObject->m_isStrictMode)
  129. thisObject->createStrictModeCallerIfNecessary(exec);
  130. return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
  131. }
  132. bool Arguments::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  133. {
  134. Arguments* thisObject = jsCast<Arguments*>(object);
  135. unsigned i = propertyName.asIndex();
  136. if (JSValue value = thisObject->tryGetArgument(i)) {
  137. RELEASE_ASSERT(i < PropertyName::NotAnIndex);
  138. descriptor.setDescriptor(value, None);
  139. return true;
  140. }
  141. if (propertyName == exec->propertyNames().length && LIKELY(!thisObject->m_overrodeLength)) {
  142. descriptor.setDescriptor(jsNumber(thisObject->m_numArguments), DontEnum);
  143. return true;
  144. }
  145. if (propertyName == exec->propertyNames().callee && LIKELY(!thisObject->m_overrodeCallee)) {
  146. if (!thisObject->m_isStrictMode) {
  147. descriptor.setDescriptor(thisObject->m_callee.get(), DontEnum);
  148. return true;
  149. }
  150. thisObject->createStrictModeCalleeIfNecessary(exec);
  151. }
  152. if (propertyName == exec->propertyNames().caller && thisObject->m_isStrictMode)
  153. thisObject->createStrictModeCallerIfNecessary(exec);
  154. return JSObject::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
  155. }
  156. void Arguments::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
  157. {
  158. Arguments* thisObject = jsCast<Arguments*>(object);
  159. for (unsigned i = 0; i < thisObject->m_numArguments; ++i) {
  160. if (!thisObject->isArgument(i))
  161. continue;
  162. propertyNames.add(Identifier(exec, String::number(i)));
  163. }
  164. if (mode == IncludeDontEnumProperties) {
  165. propertyNames.add(exec->propertyNames().callee);
  166. propertyNames.add(exec->propertyNames().length);
  167. }
  168. JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
  169. }
  170. void Arguments::putByIndex(JSCell* cell, ExecState* exec, unsigned i, JSValue value, bool shouldThrow)
  171. {
  172. Arguments* thisObject = jsCast<Arguments*>(cell);
  173. if (thisObject->trySetArgument(exec->vm(), i, value))
  174. return;
  175. PutPropertySlot slot(shouldThrow);
  176. JSObject::put(thisObject, exec, Identifier(exec, String::number(i)), value, slot);
  177. }
  178. void Arguments::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  179. {
  180. Arguments* thisObject = jsCast<Arguments*>(cell);
  181. unsigned i = propertyName.asIndex();
  182. if (thisObject->trySetArgument(exec->vm(), i, value))
  183. return;
  184. if (propertyName == exec->propertyNames().length && !thisObject->m_overrodeLength) {
  185. thisObject->m_overrodeLength = true;
  186. thisObject->putDirect(exec->vm(), propertyName, value, DontEnum);
  187. return;
  188. }
  189. if (propertyName == exec->propertyNames().callee && !thisObject->m_overrodeCallee) {
  190. if (!thisObject->m_isStrictMode) {
  191. thisObject->m_overrodeCallee = true;
  192. thisObject->putDirect(exec->vm(), propertyName, value, DontEnum);
  193. return;
  194. }
  195. thisObject->createStrictModeCalleeIfNecessary(exec);
  196. }
  197. if (propertyName == exec->propertyNames().caller && thisObject->m_isStrictMode)
  198. thisObject->createStrictModeCallerIfNecessary(exec);
  199. JSObject::put(thisObject, exec, propertyName, value, slot);
  200. }
  201. bool Arguments::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned i)
  202. {
  203. Arguments* thisObject = jsCast<Arguments*>(cell);
  204. if (i < thisObject->m_numArguments) {
  205. if (!Base::deletePropertyByIndex(cell, exec, i))
  206. return false;
  207. if (thisObject->tryDeleteArgument(i))
  208. return true;
  209. }
  210. return JSObject::deletePropertyByIndex(thisObject, exec, i);
  211. }
  212. bool Arguments::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
  213. {
  214. if (exec->vm().isInDefineOwnProperty())
  215. return Base::deleteProperty(cell, exec, propertyName);
  216. Arguments* thisObject = jsCast<Arguments*>(cell);
  217. unsigned i = propertyName.asIndex();
  218. if (i < thisObject->m_numArguments) {
  219. RELEASE_ASSERT(i < PropertyName::NotAnIndex);
  220. if (!Base::deleteProperty(cell, exec, propertyName))
  221. return false;
  222. if (thisObject->tryDeleteArgument(i))
  223. return true;
  224. }
  225. if (propertyName == exec->propertyNames().length && !thisObject->m_overrodeLength) {
  226. thisObject->m_overrodeLength = true;
  227. return true;
  228. }
  229. if (propertyName == exec->propertyNames().callee && !thisObject->m_overrodeCallee) {
  230. if (!thisObject->m_isStrictMode) {
  231. thisObject->m_overrodeCallee = true;
  232. return true;
  233. }
  234. thisObject->createStrictModeCalleeIfNecessary(exec);
  235. }
  236. if (propertyName == exec->propertyNames().caller && thisObject->m_isStrictMode)
  237. thisObject->createStrictModeCallerIfNecessary(exec);
  238. return JSObject::deleteProperty(thisObject, exec, propertyName);
  239. }
  240. bool Arguments::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor, bool shouldThrow)
  241. {
  242. Arguments* thisObject = jsCast<Arguments*>(object);
  243. unsigned i = propertyName.asIndex();
  244. if (i < thisObject->m_numArguments) {
  245. RELEASE_ASSERT(i < PropertyName::NotAnIndex);
  246. // If the property is not yet present on the object, and is not yet marked as deleted, then add it now.
  247. PropertySlot slot;
  248. if (!thisObject->isDeletedArgument(i) && !JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot)) {
  249. JSValue value = thisObject->tryGetArgument(i);
  250. ASSERT(value);
  251. object->putDirectMayBeIndex(exec, propertyName, value);
  252. }
  253. if (!Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow))
  254. return false;
  255. // From ES 5.1, 10.6 Arguments Object
  256. // 5. If the value of isMapped is not undefined, then
  257. if (thisObject->isArgument(i)) {
  258. // a. If IsAccessorDescriptor(Desc) is true, then
  259. if (descriptor.isAccessorDescriptor()) {
  260. // i. Call the [[Delete]] internal method of map passing P, and false as the arguments.
  261. thisObject->tryDeleteArgument(i);
  262. } else { // b. Else
  263. // i. If Desc.[[Value]] is present, then
  264. // 1. Call the [[Put]] internal method of map passing P, Desc.[[Value]], and Throw as the arguments.
  265. if (descriptor.value())
  266. thisObject->trySetArgument(exec->vm(), i, descriptor.value());
  267. // ii. If Desc.[[Writable]] is present and its value is false, then
  268. // 1. Call the [[Delete]] internal method of map passing P and false as arguments.
  269. if (descriptor.writablePresent() && !descriptor.writable())
  270. thisObject->tryDeleteArgument(i);
  271. }
  272. }
  273. return true;
  274. }
  275. if (propertyName == exec->propertyNames().length && !thisObject->m_overrodeLength) {
  276. thisObject->putDirect(exec->vm(), propertyName, jsNumber(thisObject->m_numArguments), DontEnum);
  277. thisObject->m_overrodeLength = true;
  278. } else if (propertyName == exec->propertyNames().callee && !thisObject->m_overrodeCallee) {
  279. thisObject->putDirect(exec->vm(), propertyName, thisObject->m_callee.get(), DontEnum);
  280. thisObject->m_overrodeCallee = true;
  281. } else if (propertyName == exec->propertyNames().caller && thisObject->m_isStrictMode)
  282. thisObject->createStrictModeCallerIfNecessary(exec);
  283. return Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow);
  284. }
  285. void Arguments::tearOff(CallFrame* callFrame)
  286. {
  287. if (isTornOff())
  288. return;
  289. if (!m_numArguments)
  290. return;
  291. // Must be called for the same call frame from which it was created.
  292. ASSERT(bitwise_cast<WriteBarrier<Unknown>*>(callFrame) == m_registers);
  293. m_registerArray = adoptArrayPtr(new WriteBarrier<Unknown>[m_numArguments]);
  294. m_registers = m_registerArray.get() + CallFrame::offsetFor(m_numArguments + 1);
  295. // If we have a captured argument that logically aliases activation storage,
  296. // but we optimize away the activation, the argument needs to tear off into
  297. // our storage. The simplest way to do this is to revert it to Normal status.
  298. if (m_slowArguments && !m_activation) {
  299. for (size_t i = 0; i < m_numArguments; ++i) {
  300. if (m_slowArguments[i].status != SlowArgument::Captured)
  301. continue;
  302. m_slowArguments[i].status = SlowArgument::Normal;
  303. m_slowArguments[i].index = CallFrame::argumentOffset(i);
  304. }
  305. }
  306. if (!callFrame->isInlineCallFrame()) {
  307. for (size_t i = 0; i < m_numArguments; ++i)
  308. trySetArgument(callFrame->vm(), i, callFrame->argumentAfterCapture(i));
  309. return;
  310. }
  311. tearOffForInlineCallFrame(
  312. callFrame->vm(), callFrame->registers(), callFrame->inlineCallFrame());
  313. }
  314. void Arguments::didTearOffActivation(ExecState* exec, JSActivation* activation)
  315. {
  316. RELEASE_ASSERT(activation);
  317. if (isTornOff())
  318. return;
  319. if (!m_numArguments)
  320. return;
  321. m_activation.set(exec->vm(), this, activation);
  322. tearOff(exec);
  323. }
  324. void Arguments::tearOff(CallFrame* callFrame, InlineCallFrame* inlineCallFrame)
  325. {
  326. if (isTornOff())
  327. return;
  328. if (!m_numArguments)
  329. return;
  330. m_registerArray = adoptArrayPtr(new WriteBarrier<Unknown>[m_numArguments]);
  331. m_registers = m_registerArray.get() + CallFrame::offsetFor(m_numArguments + 1);
  332. tearOffForInlineCallFrame(
  333. callFrame->vm(), callFrame->registers() + inlineCallFrame->stackOffset,
  334. inlineCallFrame);
  335. }
  336. void Arguments::tearOffForInlineCallFrame(VM& vm, Register* registers, InlineCallFrame* inlineCallFrame)
  337. {
  338. for (size_t i = 0; i < m_numArguments; ++i) {
  339. ValueRecovery& recovery = inlineCallFrame->arguments[i + 1];
  340. // In the future we'll support displaced recoveries (indicating that the
  341. // argument was flushed to a different location), but for now we don't do
  342. // that so this code will fail if that were to happen. On the other hand,
  343. // it's much less likely that we'll support in-register recoveries since
  344. // this code does not (easily) have access to registers.
  345. JSValue value;
  346. Register* location = &registers[CallFrame::argumentOffset(i)];
  347. switch (recovery.technique()) {
  348. case AlreadyInJSStack:
  349. value = location->jsValue();
  350. break;
  351. case AlreadyInJSStackAsUnboxedInt32:
  352. value = jsNumber(location->unboxedInt32());
  353. break;
  354. case AlreadyInJSStackAsUnboxedCell:
  355. value = location->unboxedCell();
  356. break;
  357. case AlreadyInJSStackAsUnboxedBoolean:
  358. value = jsBoolean(location->unboxedBoolean());
  359. break;
  360. case AlreadyInJSStackAsUnboxedDouble:
  361. #if USE(JSVALUE64)
  362. value = jsNumber(*bitwise_cast<double*>(location));
  363. #else
  364. value = location->jsValue();
  365. #endif
  366. break;
  367. case Constant:
  368. value = recovery.constant();
  369. break;
  370. default:
  371. RELEASE_ASSERT_NOT_REACHED();
  372. break;
  373. }
  374. trySetArgument(vm, i, value);
  375. }
  376. }
  377. } // namespace JSC