1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#ifndef QV4FUNCTION_H
40#define QV4FUNCTION_H
41
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include "qv4global_p.h"
54#include <private/qv4executablecompilationunit_p.h>
55#include <private/qv4context_p.h>
56#include <private/qv4string_p.h>
57
58namespace JSC {
59class MacroAssemblerCodeRef;
60}
61
62QT_BEGIN_NAMESPACE
63
64struct QQmlSourceLocation;
65
66namespace QV4 {
67
68struct Q_QML_EXPORT FunctionData {
69 CompiledData::CompilationUnitBase *compilationUnit;
70
71 // Intentionally require an ExecutableCompilationUnit but save only a pointer to
72 // CompilationUnitBase. This is so that we can take advantage of the standard layout
73 // of CompilationUnitBase in the JIT. Furthermore we can safely static_cast to
74 // ExecutableCompilationUnit where we need it.
75 FunctionData(ExecutableCompilationUnit *compilationUnit)
76 : compilationUnit(compilationUnit)
77 {}
78};
79// Make sure this class can be accessed through offsetof (done by the assemblers):
80Q_STATIC_ASSERT(std::is_standard_layout< FunctionData >::value);
81
82struct Q_QML_EXPORT Function : public FunctionData {
83private:
84 Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
85 const CompiledData::Function *function);
86 ~Function();
87
88public:
89 const CompiledData::Function *compiledFunction;
90
91 QV4::ExecutableCompilationUnit *executableCompilationUnit() const
92 {
93 // This is safe: We require an ExecutableCompilationUnit in the ctor.
94 return static_cast<QV4::ExecutableCompilationUnit *>(compilationUnit);
95 }
96
97 QV4::Heap::String *runtimeString(uint i) const
98 {
99 return compilationUnit->runtimeStrings[i];
100 }
101
102 ReturnedValue call(const Value *thisObject, const Value *argv, int argc, const ExecutionContext *context);
103
104 const char *codeData;
105
106 typedef ReturnedValue (*JittedCode)(CppStackFrame *, ExecutionEngine *);
107 JittedCode jittedCode;
108 JSC::MacroAssemblerCodeRef *codeRef;
109
110 // first nArguments names in internalClass are the actual arguments
111 Heap::InternalClass *internalClass;
112 uint nFormals;
113 int interpreterCallCount = 0;
114 bool isEval = false;
115
116 static Function *create(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
117 const CompiledData::Function *function);
118 void destroy();
119
120 // used when dynamically assigning signal handlers (QQmlConnection)
121 void updateInternalClass(ExecutionEngine *engine, const QList<QByteArray> &parameters);
122
123 inline Heap::String *name() const {
124 return runtimeString(compiledFunction->nameIndex);
125 }
126
127 static QString prettyName(const Function *function, const void *address);
128
129 inline QString sourceFile() const { return executableCompilationUnit()->fileName(); }
130 inline QUrl finalUrl() const { return executableCompilationUnit()->finalUrl(); }
131
132 inline bool isStrict() const { return compiledFunction->flags & CompiledData::Function::IsStrict; }
133 inline bool isArrowFunction() const { return compiledFunction->flags & CompiledData::Function::IsArrowFunction; }
134 inline bool isGenerator() const { return compiledFunction->flags & CompiledData::Function::IsGenerator; }
135
136 QQmlSourceLocation sourceLocation() const;
137
138 Function *nestedFunction() const
139 {
140 if (compiledFunction->nestedFunctionIndex == std::numeric_limits<uint32_t>::max())
141 return nullptr;
142 return executableCompilationUnit()->runtimeFunctions[compiledFunction->nestedFunctionIndex];
143 }
144};
145
146}
147
148QT_END_NAMESPACE
149
150#endif
151