| 1 | /* |
| 2 | * Copyright (C) 2012-2016 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 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "GetPutInfo.h" |
| 29 | #include "JSObject.h" |
| 30 | |
| 31 | namespace JSC { |
| 32 | |
| 33 | class ScopeChainIterator; |
| 34 | class SymbolTable; |
| 35 | class VariableEnvironment; |
| 36 | class WatchpointSet; |
| 37 | |
| 38 | class JSScope : public JSNonFinalObject { |
| 39 | public: |
| 40 | using Base = JSNonFinalObject; |
| 41 | static const unsigned StructureFlags = Base::StructureFlags | OverridesToThis; |
| 42 | |
| 43 | DECLARE_EXPORT_INFO; |
| 44 | |
| 45 | friend class LLIntOffsetsExtractor; |
| 46 | static size_t offsetOfNext(); |
| 47 | |
| 48 | static JSObject* objectAtScope(JSScope*); |
| 49 | |
| 50 | static JSObject* resolve(ExecState*, JSScope*, const Identifier&); |
| 51 | static JSValue resolveScopeForHoistingFuncDeclInEval(ExecState*, JSScope*, const Identifier&); |
| 52 | static ResolveOp abstractResolve(ExecState*, size_t depthOffset, JSScope*, const Identifier&, GetOrPut, ResolveType, InitializationMode); |
| 53 | |
| 54 | static bool hasConstantScope(ResolveType); |
| 55 | static JSScope* constantScopeForCodeBlock(ResolveType, CodeBlock*); |
| 56 | |
| 57 | static void collectClosureVariablesUnderTDZ(JSScope*, VariableEnvironment& result); |
| 58 | |
| 59 | static void visitChildren(JSCell*, SlotVisitor&); |
| 60 | |
| 61 | bool isVarScope(); |
| 62 | bool isLexicalScope(); |
| 63 | bool isModuleScope(); |
| 64 | bool isCatchScope(); |
| 65 | bool isFunctionNameScopeObject(); |
| 66 | |
| 67 | bool isNestedLexicalScope(); |
| 68 | |
| 69 | ScopeChainIterator begin(); |
| 70 | ScopeChainIterator end(); |
| 71 | JSScope* next(); |
| 72 | |
| 73 | JSObject* globalThis(); |
| 74 | |
| 75 | SymbolTable* symbolTable(VM&); |
| 76 | |
| 77 | JS_EXPORT_PRIVATE static JSValue toThis(JSCell*, ExecState*, ECMAMode); |
| 78 | |
| 79 | protected: |
| 80 | JSScope(VM&, Structure*, JSScope* next); |
| 81 | |
| 82 | template<typename ReturnPredicateFunctor, typename SkipPredicateFunctor> |
| 83 | static JSObject* resolve(ExecState*, JSScope*, const Identifier&, ReturnPredicateFunctor, SkipPredicateFunctor); |
| 84 | |
| 85 | private: |
| 86 | WriteBarrier<JSScope> m_next; |
| 87 | }; |
| 88 | |
| 89 | inline JSScope::JSScope(VM& vm, Structure* structure, JSScope* next) |
| 90 | : Base(vm, structure) |
| 91 | , m_next(vm, this, next, WriteBarrier<JSScope>::MayBeNull) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | class ScopeChainIterator { |
| 96 | public: |
| 97 | ScopeChainIterator(JSScope* node) |
| 98 | : m_node(node) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | JSObject* get() const { return JSScope::objectAtScope(m_node); } |
| 103 | JSObject* operator->() const { return JSScope::objectAtScope(m_node); } |
| 104 | JSScope* scope() const { return m_node; } |
| 105 | |
| 106 | ScopeChainIterator& operator++() { m_node = m_node->next(); return *this; } |
| 107 | |
| 108 | // postfix ++ intentionally omitted |
| 109 | |
| 110 | bool operator==(const ScopeChainIterator& other) const { return m_node == other.m_node; } |
| 111 | bool operator!=(const ScopeChainIterator& other) const { return m_node != other.m_node; } |
| 112 | |
| 113 | private: |
| 114 | JSScope* m_node; |
| 115 | }; |
| 116 | |
| 117 | inline ScopeChainIterator JSScope::begin() |
| 118 | { |
| 119 | return ScopeChainIterator(this); |
| 120 | } |
| 121 | |
| 122 | inline ScopeChainIterator JSScope::end() |
| 123 | { |
| 124 | return ScopeChainIterator(0); |
| 125 | } |
| 126 | |
| 127 | inline JSScope* JSScope::next() |
| 128 | { |
| 129 | return m_next.get(); |
| 130 | } |
| 131 | |
| 132 | inline Register& Register::operator=(JSScope* scope) |
| 133 | { |
| 134 | *this = JSValue(scope); |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | inline JSScope* Register::scope() const |
| 139 | { |
| 140 | return jsCast<JSScope*>(unboxedCell()); |
| 141 | } |
| 142 | |
| 143 | inline JSGlobalObject* ExecState::lexicalGlobalObject() const |
| 144 | { |
| 145 | return jsCallee()->globalObject(); |
| 146 | } |
| 147 | |
| 148 | inline size_t JSScope::offsetOfNext() |
| 149 | { |
| 150 | return OBJECT_OFFSETOF(JSScope, m_next); |
| 151 | } |
| 152 | |
| 153 | } // namespace JSC |
| 154 | |