| 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 | |
| 23 | namespace JSC { |
| 24 | class MacroAssemblerCodeRef; |
| 25 | } |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | struct QQmlSourceLocation; |
| 30 | |
| 31 | namespace QV4 { |
| 32 | |
| 33 | struct Q_QML_EXPORT FunctionData |
| 34 | { |
| 35 | WriteBarrier::HeapObjectWrapper<CompilationUnitRuntimeData, 1> compilationUnit; |
| 36 | |
| 37 | // Intentionally require an ExecutableCompilationUnit but save only a pointer to |
| 38 | // CompilationUnitBase. This is so that we can take advantage of the standard layout |
| 39 | // of CompilationUnitBase in the JIT. Furthermore we can safely static_cast to |
| 40 | // ExecutableCompilationUnit where we need it. |
| 41 | FunctionData(EngineBase *engine, ExecutableCompilationUnit *compilationUnit_); |
| 42 | }; |
| 43 | // Make sure this class can be accessed through offsetof (done by the assemblers): |
| 44 | Q_STATIC_ASSERT(std::is_standard_layout< FunctionData >::value); |
| 45 | |
| 46 | struct Q_QML_EXPORT Function : public FunctionData { |
| 47 | protected: |
| 48 | Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit, |
| 49 | const CompiledData::Function *function, const QQmlPrivate::AOTCompiledFunction *aotFunction); |
| 50 | ~Function(); |
| 51 | |
| 52 | public: |
| 53 | struct JSTypedFunction { |
| 54 | QVarLengthArray<QQmlType, 4> types; |
| 55 | }; |
| 56 | |
| 57 | struct AOTCompiledFunction { |
| 58 | QVarLengthArray<QMetaType, 4> types; |
| 59 | }; |
| 60 | |
| 61 | QV4::ExecutableCompilationUnit *executableCompilationUnit() const |
| 62 | { |
| 63 | // This is safe: We require an ExecutableCompilationUnit in the ctor. |
| 64 | return static_cast<QV4::ExecutableCompilationUnit *>(compilationUnit.get()); |
| 65 | } |
| 66 | |
| 67 | QV4::Heap::String *runtimeString(uint i) const |
| 68 | { |
| 69 | return compilationUnit->runtimeStrings[i]; |
| 70 | } |
| 71 | |
| 72 | bool call(QObject *thisObject, void **a, const QMetaType *types, int argc, |
| 73 | ExecutionContext *context); |
| 74 | ReturnedValue call(const Value *thisObject, const Value *argv, int argc, |
| 75 | ExecutionContext *context); |
| 76 | |
| 77 | const CompiledData::Function *compiledFunction = nullptr; |
| 78 | const char *codeData = nullptr; |
| 79 | JSC::MacroAssemblerCodeRef *codeRef = nullptr; |
| 80 | |
| 81 | typedef ReturnedValue (*JittedCode)(CppStackFrame *, ExecutionEngine *); |
| 82 | typedef void (*AotCompiledCode)(const QQmlPrivate::AOTCompiledContext *context, void **argv); |
| 83 | |
| 84 | union { |
| 85 | void *noFunction = nullptr; |
| 86 | JSTypedFunction jsTypedFunction; |
| 87 | AOTCompiledFunction aotCompiledFunction; |
| 88 | }; |
| 89 | |
| 90 | union { |
| 91 | JittedCode jittedCode = nullptr; |
| 92 | AotCompiledCode aotCompiledCode; |
| 93 | }; |
| 94 | |
| 95 | // first nArguments names in internalClass are the actual arguments |
| 96 | QV4::WriteBarrier::Pointer<Heap::InternalClass> internalClass; |
| 97 | int interpreterCallCount = 0; |
| 98 | quint16 nFormals = 0; |
| 99 | enum Kind : quint8 { JsUntyped, JsTyped, AotCompiled, Eval }; |
| 100 | Kind kind = JsUntyped; |
| 101 | bool detectedInjectedParameters = false; |
| 102 | |
| 103 | static Function *create(ExecutionEngine *engine, ExecutableCompilationUnit *unit, |
| 104 | const CompiledData::Function *function, |
| 105 | const QQmlPrivate::AOTCompiledFunction *aotFunction); |
| 106 | void destroy(); |
| 107 | |
| 108 | void mark(QV4::MarkStack *ms); |
| 109 | |
| 110 | // used when dynamically assigning signal handlers (QQmlConnection) |
| 111 | void updateInternalClass(ExecutionEngine *engine, const QList<QByteArray> ¶meters); |
| 112 | |
| 113 | inline Heap::String *name() const { |
| 114 | return runtimeString(i: compiledFunction->nameIndex); |
| 115 | } |
| 116 | |
| 117 | static QString prettyName(const Function *function, const void *address); |
| 118 | |
| 119 | inline QString sourceFile() const { return executableCompilationUnit()->fileName(); } |
| 120 | inline QUrl finalUrl() const { return executableCompilationUnit()->finalUrl(); } |
| 121 | |
| 122 | inline bool isStrict() const { return compiledFunction->flags & CompiledData::Function::IsStrict; } |
| 123 | inline bool isArrowFunction() const { return compiledFunction->flags & CompiledData::Function::IsArrowFunction; } |
| 124 | inline bool isGenerator() const { return compiledFunction->flags & CompiledData::Function::IsGenerator; } |
| 125 | inline bool isClosureWrapper() const { return compiledFunction->flags & CompiledData::Function::IsClosureWrapper; } |
| 126 | |
| 127 | QQmlSourceLocation sourceLocation() const; |
| 128 | |
| 129 | Function *nestedFunction() const |
| 130 | { |
| 131 | if (compiledFunction->nestedFunctionIndex == std::numeric_limits<uint32_t>::max()) |
| 132 | return nullptr; |
| 133 | return executableCompilationUnit()->runtimeFunctions[compiledFunction->nestedFunctionIndex]; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | QT_END_NAMESPACE |
| 140 | |
| 141 | #endif |
| 142 | |