1// Copyright (C) 2016 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#ifndef QV4FUNCTION_H
4#define QV4FUNCTION_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <qqmlprivate.h>
18#include "qv4global_p.h"
19#include <private/qv4executablecompilationunit_p.h>
20#include <private/qv4context_p.h>
21#include <private/qv4string_p.h>
22
23namespace JSC {
24class MacroAssemblerCodeRef;
25}
26
27QT_BEGIN_NAMESPACE
28
29struct QQmlSourceLocation;
30
31namespace QV4 {
32
33struct Q_QML_EXPORT FunctionData {
34 CompiledData::CompilationUnitBase *compilationUnit;
35
36 // Intentionally require an ExecutableCompilationUnit but save only a pointer to
37 // CompilationUnitBase. This is so that we can take advantage of the standard layout
38 // of CompilationUnitBase in the JIT. Furthermore we can safely static_cast to
39 // ExecutableCompilationUnit where we need it.
40 FunctionData(ExecutableCompilationUnit *compilationUnit)
41 : compilationUnit(compilationUnit)
42 {}
43};
44// Make sure this class can be accessed through offsetof (done by the assemblers):
45Q_STATIC_ASSERT(std::is_standard_layout< FunctionData >::value);
46
47struct Q_QML_EXPORT Function : public FunctionData {
48protected:
49 Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
50 const CompiledData::Function *function, const QQmlPrivate::AOTCompiledFunction *aotFunction);
51 ~Function();
52
53public:
54 const CompiledData::Function *compiledFunction;
55
56 QV4::ExecutableCompilationUnit *executableCompilationUnit() const
57 {
58 // This is safe: We require an ExecutableCompilationUnit in the ctor.
59 return static_cast<QV4::ExecutableCompilationUnit *>(compilationUnit);
60 }
61
62 QV4::Heap::String *runtimeString(uint i) const
63 {
64 return compilationUnit->runtimeStrings[i];
65 }
66
67 bool call(QObject *thisObject, void **a, const QMetaType *types, int argc,
68 ExecutionContext *context);
69 ReturnedValue call(const Value *thisObject, const Value *argv, int argc,
70 ExecutionContext *context);
71
72 const char *codeData;
73
74 typedef ReturnedValue (*JittedCode)(CppStackFrame *, ExecutionEngine *);
75 JittedCode jittedCode;
76 JSC::MacroAssemblerCodeRef *codeRef;
77 const QQmlPrivate::AOTCompiledFunction *aotCompiledFunction = nullptr;
78
79 // first nArguments names in internalClass are the actual arguments
80 Heap::InternalClass *internalClass;
81 int interpreterCallCount = 0;
82 quint16 nFormals;
83 enum Kind : quint8 { JsUntyped, JsTyped, AotCompiled, Eval };
84 Kind kind = JsUntyped;
85 bool detectedInjectedParameters = false;
86
87 static Function *create(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
88 const CompiledData::Function *function,
89 const QQmlPrivate::AOTCompiledFunction *aotFunction);
90 void destroy();
91
92 // used when dynamically assigning signal handlers (QQmlConnection)
93 void updateInternalClass(ExecutionEngine *engine, const QList<QByteArray> &parameters);
94
95 inline Heap::String *name() const {
96 return runtimeString(i: compiledFunction->nameIndex);
97 }
98
99 static QString prettyName(const Function *function, const void *address);
100
101 inline QString sourceFile() const { return executableCompilationUnit()->fileName(); }
102 inline QUrl finalUrl() const { return executableCompilationUnit()->finalUrl(); }
103
104 inline bool isStrict() const { return compiledFunction->flags & CompiledData::Function::IsStrict; }
105 inline bool isArrowFunction() const { return compiledFunction->flags & CompiledData::Function::IsArrowFunction; }
106 inline bool isGenerator() const { return compiledFunction->flags & CompiledData::Function::IsGenerator; }
107 inline bool isClosureWrapper() const { return compiledFunction->flags & CompiledData::Function::IsClosureWrapper; }
108
109 QQmlSourceLocation sourceLocation() const;
110
111 Function *nestedFunction() const
112 {
113 if (compiledFunction->nestedFunctionIndex == std::numeric_limits<uint32_t>::max())
114 return nullptr;
115 return executableCompilationUnit()->runtimeFunctions[compiledFunction->nestedFunctionIndex];
116 }
117};
118
119}
120
121QT_END_NAMESPACE
122
123#endif
124

source code of qtdeclarative/src/qml/jsruntime/qv4function_p.h