v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
source-position.cc
Go to the documentation of this file.
1// Copyright 2016 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
6
10
11namespace v8 {
12namespace internal {
13
14std::ostream& operator<<(std::ostream& out, const SourcePositionInfo& pos) {
15 out << "<";
16 if (!pos.script.is_null() && IsString(pos.script->name())) {
17 out << Cast<String>(pos.script->name())->ToCString().get();
18 } else {
19 out << "unknown";
20 }
21 out << ":" << pos.line + 1 << ":" << pos.column + 1 << ">";
22 return out;
23}
24
25std::ostream& operator<<(std::ostream& out,
26 const std::vector<SourcePositionInfo>& stack) {
27 bool first = true;
28 for (const SourcePositionInfo& pos : stack) {
29 if (!first) out << " inlined at ";
30 out << pos;
31 first = false;
32 }
33 return out;
34}
35
36std::ostream& operator<<(std::ostream& out, const SourcePosition& pos) {
37 if (pos.isInlined()) {
38 out << "<inlined(" << pos.InliningId() << "):";
39 } else {
40 out << "<not inlined:";
41 }
42
43 if (pos.IsExternal()) {
44 out << pos.ExternalLine() << ", " << pos.ExternalFileId() << ">";
45 } else {
46 out << pos.ScriptOffset() << ">";
47 }
48 return out;
49}
50
51std::vector<SourcePositionInfo> SourcePosition::InliningStack(
52 Isolate* isolate, OptimizedCompilationInfo* cinfo) const {
53 SourcePosition pos = *this;
54 std::vector<SourcePositionInfo> stack;
55 while (pos.isInlined()) {
56 const auto& inl = cinfo->inlined_functions()[pos.InliningId()];
57 stack.push_back(SourcePositionInfo(isolate, pos, inl.shared_info));
58 pos = inl.position.position;
59 }
60 stack.push_back(SourcePositionInfo(isolate, pos, cinfo->shared_info()));
61 return stack;
62}
63
64std::vector<SourcePositionInfo> SourcePosition::InliningStack(
65 Isolate* isolate, Tagged<Code> code) const {
67 Cast<DeoptimizationData>(code->deoptimization_data());
68 SourcePosition pos = *this;
69 std::vector<SourcePositionInfo> stack;
70 while (pos.isInlined()) {
72 deopt_data->InliningPositions()->get(pos.InliningId());
74 deopt_data->GetInlinedFunction(inl.inlined_function_id), isolate);
75 stack.push_back(SourcePositionInfo(isolate, pos, function));
76 pos = inl.position;
77 }
78 DirectHandle<SharedFunctionInfo> function(deopt_data->GetSharedFunctionInfo(),
79 isolate);
80 stack.push_back(SourcePositionInfo(isolate, pos, function));
81 return stack;
82}
83
85 Tagged<Code> code) const {
88 Cast<DeoptimizationData>(code->deoptimization_data());
89 SourcePosition pos = *this;
90 if (pos.isInlined()) {
92 deopt_data->InliningPositions()->get(pos.InliningId());
94 deopt_data->GetInlinedFunction(inl.inlined_function_id), isolate);
95 return SourcePositionInfo(isolate, pos, function);
96 }
97 DirectHandle<SharedFunctionInfo> function(deopt_data->GetSharedFunctionInfo(),
98 isolate);
99 return SourcePositionInfo(isolate, pos, function);
100}
101
102void SourcePosition::Print(std::ostream& out,
103 Tagged<SharedFunctionInfo> function) const {
105 Tagged<Object> source_name;
106 if (IsScript(function->script())) {
107 Tagged<Script> script = Cast<Script>(function->script());
108 source_name = script->name();
109 script->GetPositionInfo(ScriptOffset(), &pos);
110 }
111 out << "<";
112 if (IsString(source_name)) {
113 out << Cast<String>(source_name)->ToCString().get();
114 } else {
115 out << "unknown";
116 }
117 out << ":" << pos.line + 1 << ":" << pos.column + 1 << ">";
118}
119
120void SourcePosition::PrintJson(std::ostream& out) const {
121 if (IsExternal()) {
122 out << "{ \"line\" : " << ExternalLine() << ", "
123 << " \"fileId\" : " << ExternalFileId() << ", "
124 << " \"inliningId\" : " << InliningId() << "}";
125 } else {
126 out << "{ \"scriptOffset\" : " << ScriptOffset() << ", "
127 << " \"inliningId\" : " << InliningId() << "}";
128 }
129}
130
131void SourcePosition::Print(std::ostream& out, Tagged<Code> code) const {
132 Tagged<DeoptimizationData> deopt_data =
133 Cast<DeoptimizationData>(code->deoptimization_data());
134 if (!isInlined()) {
135 Tagged<SharedFunctionInfo> function(deopt_data->GetSharedFunctionInfo());
136 Print(out, function);
137 } else {
138 InliningPosition inl = deopt_data->InliningPositions()->get(InliningId());
139 if (inl.inlined_function_id == -1) {
140 out << *this;
141 } else {
143 deopt_data->GetInlinedFunction(inl.inlined_function_id);
144 Print(out, function);
145 }
146 out << " inlined at ";
147 inl.position.Print(out, code);
148 }
149}
150
153 : position(pos), shared(indirect_handle(sfi, isolate)) {
154 {
156 if (sfi.is_null()) return;
157 Tagged<Object> maybe_script = sfi->script();
158 if (!IsScript(maybe_script)) return;
159 script = handle(Cast<Script>(maybe_script), isolate);
160 }
162 if (Script::GetPositionInfo(script, pos.ScriptOffset(), &info)) {
163 line = info.line;
164 column = info.column;
165 }
166}
167
168} // namespace internal
169} // namespace v8
SourcePosition pos
V8_INLINE bool is_null() const
Definition handles.h:693
IndirectHandle< SharedFunctionInfo > shared_info() const
static bool GetPositionInfo(DirectHandle< Script > script, int position, PositionInfo *info, OffsetFlag offset_flag=OffsetFlag::kWithOffset)
Definition objects.cc:4367
std::vector< SourcePositionInfo > InliningStack(Isolate *isolate, Tagged< Code > code) const
void Print(std::ostream &out, Tagged< Code > code) const
SourcePositionInfo FirstInfo(Isolate *isolate, Tagged< Code > code) const
void PrintJson(std::ostream &out) const
Handle< SharedFunctionInfo > info
ZoneStack< RpoNumber > & stack
int position
Definition liveedit.cc:290
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
Definition handles-inl.h:72
V8_INLINE IndirectHandle< T > indirect_handle(DirectHandle< T > handle)
Definition handles.h:757
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
SourcePositionInfo(Isolate *isolate, SourcePosition pos, DirectHandle< SharedFunctionInfo > f)