1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32 ****************************************************************************/
33 #ifndef QV4FUNCTIONOBJECT_H
34 #define QV4FUNCTIONOBJECT_H
36 #include "qv4object_p.h"
37 #include "qv4function_p.h"
38 #include "qv4context_p.h"
39 #include <private/qv4mm_p.h>
43 struct QQmlSourceLocation;
49 struct Q_QML_PRIVATE_EXPORT FunctionObject : Object {
52 Index_ProtoConstructor = 0
55 FunctionObject(QV4::ExecutionContext *scope, QV4::String *name, bool createProto = false);
56 FunctionObject(QV4::ExecutionContext *scope, QV4::Function *function, bool createProto = false);
57 FunctionObject(QV4::ExecutionContext *scope, const QString &name = QString(), bool createProto = false);
58 FunctionObject(ExecutionContext *scope, const QString &name = QString(), bool createProto = false);
59 FunctionObject(QV4::ExecutionContext *scope, const ReturnedValue name);
60 FunctionObject(ExecutionContext *scope, const ReturnedValue name);
61 FunctionObject(InternalClass *ic, QV4::Object *prototype);
64 unsigned int formalParameterCount() { return function ? function->compiledFunction->nFormals : 0; }
65 unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; }
66 bool needsActivation() const { return function ? function->needsActivation() : false; }
68 Pointer<ExecutionContext> scope;
72 struct FunctionCtor : FunctionObject {
73 FunctionCtor(QV4::ExecutionContext *scope);
76 struct FunctionPrototype : FunctionObject {
77 FunctionPrototype(InternalClass *ic, QV4::Object *prototype);
80 struct Q_QML_EXPORT BuiltinFunction : FunctionObject {
81 BuiltinFunction(QV4::ExecutionContext *scope, QV4::String *name, ReturnedValue (*code)(QV4::CallContext *));
82 ReturnedValue (*code)(QV4::CallContext *);
85 struct IndexedBuiltinFunction : FunctionObject {
86 inline IndexedBuiltinFunction(QV4::ExecutionContext *scope, uint index, ReturnedValue (*code)(QV4::CallContext *ctx, uint index));
87 ReturnedValue (*code)(QV4::CallContext *, uint index);
91 struct SimpleScriptFunction : FunctionObject {
93 Index_Name = FunctionObject::Index_Prototype + 1,
96 SimpleScriptFunction(QV4::ExecutionContext *scope, Function *function, bool createProto);
99 struct ScriptFunction : SimpleScriptFunction {
100 ScriptFunction(QV4::ExecutionContext *scope, Function *function);
103 struct BoundFunction : FunctionObject {
104 BoundFunction(QV4::ExecutionContext *scope, QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
105 Pointer<FunctionObject> target;
107 Pointer<MemberData> boundArgs;
112 struct Q_QML_EXPORT FunctionObject: Object {
114 IsFunctionObject = true
116 V4_OBJECT2(FunctionObject, Object)
117 Q_MANAGED_TYPE(FunctionObject)
120 Heap::ExecutionContext *scope() const { return d()->scope; }
121 Function *function() const { return d()->function; }
123 ReturnedValue name() const;
124 unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
125 unsigned int varCount() const { return d()->varCount(); }
127 void init(String *name, bool createProto);
129 ReturnedValue newInstance();
131 using Object::construct;
133 static ReturnedValue construct(const Managed *that, CallData *);
134 static ReturnedValue call(const Managed *that, CallData *d);
136 static Heap::FunctionObject *createScriptFunction(ExecutionContext *scope, Function *function, bool createProto = true);
138 ReturnedValue protoProperty() { return memberData()->data[Heap::FunctionObject::Index_Prototype].asReturnedValue(); }
140 bool needsActivation() const { return d()->needsActivation(); }
141 bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
142 bool isBinding() const;
143 bool isBoundFunction() const;
145 QQmlSourceLocation sourceLocation() const;
147 static void markObjects(Heap::Base *that, ExecutionEngine *e);
151 inline const FunctionObject *Value::as() const {
152 return isManaged() && m() && m()->vtable()->isFunctionObject ? reinterpret_cast<const FunctionObject *>(this) : 0;
156 struct FunctionCtor: FunctionObject
158 V4_OBJECT2(FunctionCtor, FunctionObject)
160 static ReturnedValue construct(const Managed *that, CallData *callData);
161 static ReturnedValue call(const Managed *that, CallData *callData);
164 struct FunctionPrototype: FunctionObject
166 V4_OBJECT2(FunctionPrototype, FunctionObject)
168 void init(ExecutionEngine *engine, Object *ctor);
170 static ReturnedValue method_toString(CallContext *ctx);
171 static ReturnedValue method_apply(CallContext *ctx);
172 static ReturnedValue method_call(CallContext *ctx);
173 static ReturnedValue method_bind(CallContext *ctx);
176 struct Q_QML_EXPORT BuiltinFunction: FunctionObject {
177 V4_OBJECT2(BuiltinFunction, FunctionObject)
179 static Heap::BuiltinFunction *create(ExecutionContext *scope, String *name, ReturnedValue (*code)(CallContext *))
181 return scope->engine()->memoryManager->alloc<BuiltinFunction>(scope, name, code);
184 static ReturnedValue construct(const Managed *, CallData *);
185 static ReturnedValue call(const Managed *that, CallData *callData);
188 struct IndexedBuiltinFunction: FunctionObject
190 V4_OBJECT2(IndexedBuiltinFunction, FunctionObject)
192 static ReturnedValue construct(const Managed *m, CallData *)
194 return static_cast<const IndexedBuiltinFunction *>(m)->engine()->throwTypeError();
197 static ReturnedValue call(const Managed *that, CallData *callData);
200 Heap::IndexedBuiltinFunction::IndexedBuiltinFunction(QV4::ExecutionContext *scope, uint index,
201 ReturnedValue (*code)(QV4::CallContext *ctx, uint index))
202 : Heap::FunctionObject(scope),
209 struct SimpleScriptFunction: FunctionObject {
210 V4_OBJECT2(SimpleScriptFunction, FunctionObject)
212 static ReturnedValue construct(const Managed *, CallData *callData);
213 static ReturnedValue call(const Managed *that, CallData *callData);
215 Heap::Object *protoForConstructor();
218 struct ScriptFunction: SimpleScriptFunction {
219 V4_OBJECT2(ScriptFunction, FunctionObject)
221 static ReturnedValue construct(const Managed *, CallData *callData);
222 static ReturnedValue call(const Managed *that, CallData *callData);
226 struct BoundFunction: FunctionObject {
227 V4_OBJECT2(BoundFunction, FunctionObject)
229 static Heap::BoundFunction *create(ExecutionContext *scope, FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs)
231 return scope->engine()->memoryManager->alloc<BoundFunction>(scope, target, boundThis, boundArgs);
234 Heap::FunctionObject *target() const { return d()->target; }
235 Value boundThis() const { return d()->boundThis; }
236 Heap::MemberData *boundArgs() const { return d()->boundArgs; }
238 static ReturnedValue construct(const Managed *, CallData *d);
239 static ReturnedValue call(const Managed *that, CallData *dd);
241 static void markObjects(Heap::Base *that, ExecutionEngine *e);
248 #endif // QMLJS_OBJECTS_H