2013-01-21 20:26:25 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2013-01-21 20:26:25 +00:00
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
2013-01-21 20:26:25 +00:00
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2013-01-21 20:26:25 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
2013-01-21 20:26:25 +00:00
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2013-01-21 20:26:25 +00:00
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2013-01-21 20:26:25 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef QV4FUNCTIONOBJECT_H
|
|
|
|
#define QV4FUNCTIONOBJECT_H
|
|
|
|
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4object_p.h"
|
2014-03-03 20:00:30 +00:00
|
|
|
#include "qv4function_p.h"
|
2014-07-25 15:44:14 +00:00
|
|
|
#include "qv4context_p.h"
|
2015-02-12 20:16:42 +00:00
|
|
|
#include <private/qv4mm_p.h>
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2013-01-31 09:00:06 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2015-04-10 10:34:36 +00:00
|
|
|
struct QQmlSourceLocation;
|
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
namespace QV4 {
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2014-11-03 02:17:57 +00:00
|
|
|
namespace Heap {
|
|
|
|
|
|
|
|
struct Q_QML_PRIVATE_EXPORT FunctionObject : Object {
|
2013-09-01 19:22:57 +00:00
|
|
|
enum {
|
|
|
|
Index_Prototype = 0,
|
|
|
|
Index_ProtoConstructor = 0
|
|
|
|
};
|
|
|
|
|
2014-11-03 02:17:57 +00:00
|
|
|
FunctionObject(QV4::ExecutionContext *scope, QV4::String *name, bool createProto = false);
|
2014-11-12 15:07:56 +00:00
|
|
|
FunctionObject(QV4::ExecutionContext *scope, QV4::Function *function, bool createProto = false);
|
2014-11-03 02:17:57 +00:00
|
|
|
FunctionObject(QV4::ExecutionContext *scope, const QString &name = QString(), bool createProto = false);
|
2014-11-07 18:07:54 +00:00
|
|
|
FunctionObject(ExecutionContext *scope, const QString &name = QString(), bool createProto = false);
|
2014-11-03 02:17:57 +00:00
|
|
|
FunctionObject(QV4::ExecutionContext *scope, const ReturnedValue name);
|
2014-11-07 17:10:30 +00:00
|
|
|
FunctionObject(ExecutionContext *scope, const ReturnedValue name);
|
2014-11-24 14:38:41 +00:00
|
|
|
FunctionObject(InternalClass *ic, QV4::Object *prototype);
|
2014-11-03 02:17:57 +00:00
|
|
|
~FunctionObject();
|
|
|
|
|
2014-11-07 04:24:24 +00:00
|
|
|
unsigned int formalParameterCount() { return function ? function->compiledFunction->nFormals : 0; }
|
|
|
|
unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; }
|
2015-01-09 11:30:01 +00:00
|
|
|
bool needsActivation() const { return function ? function->needsActivation() : false; }
|
2014-11-07 04:24:24 +00:00
|
|
|
|
2015-02-14 22:22:23 +00:00
|
|
|
Pointer<ExecutionContext> scope;
|
2014-11-03 02:17:57 +00:00
|
|
|
Function *function;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionCtor : FunctionObject {
|
|
|
|
FunctionCtor(QV4::ExecutionContext *scope);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionPrototype : FunctionObject {
|
2014-11-24 14:38:41 +00:00
|
|
|
FunctionPrototype(InternalClass *ic, QV4::Object *prototype);
|
2014-11-03 02:17:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Q_QML_EXPORT BuiltinFunction : FunctionObject {
|
|
|
|
BuiltinFunction(QV4::ExecutionContext *scope, QV4::String *name, ReturnedValue (*code)(QV4::CallContext *));
|
|
|
|
ReturnedValue (*code)(QV4::CallContext *);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IndexedBuiltinFunction : FunctionObject {
|
|
|
|
inline IndexedBuiltinFunction(QV4::ExecutionContext *scope, uint index, ReturnedValue (*code)(QV4::CallContext *ctx, uint index));
|
|
|
|
ReturnedValue (*code)(QV4::CallContext *, uint index);
|
|
|
|
uint index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SimpleScriptFunction : FunctionObject {
|
2015-04-09 13:50:55 +00:00
|
|
|
enum {
|
|
|
|
Index_Name = FunctionObject::Index_Prototype + 1,
|
|
|
|
Index_Length
|
|
|
|
};
|
2014-11-03 02:17:57 +00:00
|
|
|
SimpleScriptFunction(QV4::ExecutionContext *scope, Function *function, bool createProto);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ScriptFunction : SimpleScriptFunction {
|
|
|
|
ScriptFunction(QV4::ExecutionContext *scope, Function *function);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BoundFunction : FunctionObject {
|
2015-01-15 10:36:57 +00:00
|
|
|
BoundFunction(QV4::ExecutionContext *scope, QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
|
2015-02-14 22:22:23 +00:00
|
|
|
Pointer<FunctionObject> target;
|
2014-11-03 02:17:57 +00:00
|
|
|
Value boundThis;
|
2015-02-14 22:22:23 +00:00
|
|
|
Pointer<MemberData> boundArgs;
|
2014-11-03 02:17:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Q_QML_EXPORT FunctionObject: Object {
|
|
|
|
enum {
|
|
|
|
IsFunctionObject = true
|
|
|
|
};
|
|
|
|
V4_OBJECT2(FunctionObject, Object)
|
|
|
|
Q_MANAGED_TYPE(FunctionObject)
|
2014-11-13 20:38:25 +00:00
|
|
|
V4_NEEDS_DESTROY
|
2014-04-05 22:29:53 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
Heap::ExecutionContext *scope() const { return d()->scope; }
|
|
|
|
Function *function() const { return d()->function; }
|
2014-04-05 22:29:53 +00:00
|
|
|
|
2015-02-13 12:56:05 +00:00
|
|
|
ReturnedValue name() const;
|
2015-06-12 11:07:39 +00:00
|
|
|
unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
|
|
|
|
unsigned int varCount() const { return d()->varCount(); }
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2014-05-07 11:33:24 +00:00
|
|
|
void init(String *name, bool createProto);
|
2013-09-19 14:05:25 +00:00
|
|
|
|
2013-09-11 13:09:25 +00:00
|
|
|
ReturnedValue newInstance();
|
2013-05-06 10:43:39 +00:00
|
|
|
|
2014-01-20 12:51:00 +00:00
|
|
|
using Object::construct;
|
|
|
|
using Object::call;
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *that, CallData *);
|
|
|
|
static ReturnedValue call(const Managed *that, CallData *d);
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2014-11-11 16:27:49 +00:00
|
|
|
static Heap::FunctionObject *createScriptFunction(ExecutionContext *scope, Function *function, bool createProto = true);
|
2013-08-21 12:52:15 +00:00
|
|
|
|
2014-11-14 08:31:59 +00:00
|
|
|
ReturnedValue protoProperty() { return memberData()->data[Heap::FunctionObject::Index_Prototype].asReturnedValue(); }
|
2013-11-14 21:08:00 +00:00
|
|
|
|
2015-01-09 11:30:01 +00:00
|
|
|
bool needsActivation() const { return d()->needsActivation(); }
|
2015-01-09 11:01:06 +00:00
|
|
|
bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
|
2015-01-09 11:11:09 +00:00
|
|
|
bool isBinding() const;
|
2015-01-09 12:28:40 +00:00
|
|
|
bool isBoundFunction() const;
|
2014-04-05 18:23:20 +00:00
|
|
|
|
2015-04-10 10:34:36 +00:00
|
|
|
QQmlSourceLocation sourceLocation() const;
|
|
|
|
|
2014-11-01 22:04:20 +00:00
|
|
|
static void markObjects(Heap::Base *that, ExecutionEngine *e);
|
2013-01-21 20:26:25 +00:00
|
|
|
};
|
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
template<>
|
2015-02-13 09:02:28 +00:00
|
|
|
inline const FunctionObject *Value::as() const {
|
Remove type punning from QV4::Value.
The union in QV4::Value is used to do type punning. In C++, this is
compiler-defined behavior. For example, Clang and GCC will try to detect
it and try to do the proper thing. However, it can play havoc with Alias
Analysis, and it is not guaranteed that some Undefined Behavior (or
Compiler depenedent behavior) might occur.
The really problematic part is the struct inside the union: depending on
the calling convention and the register size, it results in some
exciting code. For example, the AMD64 ABI specifies that a struct of two
values of INTEGER class can be passed in separate registers when doing a
function call. Now, if the AA in the compiler looses track of the fact
that the tag overlaps with the double, you might get:
ecx := someTag
... conditional jumps
double_case:
rdx := xorredDoubleValue
callq someWhere
If the someWhere function checks for the tag first, mayhem ensues: the
double value in rdx does not overwrite the tag that is passed in ecx.
Changing the code to do reinterpret_cast<>s might also give problems
on 32bit architectures, because there is a double, whose size is not the
same as the size of the tag, which could confuse AA.
So, to fix this, the following is changed:
- only have a quint64 field in the QV4::Value, which has the added
benefit that it's very clear for the compiler that it's a POD
- as memcpy is the only approved way to ensure bit-by-bit "conversion"
between types (esp. FP<->non-FP types), change all conversions to use
memcpy. Use bitops (shift/and/or) for anything else.
- only use accessor functions for non-quint64 values
As any modern compiler has memcpy as an intrinsic, the call will be
replaced with one or a few move instructions. The accessor functions
also get inlined, the bitops get optimized, so in all cases the compiler
can generate the most compact code possible.
This patch obsoletes f558bc48585c69de36151248c969a484a969ebb4 (which had
the exact aliassing problem of the double and the tag as described
above).
Change-Id: I60a39d8564be5ce6106403a56a8de90943217006
Reviewed-by: Ulf Hermann <ulf.hermann@theqtcompany.com>
2015-07-08 08:52:59 +00:00
|
|
|
return isManaged() && m() && m()->vtable->isFunctionObject ? reinterpret_cast<const FunctionObject *>(this) : 0;
|
2013-09-16 20:02:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 12:56:05 +00:00
|
|
|
|
2013-01-21 20:26:25 +00:00
|
|
|
struct FunctionCtor: FunctionObject
|
|
|
|
{
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(FunctionCtor, FunctionObject)
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *that, CallData *callData);
|
|
|
|
static ReturnedValue call(const Managed *that, CallData *callData);
|
2013-01-21 20:26:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionPrototype: FunctionObject
|
|
|
|
{
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(FunctionPrototype, FunctionObject)
|
2014-05-09 10:15:23 +00:00
|
|
|
|
2014-05-07 14:14:08 +00:00
|
|
|
void init(ExecutionEngine *engine, Object *ctor);
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2013-11-03 14:23:05 +00:00
|
|
|
static ReturnedValue method_toString(CallContext *ctx);
|
|
|
|
static ReturnedValue method_apply(CallContext *ctx);
|
|
|
|
static ReturnedValue method_call(CallContext *ctx);
|
|
|
|
static ReturnedValue method_bind(CallContext *ctx);
|
2013-01-21 20:26:25 +00:00
|
|
|
};
|
|
|
|
|
2014-06-13 12:30:03 +00:00
|
|
|
struct Q_QML_EXPORT BuiltinFunction: FunctionObject {
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(BuiltinFunction, FunctionObject)
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2014-11-11 16:27:49 +00:00
|
|
|
static Heap::BuiltinFunction *create(ExecutionContext *scope, String *name, ReturnedValue (*code)(CallContext *))
|
2014-05-09 09:35:47 +00:00
|
|
|
{
|
2014-11-11 17:08:20 +00:00
|
|
|
return scope->engine()->memoryManager->alloc<BuiltinFunction>(scope, name, code);
|
2014-05-09 09:35:47 +00:00
|
|
|
}
|
2013-02-14 13:07:57 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *, CallData *);
|
|
|
|
static ReturnedValue call(const Managed *that, CallData *callData);
|
2013-01-21 20:26:25 +00:00
|
|
|
};
|
|
|
|
|
2013-06-07 05:53:21 +00:00
|
|
|
struct IndexedBuiltinFunction: FunctionObject
|
|
|
|
{
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(IndexedBuiltinFunction, FunctionObject)
|
2013-06-07 05:53:21 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *m, CallData *)
|
2013-06-07 05:53:21 +00:00
|
|
|
{
|
2015-02-13 12:39:20 +00:00
|
|
|
return static_cast<const IndexedBuiltinFunction *>(m)->engine()->throwTypeError();
|
2013-06-07 05:53:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue call(const Managed *that, CallData *callData);
|
2013-06-07 05:53:21 +00:00
|
|
|
};
|
|
|
|
|
2014-11-03 02:17:57 +00:00
|
|
|
Heap::IndexedBuiltinFunction::IndexedBuiltinFunction(QV4::ExecutionContext *scope, uint index,
|
|
|
|
ReturnedValue (*code)(QV4::CallContext *ctx, uint index))
|
|
|
|
: Heap::FunctionObject(scope),
|
|
|
|
code(code)
|
|
|
|
, index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-07 05:53:21 +00:00
|
|
|
|
2014-03-05 07:40:11 +00:00
|
|
|
struct SimpleScriptFunction: FunctionObject {
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(SimpleScriptFunction, FunctionObject)
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *, CallData *callData);
|
|
|
|
static ReturnedValue call(const Managed *that, CallData *callData);
|
2014-03-05 07:40:11 +00:00
|
|
|
|
2014-11-24 14:38:41 +00:00
|
|
|
Heap::Object *protoForConstructor();
|
2013-01-21 20:26:25 +00:00
|
|
|
};
|
|
|
|
|
2014-03-05 07:40:11 +00:00
|
|
|
struct ScriptFunction: SimpleScriptFunction {
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(ScriptFunction, FunctionObject)
|
2013-08-21 12:52:15 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *, CallData *callData);
|
|
|
|
static ReturnedValue call(const Managed *that, CallData *callData);
|
2013-08-21 12:52:15 +00:00
|
|
|
};
|
|
|
|
|
2014-03-05 07:40:11 +00:00
|
|
|
|
2013-01-21 20:26:25 +00:00
|
|
|
struct BoundFunction: FunctionObject {
|
2014-11-03 02:17:57 +00:00
|
|
|
V4_OBJECT2(BoundFunction, FunctionObject)
|
2014-04-05 22:36:23 +00:00
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
static Heap::BoundFunction *create(ExecutionContext *scope, FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs)
|
2014-05-09 09:35:47 +00:00
|
|
|
{
|
2014-11-11 17:08:20 +00:00
|
|
|
return scope->engine()->memoryManager->alloc<BoundFunction>(scope, target, boundThis, boundArgs);
|
2014-05-09 09:35:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
Heap::FunctionObject *target() const { return d()->target; }
|
2014-04-29 08:48:39 +00:00
|
|
|
Value boundThis() const { return d()->boundThis; }
|
2014-11-07 17:10:30 +00:00
|
|
|
Heap::MemberData *boundArgs() const { return d()->boundArgs; }
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
static ReturnedValue construct(const Managed *, CallData *d);
|
|
|
|
static ReturnedValue call(const Managed *that, CallData *dd);
|
2013-02-13 22:00:10 +00:00
|
|
|
|
2014-11-01 22:04:20 +00:00
|
|
|
static void markObjects(Heap::Base *that, ExecutionEngine *e);
|
2013-01-21 20:26:25 +00:00
|
|
|
};
|
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
}
|
2013-01-21 20:26:25 +00:00
|
|
|
|
2013-01-31 09:00:06 +00:00
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
2013-01-21 20:26:25 +00:00
|
|
|
#endif // QMLJS_OBJECTS_H
|