Arguments.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  4. * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
  5. * Copyright (C) 2007 Maks Orlovich
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public License
  18. * along with this library; see the file COPYING.LIB. If not, write to
  19. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. *
  22. */
  23. #ifndef Arguments_h
  24. #define Arguments_h
  25. #include "CodeOrigin.h"
  26. #include "JSActivation.h"
  27. #include "JSDestructibleObject.h"
  28. #include "JSFunction.h"
  29. #include "JSGlobalObject.h"
  30. #include "Interpreter.h"
  31. #include "ObjectConstructor.h"
  32. namespace JSC {
  33. class Arguments : public JSDestructibleObject {
  34. friend class JIT;
  35. friend class DFG::SpeculativeJIT;
  36. public:
  37. typedef JSDestructibleObject Base;
  38. static Arguments* create(VM& vm, CallFrame* callFrame)
  39. {
  40. Arguments* arguments = new (NotNull, allocateCell<Arguments>(vm.heap)) Arguments(callFrame);
  41. arguments->finishCreation(callFrame);
  42. return arguments;
  43. }
  44. static Arguments* create(VM& vm, CallFrame* callFrame, InlineCallFrame* inlineCallFrame)
  45. {
  46. Arguments* arguments = new (NotNull, allocateCell<Arguments>(vm.heap)) Arguments(callFrame);
  47. arguments->finishCreation(callFrame, inlineCallFrame);
  48. return arguments;
  49. }
  50. enum { MaxArguments = 0x10000 };
  51. private:
  52. enum NoParametersType { NoParameters };
  53. Arguments(CallFrame*);
  54. Arguments(CallFrame*, NoParametersType);
  55. void tearOffForInlineCallFrame(VM& vm, Register*, InlineCallFrame*);
  56. #if ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT
  57. private:
  58. static const ClassInfo s_info;
  59. #else
  60. public:
  61. static const ClassInfo s_info;
  62. #endif
  63. static void visitChildren(JSCell*, SlotVisitor&);
  64. void fillArgList(ExecState*, MarkedArgumentBuffer&);
  65. uint32_t length(ExecState* exec) const
  66. {
  67. if (UNLIKELY(m_overrodeLength))
  68. return get(exec, exec->propertyNames().length).toUInt32(exec);
  69. return m_numArguments;
  70. }
  71. void copyToArguments(ExecState*, CallFrame*, uint32_t length);
  72. void tearOff(CallFrame*);
  73. void tearOff(CallFrame*, InlineCallFrame*);
  74. bool isTornOff() const { return m_registerArray; }
  75. void didTearOffActivation(ExecState*, JSActivation*);
  76. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  77. {
  78. return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
  79. }
  80. protected:
  81. static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesVisitChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
  82. void finishCreation(CallFrame*);
  83. void finishCreation(CallFrame*, InlineCallFrame*);
  84. private:
  85. static void destroy(JSCell*);
  86. static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
  87. static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
  88. static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
  89. static void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  90. static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
  91. static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
  92. static bool deleteProperty(JSCell*, ExecState*, PropertyName);
  93. static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
  94. static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
  95. void createStrictModeCallerIfNecessary(ExecState*);
  96. void createStrictModeCalleeIfNecessary(ExecState*);
  97. bool isArgument(size_t);
  98. bool trySetArgument(VM&, size_t argument, JSValue);
  99. JSValue tryGetArgument(size_t argument);
  100. bool isDeletedArgument(size_t);
  101. bool tryDeleteArgument(size_t);
  102. WriteBarrierBase<Unknown>& argument(size_t);
  103. void allocateSlowArguments();
  104. void init(CallFrame*);
  105. WriteBarrier<JSActivation> m_activation;
  106. unsigned m_numArguments;
  107. // We make these full byte booleans to make them easy to test from the JIT,
  108. // and because even if they were single-bit booleans we still wouldn't save
  109. // any space.
  110. bool m_overrodeLength;
  111. bool m_overrodeCallee;
  112. bool m_overrodeCaller;
  113. bool m_isStrictMode;
  114. WriteBarrierBase<Unknown>* m_registers;
  115. OwnArrayPtr<WriteBarrier<Unknown> > m_registerArray;
  116. OwnArrayPtr<SlowArgument> m_slowArguments;
  117. WriteBarrier<JSFunction> m_callee;
  118. };
  119. Arguments* asArguments(JSValue);
  120. inline Arguments* asArguments(JSValue value)
  121. {
  122. #if !BUILDING_DETACHED_JIT
  123. ASSERT(asObject(value)->inherits(&Arguments::s_info));
  124. #endif
  125. return static_cast<Arguments*>(asObject(value));
  126. }
  127. inline Arguments::Arguments(CallFrame* callFrame)
  128. : JSDestructibleObject(callFrame->vm(), callFrame->lexicalGlobalObject()->argumentsStructure())
  129. {
  130. }
  131. inline Arguments::Arguments(CallFrame* callFrame, NoParametersType)
  132. : JSDestructibleObject(callFrame->vm(), callFrame->lexicalGlobalObject()->argumentsStructure())
  133. {
  134. }
  135. inline void Arguments::allocateSlowArguments()
  136. {
  137. if (m_slowArguments)
  138. return;
  139. m_slowArguments = adoptArrayPtr(new SlowArgument[m_numArguments]);
  140. for (size_t i = 0; i < m_numArguments; ++i) {
  141. ASSERT(m_slowArguments[i].status == SlowArgument::Normal);
  142. m_slowArguments[i].index = CallFrame::argumentOffset(i);
  143. }
  144. }
  145. inline bool Arguments::tryDeleteArgument(size_t argument)
  146. {
  147. if (!isArgument(argument))
  148. return false;
  149. allocateSlowArguments();
  150. m_slowArguments[argument].status = SlowArgument::Deleted;
  151. return true;
  152. }
  153. inline bool Arguments::trySetArgument(VM& vm, size_t argument, JSValue value)
  154. {
  155. if (!isArgument(argument))
  156. return false;
  157. this->argument(argument).set(vm, this, value);
  158. return true;
  159. }
  160. inline JSValue Arguments::tryGetArgument(size_t argument)
  161. {
  162. if (!isArgument(argument))
  163. return JSValue();
  164. return this->argument(argument).get();
  165. }
  166. inline bool Arguments::isDeletedArgument(size_t argument)
  167. {
  168. if (argument >= m_numArguments)
  169. return false;
  170. if (!m_slowArguments)
  171. return false;
  172. if (m_slowArguments[argument].status != SlowArgument::Deleted)
  173. return false;
  174. return true;
  175. }
  176. inline bool Arguments::isArgument(size_t argument)
  177. {
  178. if (argument >= m_numArguments)
  179. return false;
  180. if (m_slowArguments && m_slowArguments[argument].status == SlowArgument::Deleted)
  181. return false;
  182. return true;
  183. }
  184. inline WriteBarrierBase<Unknown>& Arguments::argument(size_t argument)
  185. {
  186. ASSERT(isArgument(argument));
  187. if (!m_slowArguments)
  188. return m_registers[CallFrame::argumentOffset(argument)];
  189. int index = m_slowArguments[argument].index;
  190. if (!m_activation || m_slowArguments[argument].status != SlowArgument::Captured)
  191. return m_registers[index];
  192. return m_activation->registerAt(index);
  193. }
  194. inline void Arguments::finishCreation(CallFrame* callFrame)
  195. {
  196. Base::finishCreation(callFrame->vm());
  197. ASSERT(inherits(&s_info));
  198. JSFunction* callee = jsCast<JSFunction*>(callFrame->callee());
  199. m_numArguments = callFrame->argumentCount();
  200. m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers());
  201. m_callee.set(callFrame->vm(), this, callee);
  202. m_overrodeLength = false;
  203. m_overrodeCallee = false;
  204. m_overrodeCaller = false;
  205. m_isStrictMode = callFrame->codeBlock()->isStrictMode();
  206. SharedSymbolTable* symbolTable = callFrame->codeBlock()->symbolTable();
  207. const SlowArgument* slowArguments = symbolTable->slowArguments();
  208. if (slowArguments) {
  209. allocateSlowArguments();
  210. size_t count = std::min<unsigned>(m_numArguments, symbolTable->parameterCount());
  211. for (size_t i = 0; i < count; ++i)
  212. m_slowArguments[i] = slowArguments[i];
  213. }
  214. // The bytecode generator omits op_tear_off_activation in cases of no
  215. // declared parameters, so we need to tear off immediately.
  216. if (m_isStrictMode || !callee->jsExecutable()->parameterCount())
  217. tearOff(callFrame);
  218. }
  219. inline void Arguments::finishCreation(CallFrame* callFrame, InlineCallFrame* inlineCallFrame)
  220. {
  221. Base::finishCreation(callFrame->vm());
  222. ASSERT(inherits(&s_info));
  223. JSFunction* callee = inlineCallFrame->calleeForCallFrame(callFrame);
  224. m_numArguments = inlineCallFrame->arguments.size() - 1;
  225. m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers()) + inlineCallFrame->stackOffset;
  226. m_callee.set(callFrame->vm(), this, callee);
  227. m_overrodeLength = false;
  228. m_overrodeCallee = false;
  229. m_overrodeCaller = false;
  230. m_isStrictMode = jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->isStrictMode();
  231. ASSERT(!jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->symbolTable(inlineCallFrame->isCall ? CodeForCall : CodeForConstruct)->slowArguments());
  232. // The bytecode generator omits op_tear_off_activation in cases of no
  233. // declared parameters, so we need to tear off immediately.
  234. if (m_isStrictMode || !callee->jsExecutable()->parameterCount())
  235. tearOff(callFrame, inlineCallFrame);
  236. }
  237. } // namespace JSC
  238. #endif // Arguments_h