Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qv4functionobject_p.h
Go to the documentation of this file.
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 QV4FUNCTIONOBJECT_H
4#define QV4FUNCTIONOBJECT_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 "qv4object_p.h"
18#include "qv4function_p.h"
19#include "qv4context_p.h"
20#include <private/qv4mm_p.h>
21
23
25
26namespace QV4 {
27
28struct IndexedBuiltinFunction;
29struct JSCallData;
30
31namespace Heap {
32
33
34#define FunctionObjectMembers(class, Member) \
35 Member(class, Pointer, ExecutionContext *, scope) \
36 Member(class, NoMark, Function *, function) \
37 Member(class, NoMark, VTable::Call, jsCall) \
38 Member(class, NoMark, VTable::CallAsConstructor, jsConstruct) \
39 Member(class, NoMark, VTable::CallWithMetaTypes, jsCallWithMetaTypes) \
40 Member(class, NoMark, bool, canBeTailCalled)
41
44 enum {
45 Index_ProtoConstructor = 0,
46 Index_Prototype = 0,
47 Index_HasInstance = 1,
48 };
49
50 bool isConstructor() const {
51 return jsConstruct != nullptr;
52 }
53
54 Q_QML_PRIVATE_EXPORT void init(
56 VTable::Call call, VTable::CallWithMetaTypes callWithMetaTypes = nullptr);
57 Q_QML_PRIVATE_EXPORT void init(QV4::ExecutionContext *scope, QV4::String *name = nullptr);
58 Q_QML_PRIVATE_EXPORT void init(QV4::ExecutionContext *scope, QV4::Function *function, QV4::String *n = nullptr);
59 Q_QML_PRIVATE_EXPORT void init(QV4::ExecutionContext *scope, const QString &name);
60 Q_QML_PRIVATE_EXPORT void init();
61 Q_QML_PRIVATE_EXPORT void destroy();
62
63 void setFunction(Function *f);
64
65 unsigned int formalParameterCount() { return function ? function->nFormals : 0; }
66 unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; }
67};
68
71};
72
74 void init();
75};
76
77// A function object with an additional index into a list.
78// Used by Models to refer to property roles.
82};
83
85 enum {
86 Index_Name = Index_HasInstance + 1,
88 };
90};
91
92#define ScriptFunctionMembers(class, Member) \
93 Member(class, Pointer, InternalClass *, cachedClassForConstructor)
94
98};
99
100#define MemberFunctionMembers(class, Member) \
101 Member(class, Pointer, Object *, homeObject)
102
105
106 void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name = nullptr) {
108 }
109};
110
111#define ConstructorFunctionMembers(class, Member) \
112 Member(class, Pointer, Object *, homeObject)
113
116 bool isDerivedConstructor;
117};
118
120{
122};
123
124#define BoundFunctionMembers(class, Member) \
125 Member(class, Pointer, FunctionObject *, target) \
126 Member(class, HeapValue, HeapValue, boundThis) \
127 Member(class, Pointer, MemberData *, boundArgs)
128
131
132 void init(QV4::ExecutionContext *scope, QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
133};
134
135}
136
137struct Q_QML_EXPORT FunctionObject: Object {
138 enum {
139 IsFunctionObject = true
140 };
144 V4_PROTOTYPE(functionPrototype)
146 enum { NInlineProperties = 1 };
147
148 bool canBeTailCalled() const { return d()->canBeTailCalled; }
149 Heap::ExecutionContext *scope() const { return d()->scope; }
150 Function *function() const { return d()->function; }
151
152 ReturnedValue name() const;
153 unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
154 unsigned int varCount() const { return d()->varCount(); }
155
157 defineReadonlyConfigurableProperty(engine()->id_name(), *name);
158 }
159 void createDefaultPrototypeProperty(uint protoConstructorSlot);
160
161 inline ReturnedValue callAsConstructor(const JSCallData &data) const;
162 ReturnedValue callAsConstructor(const Value *argv, int argc, const Value *newTarget = nullptr) const {
163 if (!d()->jsConstruct)
164 return engine()->throwTypeError(QStringLiteral("Function is not a constructor."));
165 return d()->jsConstruct(this, argv, argc, newTarget ? newTarget : this);
166 }
167 inline ReturnedValue call(const JSCallData &data) const;
168 ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const {
169 if (!d()->jsCall)
170 return engine()->throwTypeError(QStringLiteral("Function can only be called with |new|."));
171 return d()->jsCall(this, thisObject, argv, argc);
172 }
173 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
174 void call(QObject *thisObject, void **a, const QMetaType *types, int argc);
175 static void virtualCallWithMetaTypes(const FunctionObject *f, QObject *thisObject,
176 void **a, const QMetaType *types, int argc);
177
178 static Heap::FunctionObject *createScriptFunction(ExecutionContext *scope, Function *function);
179 static Heap::FunctionObject *createConstructorFunction(ExecutionContext *scope, Function *function, Object *homeObject, bool isDerivedConstructor);
180 static Heap::FunctionObject *createMemberFunction(ExecutionContext *scope, Function *function, Object *homeObject, String *name);
181 static Heap::FunctionObject *createBuiltinFunction(ExecutionEngine *engine, StringOrSymbol *nameOrSymbol, VTable::Call code, int argumentCount);
182
183 bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
184 bool isBinding() const;
185 bool isBoundFunction() const;
186 bool isConstructor() const {
187 return d()->isConstructor();
188 }
189
190 ReturnedValue getHomeObject() const;
191
193 return getValueByIndex(Heap::FunctionObject::Index_Prototype);
194 }
196 return !internalClass()->propertyData.at(Heap::FunctionObject::Index_HasInstance).isEmpty();
197 }
198
199 QQmlSourceLocation sourceLocation() const;
200};
201
202template<>
203inline const FunctionObject *Value::as() const {
204 return isManaged() && m()->internalClass->vtable->isFunctionObject ? reinterpret_cast<const FunctionObject *>(this) : nullptr;
205}
206
207
209{
211
212 static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
213 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
214protected:
215 enum Type {
218 };
220};
221
223{
225
226 void init(ExecutionEngine *engine, Object *ctor);
227
228 static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
229 static ReturnedValue method_apply(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
230 static ReturnedValue method_call(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
231 static ReturnedValue method_bind(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
232 static ReturnedValue method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
233};
234
235struct Q_QML_PRIVATE_EXPORT IndexedBuiltinFunction : FunctionObject
236{
238};
239
242{
243 Heap::FunctionObject::init(scope);
244 this->jsCall = call;
245 this->index = index;
246}
247
251 enum { NInlineProperties = 3 };
252
253 static void virtualCallWithMetaTypes(const FunctionObject *f, QObject *thisObject,
254 void **a, const QMetaType *types, int argc);
255 static ReturnedValue virtualCall(const QV4::FunctionObject *f, const QV4::Value *thisObject,
256 const QV4::Value *argv, int argc);
257};
258
262
263 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
264
265 Heap::InternalClass *classForConstructor() const;
266};
267
271};
272
276 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
277 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
278};
279
282 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
283 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
284};
285
288
289 static Heap::BoundFunction *create(ExecutionContext *scope, FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs)
290 {
291 return scope->engine()->memoryManager->allocate<BoundFunction>(scope, target, boundThis, boundArgs);
292 }
293
294 Heap::FunctionObject *target() const { return d()->target; }
295 Value boundThis() const { return d()->boundThis; }
296 Heap::MemberData *boundArgs() const { return d()->boundArgs; }
297
298 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
299 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
300};
301
303{
304 return d()->vtable() == BoundFunction::staticVTable();
305}
306
308{
310}
311
312}
313
315
316#endif // QMLJS_OBJECTS_H
\inmodule QtCore
Definition qmetatype.h:320
\inmodule QtCore
Definition qobject.h:90
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
type name READ getFunction WRITE setFunction
[0]
Combined button and popup list for selecting options.
\qmltype Particle \inqmlmodule QtQuick.Particles
quint64 ReturnedValue
ReturnedValue checkedResult(QV4::ExecutionEngine *v4, ReturnedValue result)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLsizei GLenum GLenum * types
GLfloat GLfloat f
GLenum target
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLfloat n
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
#define QStringLiteral(str)
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
ptrdiff_t qsizetype
Definition qtypes.h:70
unsigned int uint
Definition qtypes.h:29
#define V4_NEEDS_DESTROY
#define Q_MANAGED_TYPE(type)
#define V4_INTERNALCLASS(c)
#define DECLARE_HEAP_OBJECT(name, base)
#define DECLARE_MARKOBJECTS(class)
#define V4_PROTOTYPE(p)
#define V4_OBJECT2(DataClass, superClass)
QObject::connect nullptr
view create()
QJSEngine engine
[0]
Heap::FunctionObject * target() const
Heap::MemberData * boundArgs() const
static constexpr ReturnedValue undefined()
static QQmlRefPointer< ExecutableCompilationUnit > parse(ExecutionEngine *engine, const Value *argv, int argc, Type t=Type_Function)
ReturnedValue protoProperty() const
unsigned int varCount() const
unsigned int formalParameterCount() const
ReturnedValue callAsConstructor(const Value *argv, int argc, const Value *newTarget=nullptr) const
ReturnedValue call(const JSCallData &data) const
bool hasHasInstanceProperty() const
Function * function() const
ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const
Heap::ExecutionContext * scope() const
void setName(String *name)
void init(ExecutionEngine *engine, Object *ctor)
static ReturnedValue method_bind(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_call(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_apply(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name=nullptr)
void init(QV4::ExecutionContext *scope)
void init(QV4::ExecutionContext *scope, qsizetype index, VTable::Call call)
ExecutionEngine * engine() const
bool isManaged() const
static constexpr VTable::CallAsConstructor virtualCallAsConstructor
static constexpr VTable::Call virtualCall
static constexpr VTable::CallWithMetaTypes virtualCallWithMetaTypes
ReturnedValue(* Call)(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
Definition qv4vtable_p.h:52
void(* CallWithMetaTypes)(const FunctionObject *, QObject *, void **, const QMetaType *, int)
Definition qv4vtable_p.h:53
const T * as() const
Definition qv4value_p.h:132
Definition moc.h:24