Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant
4#ifndef QV4FUNCTIONOBJECT_H
5#define QV4FUNCTIONOBJECT_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qv4object_p.h"
19#include "qv4function_p.h"
20#include "qv4context_p.h"
21#include <private/qv4mm_p.h>
22
24
26
27namespace QV4 {
28
29struct IndexedBuiltinFunction;
30struct JSCallData;
31
32// A FunctionObject is generally something that can be called, either with a JavaScript
33// signature (QV4::Value etc) or with a C++ signature (QMetaType etc). For this, it has
34// the Call and CallWithMetaTypes VTable entries.
35// Some FunctionObjects need to select the actual implementation of the call at run time.
36// This comese in two flavors:
37// 1. The implementation is a JavaScript function. For these we have
38// JavaScriptFunctionObject that holds a QV4::Function member to defer the call to.
39// 2. The implementation is a C++ function. For these we have DynamicFunctionObject that
40// holds another Call member in the heap object to defer the call to.
41// In addition, a FunctionObject may want to be called as constructor. For this we have
42// another VTable entry and a flag in the heap object.
43
44namespace Heap {
45
46#define FunctionObjectMembers(class, Member)
47DECLARE_HEAP_OBJECT(FunctionObject, Object) {
48 enum {
49 Index_ProtoConstructor = 0,
50 Index_Prototype = 0,
51 Index_HasInstance = 1,
52 };
53
54 Q_QML_EXPORT void init(QV4::ExecutionEngine *engine, QV4::String *name = nullptr);
55 Q_QML_EXPORT void init(QV4::ExecutionEngine *engine, const QString &name);
56 Q_QML_EXPORT void init();
57};
58
59#define JavaScriptFunctionObjectMembers(class, Member)
60 Member(class, Pointer, ExecutionContext *, scope)
61 Member(class, NoMark, Function *, function)
62
63DECLARE_HEAP_OBJECT(JavaScriptFunctionObject, FunctionObject) {
64 DECLARE_MARKOBJECTS(JavaScriptFunctionObject)
65
66 void init(QV4::ExecutionContext *scope, QV4::Function *function, QV4::String *n = nullptr);
67 Q_QML_EXPORT void destroy();
68
69 void setFunction(Function *f);
70
71 unsigned int formalParameterCount() { return function ? function->nFormals : 0; }
72 unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; }
73};
74
75#define DynamicFunctionObjectMembers(class, Member)
76 Member(class, NoMark, VTable::Call, jsCall)
77
79 // NB: We might add a CallWithMetaTypes member to this struct and implement our
80 // builtins with metatypes, to be called from C++ code. This would make them
81 // available to qmlcachegen's C++ code generation.
82 void init(ExecutionEngine *engine, QV4::String *name, VTable::Call call);
83};
84
86 void init(QV4::ExecutionEngine *engine);
87};
88
90 void init();
91};
92
93// A function object with an additional index into a list.
94// Used by Models to refer to property roles.
96 inline void init(QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call);
98};
99
101 enum {
102 Index_Name = Index_HasInstance + 1,
104 };
105 void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name = nullptr);
106};
107
108#define ScriptFunctionMembers(class, Member)
109 Member(class, Pointer, InternalClass *, cachedClassForConstructor)
110
112 DECLARE_MARKOBJECTS(ScriptFunction)
113 void init(QV4::ExecutionContext *scope, Function *function);
114};
115
116#define MemberFunctionMembers(class, Member)
117 Member(class, Pointer, Object *, homeObject)
118
120 DECLARE_MARKOBJECTS(MemberFunction)
121
122 void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name = nullptr) {
123 ArrowFunction::init(scope, function, name);
124 }
125};
126
127#define ConstructorFunctionMembers(class, Member)
128 Member(class, Pointer, Object *, homeObject)
129
131 DECLARE_MARKOBJECTS(ConstructorFunction)
132 bool isDerivedConstructor;
133};
134
135#define DefaultClassConstructorFunctionMembers(class, Member)
136 Member(class, Pointer, ExecutionContext *, scope)
137
139 DECLARE_MARKOBJECTS(DefaultClassConstructorFunction)
140
141 bool isDerivedConstructor;
142
143 void init(QV4::ExecutionContext *scope);
144};
145
146#define BoundFunctionMembers(class, Member)
147 Member(class, Pointer, FunctionObject *, target)
148 Member(class, HeapValue, HeapValue, boundThis)
149 Member(class, Pointer, MemberData *, boundArgs)
150
151DECLARE_HEAP_OBJECT(BoundFunction, JavaScriptFunctionObject) {
152 DECLARE_MARKOBJECTS(BoundFunction)
153
154 void init(QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
155};
156
158
159}
160
166 enum { NInlineProperties = 1 };
167
168 bool canBeTailCalled() const { return vtable()->isTailCallable; }
169
170 ReturnedValue name() const;
171
176
178 const Value *argv, int argc, const Value *newTarget = nullptr) const
179 {
180 if (const auto callAsConstructor = vtable()->callAsConstructor)
181 return callAsConstructor(this, argv, argc, newTarget ? newTarget : this);
182 return failCallAsConstructor();
183 }
184
185 ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const
186 {
187 if (const auto call = vtable()->call)
188 return call(this, thisObject, argv, argc);
189 return failCall();
190 }
191
192 void call(QObject *thisObject, void **argv, const QMetaType *types, int argc) const
193 {
194 if (const auto callWithMetaTypes = vtable()->callWithMetaTypes)
196 else
197 failCall();
198 }
199
200 inline ReturnedValue callAsConstructor(const JSCallData &data) const;
201 inline ReturnedValue call(const JSCallData &data) const;
202
203 ReturnedValue failCall() const;
205 static void virtualConvertAndCall(
207 void **argv, const QMetaType *types, int argc);
208
213
214 bool isBinding() const;
215 bool isBoundFunction() const;
216 bool isConstructor() const { return vtable()->callAsConstructor; }
217
219
226};
227
228template<>
229inline const FunctionObject *Value::as() const {
230 if (!isManaged())
231 return nullptr;
232
233 const VTable *vtable = m()->internalClass->vtable;
234 return (vtable->call || vtable->callAsConstructor)
235 ? reinterpret_cast<const FunctionObject *>(this)
236 : nullptr;
237}
238
240{
243
244 Heap::ExecutionContext *scope() const { return d()->scope; }
245
246 Function *function() const { return d()->function; }
247 unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
248 unsigned int varCount() const { return d()->varCount(); }
249 bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
251};
252
254{
256
258 const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
259};
260
262{
263 V4_OBJECT2(FunctionCtor, FunctionObject)
264
265 static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
266 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
267protected:
272 static QQmlRefPointer<ExecutableCompilationUnit> parse(ExecutionEngine *engine, const Value *argv, int argc, Type t = Type_Function);
273};
274
276{
277 V4_OBJECT2(FunctionPrototype, FunctionObject)
278
280
282 const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
283
284 static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
285 static ReturnedValue method_apply(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
286 static ReturnedValue method_call(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
287 static ReturnedValue method_bind(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
288 static ReturnedValue method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
289};
290
295
297 QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call)
298{
299 Heap::FunctionObject::init(engine);
300 this->jsCall = call;
301 this->index = index;
302}
303
305 V4_OBJECT2(ArrowFunction, JavaScriptFunctionObject)
307 enum {
310 };
311
312 static void virtualCallWithMetaTypes(const FunctionObject *f, QObject *thisObject,
313 void **a, const QMetaType *types, int argc);
314 static ReturnedValue virtualCall(const QV4::FunctionObject *f, const QV4::Value *thisObject,
315 const QV4::Value *argv, int argc);
316};
317
326
331
335 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
336 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
337};
338
340 V4_OBJECT2(DefaultClassConstructorFunction, FunctionObject)
341
342 Heap::ExecutionContext *scope() const { return d()->scope; }
343 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
344 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
345};
346
348 V4_OBJECT2(BoundFunction, JavaScriptFunctionObject)
349
350 Heap::FunctionObject *target() const { return d()->target; }
351 Value boundThis() const { return d()->boundThis; }
352 Heap::MemberData *boundArgs() const { return d()->boundArgs; }
353
354 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
355};
356
359
361 const FunctionObject *f, const Value *argv, int argc, const Value *);
362};
363
364inline bool FunctionObject::isBoundFunction() const
365{
366 const VTable *vtable = d()->vtable();
368}
369
370inline ReturnedValue checkedResult(QV4::ExecutionEngine *v4, ReturnedValue result)
371{
372 return v4->hasException ? QV4::Encode::undefined() : result;
373}
374
375}
376
377QT_END_NAMESPACE
378
379#endif // QMLJS_OBJECTS_H
DECLARE_HEAP_OBJECT(DynamicFunctionObject, FunctionObject)
DECLARE_HEAP_OBJECT(BoundFunction, JavaScriptFunctionObject)
DECLARE_HEAP_OBJECT(ConstructorFunction, ScriptFunction)
DECLARE_HEAP_OBJECT(FunctionObject, Object)
DECLARE_HEAP_OBJECT(JavaScriptFunctionObject, FunctionObject)
DECLARE_HEAP_OBJECT(MemberFunction, ArrowFunction)
DECLARE_HEAP_OBJECT(ScriptFunction, ArrowFunction)
Definition qjsvalue.h:23
ReturnedValue checkedResult(QV4::ExecutionEngine *v4, ReturnedValue result)
DEFINE_OBJECT_VTABLE(SharedArrayBufferCtor)
DEFINE_OBJECT_VTABLE(ArrayBufferCtor)
DEFINE_OBJECT_VTABLE(ArrayBuffer)
DEFINE_OBJECT_VTABLE(SharedArrayBuffer)
#define V4_NEEDS_DESTROY
#define Q_MANAGED_TYPE(type)
#define V4_INTERNALCLASS(c)
#define RETURN_UNDEFINED()
static ReturnedValue method_isView(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_get_byteLength(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_slice(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
void init(ExecutionEngine *engine, Object *ctor)
static void virtualCallWithMetaTypes(const FunctionObject *f, QObject *thisObject, void **a, const QMetaType *types, int argc)
static ReturnedValue virtualCall(const QV4::FunctionObject *f, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
static QQmlRefPointer< ExecutableCompilationUnit > parse(ExecutionEngine *engine, const Value *argv, int argc, Type t=Type_Function)
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 virtualCall(const FunctionObject *f, 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::ExecutionEngine *engine)
void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name=nullptr)
void init(QV4::ExecutionEngine *engine)
void init(QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call)
void init(QV4::ExecutionEngine *engine)
Heap::InternalClass * classForConstructor() const
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
void init(ExecutionEngine *engine, Object *ctor)
static ReturnedValue method_get_byteLength(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_slice(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue slice(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc, bool shared)