Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_DEBUG_DEBUG_SCOPES_H_
6 : #define V8_DEBUG_DEBUG_SCOPES_H_
7 :
8 : #include <vector>
9 :
10 : #include "src/debug/debug-frames.h"
11 : #include "src/frames.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 : class ParseInfo;
17 :
18 : // Iterate over the actual scopes visible from a stack frame or from a closure.
19 : // The iteration proceeds from the innermost visible nested scope outwards.
20 : // All scopes are backed by an actual context except the local scope,
21 : // which is inserted "artificially" in the context chain.
22 2760 : class ScopeIterator {
23 : public:
24 : enum ScopeType {
25 : ScopeTypeGlobal = 0,
26 : ScopeTypeLocal,
27 : ScopeTypeWith,
28 : ScopeTypeClosure,
29 : ScopeTypeCatch,
30 : ScopeTypeBlock,
31 : ScopeTypeScript,
32 : ScopeTypeEval,
33 : ScopeTypeModule
34 : };
35 :
36 : static const int kScopeDetailsTypeIndex = 0;
37 : static const int kScopeDetailsObjectIndex = 1;
38 : static const int kScopeDetailsNameIndex = 2;
39 : static const int kScopeDetailsStartPositionIndex = 3;
40 : static const int kScopeDetailsEndPositionIndex = 4;
41 : static const int kScopeDetailsFunctionIndex = 5;
42 : static const int kScopeDetailsSize = 6;
43 :
44 : enum Option { DEFAULT, IGNORE_NESTED_SCOPES, COLLECT_NON_LOCALS };
45 :
46 : ScopeIterator(Isolate* isolate, FrameInspector* frame_inspector,
47 : Option options = DEFAULT);
48 :
49 : ScopeIterator(Isolate* isolate, Handle<JSFunction> function);
50 : ScopeIterator(Isolate* isolate, Handle<JSGeneratorObject> generator);
51 :
52 : MUST_USE_RESULT MaybeHandle<JSObject> MaterializeScopeDetails();
53 :
54 : // More scopes?
55 6540 : bool Done() { return context_.is_null(); }
56 :
57 : // Move to the next scope.
58 : void Next();
59 :
60 : // Return the type of the current scope.
61 : ScopeType Type();
62 :
63 : // Return the JavaScript object with the content of the current scope.
64 : MaybeHandle<JSObject> ScopeObject();
65 :
66 : bool HasContext();
67 :
68 : // Set variable value and return true on success.
69 : bool SetVariableValue(Handle<String> variable_name, Handle<Object> new_value);
70 :
71 : Handle<ScopeInfo> CurrentScopeInfo();
72 :
73 : // Return the context for this scope. For the local context there might not
74 : // be an actual context.
75 : Handle<Context> CurrentContext();
76 :
77 : // Populate the set with collected non-local variable names.
78 : Handle<StringSet> GetNonLocals();
79 :
80 : // Return function which represents closure for current scope.
81 : Handle<JSFunction> GetClosure();
82 : int start_position();
83 : int end_position();
84 :
85 : #ifdef DEBUG
86 : // Debug print of the content of the current scope.
87 : void DebugPrint();
88 : #endif
89 :
90 : private:
91 : struct ExtendedScopeInfo {
92 : ExtendedScopeInfo(Handle<ScopeInfo> info, int start, int end)
93 140563 : : scope_info(info), start_position(start), end_position(end) {}
94 : explicit ExtendedScopeInfo(Handle<ScopeInfo> info)
95 1457 : : scope_info(info), start_position(-1), end_position(-1) {}
96 : Handle<ScopeInfo> scope_info;
97 : int start_position;
98 : int end_position;
99 7376 : bool is_hidden() { return start_position == -1 && end_position == -1; }
100 : };
101 :
102 : Isolate* isolate_;
103 : FrameInspector* const frame_inspector_ = nullptr;
104 : Handle<JSGeneratorObject> generator_;
105 : Handle<Context> context_;
106 : std::vector<ExtendedScopeInfo> nested_scope_chain_;
107 : Handle<StringSet> non_locals_;
108 : bool seen_script_scope_;
109 :
110 : inline JavaScriptFrame* GetFrame() {
111 : return frame_inspector_->GetArgumentsFrame();
112 : }
113 :
114 : Handle<Context> GetContext();
115 : Handle<JSFunction> GetFunction();
116 : int GetSourcePosition();
117 :
118 : void MaterializeStackLocals(Handle<JSObject> local_scope,
119 : Handle<ScopeInfo> scope_info);
120 :
121 : void TryParseAndRetrieveScopes(ScopeIterator::Option option);
122 :
123 : void RetrieveScopeChain(DeclarationScope* scope);
124 :
125 : void CollectNonLocals(ParseInfo* info, DeclarationScope* scope);
126 :
127 : void UnwrapEvaluationContext();
128 :
129 : MUST_USE_RESULT MaybeHandle<JSObject> MaterializeScriptScope();
130 : MUST_USE_RESULT MaybeHandle<JSObject> MaterializeLocalScope();
131 : MUST_USE_RESULT MaybeHandle<JSObject> MaterializeModuleScope();
132 : Handle<JSObject> MaterializeClosure();
133 : Handle<JSObject> MaterializeCatchScope();
134 : Handle<JSObject> MaterializeInnerScope();
135 : Handle<JSObject> WithContextExtension();
136 :
137 : bool SetLocalVariableValue(Handle<String> variable_name,
138 : Handle<Object> new_value);
139 : bool SetInnerScopeVariableValue(Handle<String> variable_name,
140 : Handle<Object> new_value);
141 : bool SetClosureVariableValue(Handle<String> variable_name,
142 : Handle<Object> new_value);
143 : bool SetScriptVariableValue(Handle<String> variable_name,
144 : Handle<Object> new_value);
145 : bool SetCatchVariableValue(Handle<String> variable_name,
146 : Handle<Object> new_value);
147 : bool SetModuleVariableValue(Handle<String> variable_name,
148 : Handle<Object> new_value);
149 :
150 : // Helper functions.
151 : bool SetParameterValue(Handle<ScopeInfo> scope_info,
152 : Handle<String> parameter_name,
153 : Handle<Object> new_value);
154 : bool SetStackVariableValue(Handle<ScopeInfo> scope_info,
155 : Handle<String> variable_name,
156 : Handle<Object> new_value);
157 : bool SetContextVariableValue(Handle<ScopeInfo> scope_info,
158 : Handle<Context> context,
159 : Handle<String> variable_name,
160 : Handle<Object> new_value);
161 :
162 : void CopyContextLocalsToScopeObject(Handle<ScopeInfo> scope_info,
163 : Handle<Context> context,
164 : Handle<JSObject> scope_object);
165 : void CopyModuleVarsToScopeObject(Handle<ScopeInfo> scope_info,
166 : Handle<Context> context,
167 : Handle<JSObject> scope_object);
168 : void CopyContextExtensionToScopeObject(Handle<Context> context,
169 : Handle<JSObject> scope_object,
170 : KeyCollectionMode mode);
171 :
172 : // Get the chain of nested scopes within this scope for the source statement
173 : // position. The scopes will be added to the list from the outermost scope to
174 : // the innermost scope. Only nested block, catch or with scopes are tracked
175 : // and will be returned, but no inner function scopes.
176 : void GetNestedScopeChain(Isolate* isolate, Scope* scope,
177 : int statement_position);
178 :
179 : bool HasNestedScopeChain();
180 : ExtendedScopeInfo& LastNestedScopeChain();
181 :
182 : DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator);
183 : };
184 :
185 : } // namespace internal
186 : } // namespace v8
187 :
188 : #endif // V8_DEBUG_DEBUG_SCOPES_H_
|