1 | // Copyright (C) 2018 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qv4stackframe_p.h" |
5 | #include <private/qv4qobjectwrapper_p.h> |
6 | #include <QtCore/qstring.h> |
7 | |
8 | using namespace QV4; |
9 | |
10 | QString CppStackFrame::source() const |
11 | { |
12 | return v4Function ? v4Function->sourceFile() : QString(); |
13 | } |
14 | |
15 | QString CppStackFrame::function() const |
16 | { |
17 | return v4Function ? v4Function->name()->toQString() : QString(); |
18 | } |
19 | |
20 | static const CompiledData::CodeOffsetToLineAndStatement *lineAndStatement(const CppStackFrame *frame) |
21 | { |
22 | if (!frame->v4Function || frame->instructionPointer <= 0) |
23 | return nullptr; |
24 | |
25 | auto findLine = [](const CompiledData::CodeOffsetToLineAndStatement &entry, uint offset) { |
26 | return entry.codeOffset < offset; |
27 | }; |
28 | |
29 | const QV4::CompiledData::Function *cf = frame->v4Function->compiledFunction; |
30 | const uint offset = frame->instructionPointer; |
31 | const CompiledData::CodeOffsetToLineAndStatement *lineAndStatementNumbers |
32 | = cf->lineAndStatementNumberTable(); |
33 | const uint nLineAndStatementNumbers = cf->nLineAndStatementNumbers; |
34 | return std::lower_bound( |
35 | first: lineAndStatementNumbers, last: lineAndStatementNumbers + nLineAndStatementNumbers, |
36 | val: offset, comp: findLine) - 1; |
37 | } |
38 | |
39 | int CppStackFrame::lineNumber() const |
40 | { |
41 | if (auto *line = lineAndStatement(frame: this)) |
42 | return line->line; |
43 | return missingLineNumber(); |
44 | } |
45 | |
46 | int CppStackFrame::statementNumber() const |
47 | { |
48 | if (auto *statement = lineAndStatement(frame: this)) |
49 | return statement->statement; |
50 | return -1; |
51 | } |
52 | |
53 | int CppStackFrame::missingLineNumber() const |
54 | { |
55 | // Remove the first bit so that we can cast to positive int and negate. |
56 | // Remove the last bit so that it can't be -1. |
57 | const int result = -int(quintptr(this) & 0x7ffffffe); |
58 | Q_ASSERT(result < -1); |
59 | return result; |
60 | } |
61 | |
62 | ReturnedValue QV4::CppStackFrame::thisObject() const |
63 | { |
64 | if (isJSTypesFrame()) |
65 | return static_cast<const JSTypesStackFrame *>(this)->thisObject(); |
66 | |
67 | Q_ASSERT(isMetaTypesFrame()); |
68 | const auto metaTypesFrame = static_cast<const MetaTypesStackFrame *>(this); |
69 | return QObjectWrapper::wrap(engine: metaTypesFrame->context()->engine(), object: metaTypesFrame->thisObject()); |
70 | } |
71 |