JSScope.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2012 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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef JSScope_h
  26. #define JSScope_h
  27. #include "JSObject.h"
  28. #include "ResolveOperation.h"
  29. namespace JSC {
  30. class ScopeChainIterator;
  31. class JSScope : public JSNonFinalObject {
  32. public:
  33. typedef JSNonFinalObject Base;
  34. friend class LLIntOffsetsExtractor;
  35. static size_t offsetOfNext();
  36. JS_EXPORT_PRIVATE static JSObject* objectAtScope(JSScope*);
  37. static JSValue resolve(CallFrame*, const Identifier&, ResolveOperations*);
  38. static JSValue resolveBase(CallFrame*, const Identifier&, bool isStrict, ResolveOperations*, PutToBaseOperation*);
  39. static JSValue resolveWithBase(CallFrame*, const Identifier&, Register* base, ResolveOperations*, PutToBaseOperation*);
  40. static JSValue resolveWithThis(CallFrame*, const Identifier&, Register* base, ResolveOperations*);
  41. static JSValue resolveGlobal(CallFrame*, const Identifier&, JSGlobalObject*, ResolveOperation*);
  42. static void resolvePut(CallFrame*, JSValue base, const Identifier&, JSValue, PutToBaseOperation*);
  43. static void visitChildren(JSCell*, SlotVisitor&);
  44. bool isDynamicScope(bool& requiresDynamicChecks) const;
  45. ScopeChainIterator begin();
  46. ScopeChainIterator end();
  47. JSScope* next();
  48. int localDepth();
  49. JSGlobalObject* globalObject();
  50. VM* vm();
  51. JSObject* globalThis();
  52. protected:
  53. JSScope(VM&, Structure*, JSScope* next);
  54. static const unsigned StructureFlags = OverridesVisitChildren | Base::StructureFlags;
  55. private:
  56. WriteBarrier<JSScope> m_next;
  57. enum ReturnValues {
  58. ReturnValue = 1,
  59. ReturnBase = 2,
  60. ReturnThis = 4,
  61. ReturnBaseAndValue = ReturnValue | ReturnBase,
  62. ReturnThisAndValue = ReturnValue | ReturnThis,
  63. };
  64. enum LookupMode { UnknownResolve, KnownResolve };
  65. template <LookupMode, ReturnValues> static JSObject* resolveContainingScopeInternal(CallFrame*, const Identifier&, PropertySlot&, ResolveOperations*, PutToBaseOperation*, bool isStrict);
  66. template <ReturnValues> static JSObject* resolveContainingScope(CallFrame*, const Identifier&, PropertySlot&, ResolveOperations*, PutToBaseOperation*, bool isStrict);
  67. };
  68. inline JSScope::JSScope(VM& vm, Structure* structure, JSScope* next)
  69. : Base(vm, structure)
  70. , m_next(vm, this, next, WriteBarrier<JSScope>::MayBeNull)
  71. {
  72. }
  73. class ScopeChainIterator {
  74. public:
  75. ScopeChainIterator(JSScope* node)
  76. : m_node(node)
  77. {
  78. }
  79. JSObject* get() const { return JSScope::objectAtScope(m_node); }
  80. JSObject* operator->() const { return JSScope::objectAtScope(m_node); }
  81. ScopeChainIterator& operator++() { m_node = m_node->next(); return *this; }
  82. // postfix ++ intentionally omitted
  83. bool operator==(const ScopeChainIterator& other) const { return m_node == other.m_node; }
  84. bool operator!=(const ScopeChainIterator& other) const { return m_node != other.m_node; }
  85. private:
  86. JSScope* m_node;
  87. };
  88. inline ScopeChainIterator JSScope::begin()
  89. {
  90. return ScopeChainIterator(this);
  91. }
  92. inline ScopeChainIterator JSScope::end()
  93. {
  94. return ScopeChainIterator(0);
  95. }
  96. inline JSScope* JSScope::next()
  97. {
  98. return m_next.get();
  99. }
  100. inline JSGlobalObject* JSScope::globalObject()
  101. {
  102. return structure()->globalObject();
  103. }
  104. inline VM* JSScope::vm()
  105. {
  106. return Heap::heap(this)->vm();
  107. }
  108. inline Register& Register::operator=(JSScope* scope)
  109. {
  110. *this = JSValue(scope);
  111. return *this;
  112. }
  113. inline JSScope* Register::scope() const
  114. {
  115. return jsCast<JSScope*>(jsValue());
  116. }
  117. inline VM& ExecState::vm() const
  118. {
  119. ASSERT(scope()->vm());
  120. return *scope()->vm();
  121. }
  122. inline JSGlobalObject* ExecState::lexicalGlobalObject() const
  123. {
  124. return scope()->globalObject();
  125. }
  126. inline JSObject* ExecState::globalThisValue() const
  127. {
  128. return scope()->globalThis();
  129. }
  130. inline size_t JSScope::offsetOfNext()
  131. {
  132. return OBJECT_OFFSETOF(JSScope, m_next);
  133. }
  134. } // namespace JSC
  135. #endif // JSScope_h