aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4functionobject_p.h
blob: f4a2935b5a2fa48399f60b48a0387f03376360e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QV4FUNCTIONOBJECT_H
#define QV4FUNCTIONOBJECT_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qv4object_p.h"
#include "qv4function_p.h"
#include "qv4context_p.h"
#include <private/qv4mm_p.h>

QT_BEGIN_NAMESPACE

struct QQmlSourceLocation;

namespace QV4 {

struct IndexedBuiltinFunction;
struct JSCallData;

// A FunctionObject is generally something that can be called, either with a JavaScript
// signature (QV4::Value etc) or with a C++ signature (QMetaType etc). For this, it has
// the Call and CallWithMetaTypes VTable entries.
// Some FunctionObjects need to select the actual implementation of the call at run time.
// This comese in two flavors:
// 1. The implementation is a JavaScript function. For these we have
//    JavaScriptFunctionObject that holds a QV4::Function member to defer the call to.
// 2. The implementation is a C++ function. For these we have DynamicFunctionObject that
//    holds another Call member in the heap object to defer the call to.
// In addition, a FunctionObject may want to be called as constructor. For this we have
// another VTable entry and a flag in the heap object.

namespace Heap {

#define FunctionObjectMembers(class, Member)
DECLARE_HEAP_OBJECT(FunctionObject, Object) {
    enum {
        Index_ProtoConstructor = 0,
        Index_Prototype = 0,
        Index_HasInstance = 1,
    };

    Q_QML_EXPORT void init(QV4::ExecutionEngine *engine, QV4::String *name = nullptr);
    Q_QML_EXPORT void init(QV4::ExecutionEngine *engine, const QString &name);
    Q_QML_EXPORT void init();
};

#define JavaScriptFunctionObjectMembers(class, Member) \
    Member(class, Pointer, ExecutionContext *, scope) \
    Member(class, NoMark, Function *, function)

DECLARE_HEAP_OBJECT(JavaScriptFunctionObject, FunctionObject) {
    DECLARE_MARKOBJECTS(JavaScriptFunctionObject)

    void init(QV4::ExecutionContext *scope, QV4::Function *function, QV4::String *n = nullptr);
    Q_QML_EXPORT void destroy();

    void setFunction(Function *f);

    unsigned int formalParameterCount() { return function ? function->nFormals : 0; }
    unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; }
};

#define DynamicFunctionObjectMembers(class, Member) \
    Member(class, NoMark, VTable::Call, jsCall)

DECLARE_HEAP_OBJECT(DynamicFunctionObject, FunctionObject) {
    // NB: We might add a CallWithMetaTypes member to this struct and implement our
    //     builtins with metatypes, to be called from C++ code. This would make them
    //     available to qmlcachegen's C++ code generation.
    void init(ExecutionEngine *engine, QV4::String *name, VTable::Call call);
};

struct FunctionCtor : FunctionObject {
    void init(QV4::ExecutionEngine *engine);
};

struct FunctionPrototype : FunctionObject {
    void init();
};

// A function object with an additional index into a list.
// Used by Models to refer to property roles.
struct IndexedBuiltinFunction : DynamicFunctionObject {
    inline void init(QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call);
    qsizetype index;
};

struct ArrowFunction : JavaScriptFunctionObject {
    enum {
        Index_Name = Index_HasInstance + 1,
        Index_Length
    };
    void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name = nullptr);
};

#define ScriptFunctionMembers(class, Member) \
    Member(class, Pointer, InternalClass *, cachedClassForConstructor)

DECLARE_HEAP_OBJECT(ScriptFunction, ArrowFunction) {
    DECLARE_MARKOBJECTS(ScriptFunction)
    void init(QV4::ExecutionContext *scope, Function *function);
};

#define MemberFunctionMembers(class, Member) \
    Member(class, Pointer, Object *, homeObject)

DECLARE_HEAP_OBJECT(MemberFunction, ArrowFunction) {
    DECLARE_MARKOBJECTS(MemberFunction)

    void init(QV4::ExecutionContext *scope, Function *function, QV4::String *name = nullptr) {
        ArrowFunction::init(scope, function, name);
    }
};

#define ConstructorFunctionMembers(class, Member) \
    Member(class, Pointer, Object *, homeObject)

DECLARE_HEAP_OBJECT(ConstructorFunction, ScriptFunction) {
    DECLARE_MARKOBJECTS(ConstructorFunction)
    bool isDerivedConstructor;
};

#define DefaultClassConstructorFunctionMembers(class, Member) \
    Member(class, Pointer, ExecutionContext *, scope)

DECLARE_HEAP_OBJECT(DefaultClassConstructorFunction, FunctionObject) {
    DECLARE_MARKOBJECTS(DefaultClassConstructorFunction)

    bool isDerivedConstructor;

    void init(QV4::ExecutionContext *scope);
};

#define BoundFunctionMembers(class, Member) \
    Member(class, Pointer, FunctionObject *, target) \
    Member(class, HeapValue, HeapValue, boundThis) \
    Member(class, Pointer, MemberData *, boundArgs)

DECLARE_HEAP_OBJECT(BoundFunction, JavaScriptFunctionObject) {
    DECLARE_MARKOBJECTS(BoundFunction)

    void init(QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
};

struct BoundConstructor : BoundFunction {};

}

struct Q_QML_EXPORT FunctionObject: Object {
    V4_OBJECT2(FunctionObject, Object)
    Q_MANAGED_TYPE(FunctionObject)
    V4_INTERNALCLASS(FunctionObject)
    V4_PROTOTYPE(functionPrototype)
    enum { NInlineProperties = 1 };

    bool canBeTailCalled() const { return vtable()->isTailCallable; }

    ReturnedValue name() const;

    void setName(String *name) {
        defineReadonlyConfigurableProperty(engine()->id_name(), *name);
    }
    void createDefaultPrototypeProperty(uint protoConstructorSlot);

    ReturnedValue callAsConstructor(
            const Value *argv, int argc, const Value *newTarget = nullptr) const
    {
        if (const auto callAsConstructor = vtable()->callAsConstructor)
            return callAsConstructor(this, argv, argc, newTarget ? newTarget : this);
        return failCallAsConstructor();
    }

    ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const
    {
        if (const auto call = vtable()->call)
            return call(this, thisObject, argv, argc);
        return failCall();
    }

    void call(QObject *thisObject, void **argv, const QMetaType *types, int argc) const
    {
        if (const auto callWithMetaTypes = vtable()->callWithMetaTypes)
            callWithMetaTypes(this, thisObject, argv, types, argc);
        else
            failCall();
    }

    inline ReturnedValue callAsConstructor(const JSCallData &data) const;
    inline ReturnedValue call(const JSCallData &data) const;

    ReturnedValue failCall() const;
    ReturnedValue failCallAsConstructor() const;
    static void virtualConvertAndCall(
            const FunctionObject *f, QObject *thisObject,
            void **argv, const QMetaType *types, int argc);

    static Heap::FunctionObject *createScriptFunction(ExecutionContext *scope, Function *function);
    static Heap::FunctionObject *createConstructorFunction(ExecutionContext *scope, Function *function, Object *homeObject, bool isDerivedConstructor);
    static Heap::FunctionObject *createMemberFunction(ExecutionContext *scope, Function *function, Object *homeObject, String *name);
    static Heap::FunctionObject *createBuiltinFunction(ExecutionEngine *engine, StringOrSymbol *nameOrSymbol, VTable::Call code, int argumentCount);

    bool isBinding() const;
    bool isBoundFunction() const;
    bool isConstructor() const { return vtable()->callAsConstructor; }

    ReturnedValue getHomeObject() const;

    ReturnedValue protoProperty() const {
        return getValueByIndex(Heap::FunctionObject::Index_Prototype);
    }
    bool hasHasInstanceProperty() const {
        return !internalClass()->propertyData.at(Heap::FunctionObject::Index_HasInstance).isEmpty();
    }
};

template<>
inline const FunctionObject *Value::as() const {
    if (!isManaged())
        return nullptr;

    const VTable *vtable = m()->internalClass->vtable;
    return (vtable->call || vtable->callAsConstructor)
            ? reinterpret_cast<const FunctionObject *>(this)
            : nullptr;
}

struct Q_QML_EXPORT JavaScriptFunctionObject: FunctionObject
{
    V4_OBJECT2(JavaScriptFunctionObject, FunctionObject)
    V4_NEEDS_DESTROY

    Heap::ExecutionContext *scope() const { return d()->scope; }

    Function *function() const { return d()->function; }
    unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
    unsigned int varCount() const { return d()->varCount(); }
    bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
    QQmlSourceLocation sourceLocation() const;
};

struct Q_QML_EXPORT DynamicFunctionObject: FunctionObject
{
    V4_OBJECT2(DynamicFunctionObject, FunctionObject)

    static ReturnedValue virtualCall(
            const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};

struct FunctionCtor: FunctionObject
{
    V4_OBJECT2(FunctionCtor, FunctionObject)

    static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
    static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
protected:
    enum Type {
        Type_Function,
        Type_Generator
    };
    static QQmlRefPointer<ExecutableCompilationUnit> parse(ExecutionEngine *engine, const Value *argv, int argc, Type t = Type_Function);
};

struct FunctionPrototype: FunctionObject
{
    V4_OBJECT2(FunctionPrototype, FunctionObject)

    void init(ExecutionEngine *engine, Object *ctor);

    static ReturnedValue virtualCall(
            const FunctionObject *f, 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_apply(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_bind(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};

struct Q_QML_EXPORT IndexedBuiltinFunction : DynamicFunctionObject
{
    V4_OBJECT2(IndexedBuiltinFunction, DynamicFunctionObject)
};

void Heap::IndexedBuiltinFunction::init(
        QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call)
{
    Heap::FunctionObject::init(engine);
    this->jsCall = call;
    this->index = index;
}

struct ArrowFunction : JavaScriptFunctionObject {
    V4_OBJECT2(ArrowFunction, JavaScriptFunctionObject)
    V4_INTERNALCLASS(ArrowFunction)
    enum {
        NInlineProperties = 3,
        IsTailCallable = true,
    };

    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);
};

struct ScriptFunction : ArrowFunction {
    V4_OBJECT2(ScriptFunction, ArrowFunction)
    V4_INTERNALCLASS(ScriptFunction)

    static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);

    Heap::InternalClass *classForConstructor() const;
};

struct MemberFunction : ArrowFunction {
    V4_OBJECT2(MemberFunction, ArrowFunction)
    V4_INTERNALCLASS(MemberFunction)
};

struct ConstructorFunction : ScriptFunction {
    V4_OBJECT2(ConstructorFunction, ScriptFunction)
    V4_INTERNALCLASS(ConstructorFunction)
    static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
    static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};

struct DefaultClassConstructorFunction : FunctionObject {
    V4_OBJECT2(DefaultClassConstructorFunction, FunctionObject)

    Heap::ExecutionContext *scope() const { return d()->scope; }
    static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
    static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};

struct BoundFunction: JavaScriptFunctionObject {
    V4_OBJECT2(BoundFunction, JavaScriptFunctionObject)

    Heap::FunctionObject *target() const { return d()->target; }
    Value boundThis() const { return d()->boundThis; }
    Heap::MemberData *boundArgs() const { return d()->boundArgs; }

    static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};

struct BoundConstructor: BoundFunction {
    V4_OBJECT2(BoundConstructor, BoundFunction)

    static ReturnedValue virtualCallAsConstructor(
            const FunctionObject *f, const Value *argv, int argc, const Value *);
};

inline bool FunctionObject::isBoundFunction() const
{
    const VTable *vtable = d()->vtable();
    return vtable == BoundFunction::staticVTable() || vtable == BoundConstructor::staticVTable();
}

inline ReturnedValue checkedResult(QV4::ExecutionEngine *v4, ReturnedValue result)
{
    return v4->hasException ? QV4::Encode::undefined() : result;
}

}

QT_END_NAMESPACE

#endif // QMLJS_OBJECTS_H