v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
debug-scopes.h
Go to the documentation of this file.
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
10
11namespace v8 {
12namespace internal {
13
14class JavaScriptFrame;
15class ParseInfo;
16
17// Iterate over the actual scopes visible from a stack frame or from a closure.
18// The iteration proceeds from the innermost visible nested scope outwards.
19// All scopes are backed by an actual context except the local scope,
20// which is inserted "artificially" in the context chain.
22 public:
34
35 static const int kScopeDetailsTypeIndex = 0;
36 static const int kScopeDetailsObjectIndex = 1;
37 static const int kScopeDetailsNameIndex = 2;
38 static const int kScopeDetailsStartPositionIndex = 3;
39 static const int kScopeDetailsEndPositionIndex = 4;
40 static const int kScopeDetailsFunctionIndex = 5;
41 static const int kScopeDetailsSize = 6;
42
43 enum class ReparseStrategy {
44 kFunctionLiteral,
45 // Checks whether the paused function (and its scope chain) already has
46 // its blocklist calculated and re-parses the whole script if not.
47 // Otherwise only the function literal is re-parsed.
48 kScriptIfNeeded,
49 };
50
51 ScopeIterator(Isolate* isolate, FrameInspector* frame_inspector,
52 ReparseStrategy strategy);
53
57
58 DirectHandle<JSObject> MaterializeScopeDetails();
59
60 // More scopes?
61 bool Done() const { return context_.is_null(); }
62
63 // Move to the next scope.
64 void Next();
65
66 // Restart to the first scope and context.
67 void Restart();
68
69 // Return the type of the current scope.
70 ScopeType Type() const;
71
72 // Indicates which variables should be visited. Either only variables from the
73 // scope that are available on the stack, or all variables.
74 enum class Mode { STACK, ALL };
75
76 // Return the JavaScript object with the content of the current scope.
77 Handle<JSObject> ScopeObject(Mode mode);
78
79 // Returns whether the current scope declares any variables.
80 bool DeclaresLocals(Mode mode) const;
81
82 // Set variable value and return true on success.
83 bool SetVariableValue(Handle<String> variable_name,
84 DirectHandle<Object> new_value);
85
86 bool ClosureScopeHasThisReference() const;
87
88 // Populate the set with collected non-local variable names.
90
91 // Similar to JSFunction::GetName return the function's name or it's inferred
92 // name.
93 DirectHandle<Object> GetFunctionDebugName() const;
94
96
97 bool HasPositionInfo();
98 int start_position();
99 int end_position();
100
101#ifdef DEBUG
102 // Debug print of the content of the current scope.
103 void DebugPrint();
104#endif
105
106 bool InInnerScope() const { return !function_.is_null(); }
107 bool HasContext() const;
108 bool NeedsContext() const;
110 DCHECK(HasContext());
111 return context_;
112 }
113
114 private:
116 std::unique_ptr<ReusableUnoptimizedCompileState> reusable_compile_state_;
117 std::unique_ptr<ParseInfo> info_;
118 FrameInspector* const frame_inspector_ = nullptr;
120
121 // The currently-executing function from the inspected frame, or null if this
122 // ScopeIterator has already iterated to any Scope outside that function.
124
129 Scope* start_scope_ = nullptr;
130 Scope* current_scope_ = nullptr;
131 bool seen_script_scope_ = false;
132 bool calculate_blocklists_ = false;
133
134 inline JavaScriptFrame* GetFrame() const {
135 return frame_inspector_->javascript_frame();
136 }
137
138 bool AdvanceOneScope();
139 void AdvanceOneContext();
140 void AdvanceScope();
141 void AdvanceContext();
142 void CollectLocalsFromCurrentScope();
143
144 // Calculates all the block list starting at the current scope and stores
145 // them in the global "LocalsBlocklistCache".
146 //
147 // Is a no-op unless `calculate_blocklists_` is true and
148 // current_scope_ == closure_scope_. Otherwise `context_` does not match
149 // with current_scope_/closure_scope_.
150 void MaybeCollectAndStoreLocalBlocklists() const;
151
152 int GetSourcePosition() const;
153
154 void TryParseAndRetrieveScopes(ReparseStrategy strategy);
155
156 void UnwrapEvaluationContext();
157
158 using Visitor = std::function<bool(Handle<String> name, Handle<Object> value,
159 ScopeType scope_type)>;
160
161 Handle<JSObject> WithContextExtension();
162
163 bool SetLocalVariableValue(DirectHandle<String> variable_name,
164 DirectHandle<Object> new_value);
165 bool SetContextVariableValue(DirectHandle<String> variable_name,
166 DirectHandle<Object> new_value);
167 bool SetContextExtensionValue(DirectHandle<String> variable_name,
168 DirectHandle<Object> new_value);
169 bool SetScriptVariableValue(DirectHandle<String> variable_name,
170 DirectHandle<Object> new_value);
171 bool SetModuleVariableValue(DirectHandle<String> variable_name,
172 DirectHandle<Object> new_value);
173
174 // Helper functions.
175 void VisitScope(const Visitor& visitor, Mode mode) const;
176 void VisitLocalScope(const Visitor& visitor, Mode mode,
177 ScopeType scope_type) const;
178 void VisitScriptScope(const Visitor& visitor) const;
179 void VisitModuleScope(const Visitor& visitor) const;
180 bool VisitLocals(const Visitor& visitor, Mode mode,
181 ScopeType scope_type) const;
182 bool VisitContextLocals(const Visitor& visitor,
183 DirectHandle<ScopeInfo> scope_info,
184 DirectHandle<Context> context,
185 ScopeType scope_type) const;
186
188};
189
190} // namespace internal
191} // namespace v8
192
193#endif // V8_DEBUG_DEBUG_SCOPES_H_
JavaScriptFrame * javascript_frame()
std::unique_ptr< ReusableUnoptimizedCompileState > reusable_compile_state_
JavaScriptFrame * GetFrame() const
DirectHandle< Script > GetScript() const
Handle< StringSet > locals_
Handle< Context > CurrentContext() const
Handle< Context > context_
std::function< bool(Handle< String > name, Handle< Object > value, ScopeType scope_type)> Visitor
std::unique_ptr< ParseInfo > info_
Handle< JSFunction > function_
DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator)
Handle< JSGeneratorObject > generator_
DirectHandle< StringSet > GetLocals()
const JSFunctionRef function_
Scope * start_scope_
Handle< Context > context_
DeclarationScope * closure_scope_
Handle< Script > script_
std::vector< ValueType > locals_
#define DCHECK(condition)
Definition logging.h:482
#define V8_EXPORT_PRIVATE
Definition macros.h:460