JSActivation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include "JSActivation.h"
  30. #include "Arguments.h"
  31. #include "Interpreter.h"
  32. #include "JSFunction.h"
  33. #include "Operations.h"
  34. using namespace std;
  35. namespace JSC {
  36. const ClassInfo JSActivation::s_info = { "JSActivation", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSActivation) };
  37. void JSActivation::visitChildren(JSCell* cell, SlotVisitor& visitor)
  38. {
  39. JSActivation* thisObject = jsCast<JSActivation*>(cell);
  40. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  41. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  42. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  43. Base::visitChildren(thisObject, visitor);
  44. // No need to mark our registers if they're still in the JSStack.
  45. if (!thisObject->isTornOff())
  46. return;
  47. for (int i = 0; i < thisObject->symbolTable()->captureCount(); ++i)
  48. visitor.append(&thisObject->storage()[i]);
  49. }
  50. inline bool JSActivation::symbolTableGet(PropertyName propertyName, PropertySlot& slot)
  51. {
  52. SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
  53. if (entry.isNull())
  54. return false;
  55. // Defend against the inspector asking for a var after it has been optimized out.
  56. if (isTornOff() && !isValid(entry))
  57. return false;
  58. slot.setValue(registerAt(entry.getIndex()).get());
  59. return true;
  60. }
  61. inline bool JSActivation::symbolTableGet(PropertyName propertyName, PropertyDescriptor& descriptor)
  62. {
  63. SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
  64. if (entry.isNull())
  65. return false;
  66. // Defend against the inspector asking for a var after it has been optimized out.
  67. if (isTornOff() && !isValid(entry))
  68. return false;
  69. descriptor.setDescriptor(registerAt(entry.getIndex()).get(), entry.getAttributes());
  70. return true;
  71. }
  72. inline bool JSActivation::symbolTablePut(ExecState* exec, PropertyName propertyName, JSValue value, bool shouldThrow)
  73. {
  74. VM& vm = exec->vm();
  75. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
  76. SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
  77. if (entry.isNull())
  78. return false;
  79. if (entry.isReadOnly()) {
  80. if (shouldThrow)
  81. throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
  82. return true;
  83. }
  84. // Defend against the inspector asking for a var after it has been optimized out.
  85. if (isTornOff() && !isValid(entry))
  86. return false;
  87. registerAt(entry.getIndex()).set(vm, this, value);
  88. return true;
  89. }
  90. void JSActivation::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
  91. {
  92. JSActivation* thisObject = jsCast<JSActivation*>(object);
  93. if (mode == IncludeDontEnumProperties && !thisObject->isTornOff())
  94. propertyNames.add(exec->propertyNames().arguments);
  95. SymbolTable::const_iterator end = thisObject->symbolTable()->end();
  96. for (SymbolTable::const_iterator it = thisObject->symbolTable()->begin(); it != end; ++it) {
  97. if (it->value.getAttributes() & DontEnum && mode != IncludeDontEnumProperties)
  98. continue;
  99. if (!thisObject->isValid(it->value))
  100. continue;
  101. propertyNames.add(Identifier(exec, it->key.get()));
  102. }
  103. // Skip the JSVariableObject implementation of getOwnNonIndexPropertyNames
  104. JSObject::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
  105. }
  106. inline bool JSActivation::symbolTablePutWithAttributes(VM& vm, PropertyName propertyName, JSValue value, unsigned attributes)
  107. {
  108. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
  109. SymbolTable::iterator iter = symbolTable()->find(propertyName.publicName());
  110. if (iter == symbolTable()->end())
  111. return false;
  112. SymbolTableEntry& entry = iter->value;
  113. ASSERT(!entry.isNull());
  114. if (!isValid(entry))
  115. return false;
  116. entry.setAttributes(attributes);
  117. registerAt(entry.getIndex()).set(vm, this, value);
  118. return true;
  119. }
  120. bool JSActivation::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  121. {
  122. JSActivation* thisObject = jsCast<JSActivation*>(cell);
  123. if (propertyName == exec->propertyNames().arguments) {
  124. // Defend against the inspector asking for the arguments object after it has been optimized out.
  125. if (!thisObject->isTornOff()) {
  126. slot.setCustom(thisObject, thisObject->getArgumentsGetter());
  127. return true;
  128. }
  129. }
  130. if (thisObject->symbolTableGet(propertyName, slot))
  131. return true;
  132. if (JSValue value = thisObject->getDirect(exec->vm(), propertyName)) {
  133. slot.setValue(value);
  134. return true;
  135. }
  136. // We don't call through to JSObject because there's no way to give an
  137. // activation object getter properties or a prototype.
  138. ASSERT(!thisObject->hasGetterSetterProperties());
  139. ASSERT(thisObject->prototype().isNull());
  140. return false;
  141. }
  142. bool JSActivation::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  143. {
  144. JSActivation* thisObject = jsCast<JSActivation*>(object);
  145. if (propertyName == exec->propertyNames().arguments) {
  146. // Defend against the inspector asking for the arguments object after it has been optimized out.
  147. if (!thisObject->isTornOff()) {
  148. PropertySlot slot;
  149. JSActivation::getOwnPropertySlot(thisObject, exec, propertyName, slot);
  150. descriptor.setDescriptor(slot.getValue(exec, propertyName), DontEnum);
  151. return true;
  152. }
  153. }
  154. if (thisObject->symbolTableGet(propertyName, descriptor))
  155. return true;
  156. return Base::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);
  157. }
  158. void JSActivation::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  159. {
  160. JSActivation* thisObject = jsCast<JSActivation*>(cell);
  161. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
  162. if (thisObject->symbolTablePut(exec, propertyName, value, slot.isStrictMode()))
  163. return;
  164. // We don't call through to JSObject because __proto__ and getter/setter
  165. // properties are non-standard extensions that other implementations do not
  166. // expose in the activation object.
  167. ASSERT(!thisObject->hasGetterSetterProperties());
  168. thisObject->putOwnDataProperty(exec->vm(), propertyName, value, slot);
  169. }
  170. // FIXME: Make this function honor ReadOnly (const) and DontEnum
  171. void JSActivation::putDirectVirtual(JSObject* object, ExecState* exec, PropertyName propertyName, JSValue value, unsigned attributes)
  172. {
  173. JSActivation* thisObject = jsCast<JSActivation*>(object);
  174. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
  175. if (thisObject->symbolTablePutWithAttributes(exec->vm(), propertyName, value, attributes))
  176. return;
  177. // We don't call through to JSObject because __proto__ and getter/setter
  178. // properties are non-standard extensions that other implementations do not
  179. // expose in the activation object.
  180. ASSERT(!thisObject->hasGetterSetterProperties());
  181. JSObject::putDirectVirtual(thisObject, exec, propertyName, value, attributes);
  182. }
  183. bool JSActivation::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
  184. {
  185. if (propertyName == exec->propertyNames().arguments)
  186. return false;
  187. return Base::deleteProperty(cell, exec, propertyName);
  188. }
  189. JSObject* JSActivation::toThisObject(JSCell*, ExecState* exec)
  190. {
  191. return exec->globalThisValue();
  192. }
  193. JSValue JSActivation::argumentsGetter(ExecState*, JSValue slotBase, PropertyName)
  194. {
  195. JSActivation* activation = jsCast<JSActivation*>(slotBase);
  196. if (activation->isTornOff())
  197. return jsUndefined();
  198. CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(activation->m_registers));
  199. int argumentsRegister = callFrame->codeBlock()->argumentsRegister();
  200. if (JSValue arguments = callFrame->uncheckedR(argumentsRegister).jsValue())
  201. return arguments;
  202. int realArgumentsRegister = unmodifiedArgumentsRegister(argumentsRegister);
  203. JSValue arguments = JSValue(Arguments::create(callFrame->vm(), callFrame));
  204. callFrame->uncheckedR(argumentsRegister) = arguments;
  205. callFrame->uncheckedR(realArgumentsRegister) = arguments;
  206. ASSERT(callFrame->uncheckedR(realArgumentsRegister).jsValue().inherits(&Arguments::s_info));
  207. return callFrame->uncheckedR(realArgumentsRegister).jsValue();
  208. }
  209. // These two functions serve the purpose of isolating the common case from a
  210. // PIC branch.
  211. PropertySlot::GetValueFunc JSActivation::getArgumentsGetter()
  212. {
  213. return argumentsGetter;
  214. }
  215. } // namespace JSC