2013-01-10 21:42:58 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-01-10 21:42:58 +00:00
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
2013-01-10 21:42:58 +00:00
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2013-01-10 21:42:58 +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
|
2016-01-19 09:38:36 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-01-10 21:42:58 +00:00
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-19 09:38:36 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
2013-01-10 21:42:58 +00:00
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-01-10 21:42:58 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4objectiterator_p.h"
|
|
|
|
#include "qv4object_p.h"
|
|
|
|
#include "qv4stringobject_p.h"
|
|
|
|
#include "qv4identifier_p.h"
|
2013-11-14 10:08:41 +00:00
|
|
|
#include "qv4argumentsobject_p.h"
|
2015-02-14 21:46:41 +00:00
|
|
|
#include "qv4string_p.h"
|
2018-05-03 19:50:18 +00:00
|
|
|
#include "qv4iterator_p.h"
|
2018-08-02 13:44:12 +00:00
|
|
|
#include "qv4propertykey_p.h"
|
2013-01-10 21:42:58 +00:00
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
using namespace QV4;
|
2013-01-10 21:42:58 +00:00
|
|
|
|
2018-05-03 19:50:18 +00:00
|
|
|
void ForInIteratorPrototype::init(ExecutionEngine *)
|
|
|
|
{
|
|
|
|
defineDefaultProperty(QStringLiteral("next"), method_next, 0);
|
|
|
|
}
|
|
|
|
|
2015-04-26 20:58:49 +00:00
|
|
|
void ObjectIterator::init(const Object *o)
|
2013-01-10 21:42:58 +00:00
|
|
|
{
|
2018-02-21 09:41:54 +00:00
|
|
|
object->setM(o ? o->m() : nullptr);
|
|
|
|
current->setM(o ? o->m() : nullptr);
|
2013-11-14 10:08:41 +00:00
|
|
|
|
2014-05-07 14:14:08 +00:00
|
|
|
if (object->as<ArgumentsObject>()) {
|
2015-01-10 21:04:54 +00:00
|
|
|
Scope scope(engine);
|
2013-11-14 10:08:41 +00:00
|
|
|
Scoped<ArgumentsObject> (scope, object->asReturnedValue())->fullyCreate();
|
|
|
|
}
|
2013-09-30 11:48:05 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
void ObjectIterator::next(Value *name, uint *index, Property *pd, PropertyAttributes *attrs)
|
2013-09-30 11:48:05 +00:00
|
|
|
{
|
2018-02-21 09:41:54 +00:00
|
|
|
name->setM(nullptr);
|
2013-01-10 21:42:58 +00:00
|
|
|
*index = UINT_MAX;
|
2014-01-08 12:08:41 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
if (!object->as<Object>()) {
|
2014-01-08 12:08:41 +00:00
|
|
|
*attrs = PropertyAttributes();
|
|
|
|
return;
|
|
|
|
}
|
2014-11-27 07:56:03 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject o(scope);
|
2014-11-28 12:25:56 +00:00
|
|
|
ScopedString n(scope);
|
2013-09-30 11:48:05 +00:00
|
|
|
|
2013-01-10 21:42:58 +00:00
|
|
|
while (1) {
|
2016-11-25 09:27:02 +00:00
|
|
|
Object *co = current->objectValue();
|
|
|
|
if (!co)
|
2013-01-10 21:42:58 +00:00
|
|
|
break;
|
|
|
|
|
2014-01-08 13:51:33 +00:00
|
|
|
while (1) {
|
2016-11-25 09:27:02 +00:00
|
|
|
co->advanceIterator(this, name, index, pd, attrs);
|
2014-01-08 13:51:33 +00:00
|
|
|
if (attrs->isEmpty())
|
|
|
|
break;
|
2013-06-13 15:15:57 +00:00
|
|
|
// check the property is not already defined earlier in the proto chain
|
2016-11-25 09:27:02 +00:00
|
|
|
if (co->heapObject() != object->heapObject()) {
|
2015-02-13 12:39:20 +00:00
|
|
|
o = object->as<Object>();
|
2014-11-28 12:25:56 +00:00
|
|
|
n = *name;
|
2014-01-08 13:51:33 +00:00
|
|
|
bool shadowed = false;
|
2015-02-13 12:39:20 +00:00
|
|
|
while (o->d() != current->heapObject()) {
|
2018-06-23 21:07:23 +00:00
|
|
|
PropertyKey id = n ? (n->toPropertyKey()) : PropertyKey::fromArrayIndex(*index);
|
2018-06-23 18:08:40 +00:00
|
|
|
if (id.isValid() && o->getOwnProperty(id) != Attr_Invalid) {
|
2014-01-08 13:51:33 +00:00
|
|
|
shadowed = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-19 11:28:26 +00:00
|
|
|
o = o->getPrototypeOf();
|
2013-06-13 15:15:57 +00:00
|
|
|
}
|
2014-01-08 13:51:33 +00:00
|
|
|
if (shadowed)
|
2013-06-13 15:15:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-08 12:08:41 +00:00
|
|
|
return;
|
2013-06-13 15:15:57 +00:00
|
|
|
}
|
2013-05-14 13:59:10 +00:00
|
|
|
|
2014-11-27 07:56:03 +00:00
|
|
|
if (flags & WithProtoChain)
|
2018-06-19 11:28:26 +00:00
|
|
|
current->setM(co->getPrototypeOf());
|
2014-11-27 07:56:03 +00:00
|
|
|
else
|
2018-02-21 09:41:54 +00:00
|
|
|
current->setM(nullptr);
|
2013-02-10 21:22:53 +00:00
|
|
|
|
2013-06-10 15:11:52 +00:00
|
|
|
arrayIndex = 0;
|
|
|
|
memberIndex = 0;
|
2013-01-10 21:42:58 +00:00
|
|
|
}
|
2014-01-08 12:08:41 +00:00
|
|
|
*attrs = PropertyAttributes();
|
2013-01-10 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 14:01:06 +00:00
|
|
|
ReturnedValue ObjectIterator::nextPropertyName(Value *value)
|
2013-01-10 21:42:58 +00:00
|
|
|
{
|
2016-11-25 09:27:02 +00:00
|
|
|
Object *o = object->objectValue();
|
|
|
|
if (!o)
|
2013-09-30 11:48:05 +00:00
|
|
|
return Encode::null();
|
|
|
|
|
2013-06-04 08:36:39 +00:00
|
|
|
PropertyAttributes attrs;
|
2013-01-10 21:42:58 +00:00
|
|
|
uint index;
|
2015-01-10 21:04:54 +00:00
|
|
|
Scope scope(engine);
|
2014-12-15 07:46:38 +00:00
|
|
|
ScopedProperty p(scope);
|
2013-09-30 11:48:05 +00:00
|
|
|
ScopedString name(scope);
|
2014-12-15 07:46:38 +00:00
|
|
|
next(name.getRef(), &index, p, &attrs);
|
2014-01-08 12:08:41 +00:00
|
|
|
if (attrs.isEmpty())
|
2013-09-12 20:37:41 +00:00
|
|
|
return Encode::null();
|
2013-06-04 08:36:39 +00:00
|
|
|
|
2016-11-25 09:27:02 +00:00
|
|
|
*value = o->getValue(p->value, attrs);
|
2013-06-04 08:36:39 +00:00
|
|
|
|
2013-09-30 11:48:05 +00:00
|
|
|
if (!!name)
|
2013-09-25 13:24:50 +00:00
|
|
|
return name->asReturnedValue();
|
2015-02-13 08:02:28 +00:00
|
|
|
Q_ASSERT(index < UINT_MAX);
|
2013-09-12 20:37:41 +00:00
|
|
|
return Encode(index);
|
2013-01-10 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 14:01:06 +00:00
|
|
|
ReturnedValue ObjectIterator::nextPropertyNameAsString(Value *value)
|
2013-01-10 21:42:58 +00:00
|
|
|
{
|
2016-11-25 09:27:02 +00:00
|
|
|
Object *o = object->objectValue();
|
|
|
|
if (!o)
|
2013-09-30 11:48:05 +00:00
|
|
|
return Encode::null();
|
|
|
|
|
2013-06-04 08:36:39 +00:00
|
|
|
PropertyAttributes attrs;
|
2013-01-10 21:42:58 +00:00
|
|
|
uint index;
|
2015-01-10 21:04:54 +00:00
|
|
|
Scope scope(engine);
|
2014-12-15 07:46:38 +00:00
|
|
|
ScopedProperty p(scope);
|
2013-09-30 11:48:05 +00:00
|
|
|
ScopedString name(scope);
|
2014-12-15 07:46:38 +00:00
|
|
|
next(name.getRef(), &index, p, &attrs);
|
2014-01-08 12:08:41 +00:00
|
|
|
if (attrs.isEmpty())
|
2013-09-12 20:37:41 +00:00
|
|
|
return Encode::null();
|
2013-06-04 08:36:39 +00:00
|
|
|
|
2016-11-25 09:27:02 +00:00
|
|
|
*value = o->getValue(p->value, attrs);
|
2013-06-04 08:36:39 +00:00
|
|
|
|
2013-09-30 11:48:05 +00:00
|
|
|
if (!!name)
|
2013-09-25 13:24:50 +00:00
|
|
|
return name->asReturnedValue();
|
2015-02-13 08:02:28 +00:00
|
|
|
Q_ASSERT(index < UINT_MAX);
|
2015-01-10 21:04:54 +00:00
|
|
|
return Encode(engine->newString(QString::number(index)));
|
2013-01-10 21:42:58 +00:00
|
|
|
}
|
2013-09-30 11:48:05 +00:00
|
|
|
|
|
|
|
ReturnedValue ObjectIterator::nextPropertyNameAsString()
|
|
|
|
{
|
2015-02-13 12:39:20 +00:00
|
|
|
if (!object->as<Object>())
|
2013-09-30 11:48:05 +00:00
|
|
|
return Encode::null();
|
|
|
|
|
|
|
|
PropertyAttributes attrs;
|
|
|
|
uint index;
|
2015-01-10 21:04:54 +00:00
|
|
|
Scope scope(engine);
|
2014-12-15 07:46:38 +00:00
|
|
|
ScopedProperty p(scope);
|
2013-09-30 11:48:05 +00:00
|
|
|
ScopedString name(scope);
|
2014-12-15 07:46:38 +00:00
|
|
|
next(name.getRef(), &index, p, &attrs);
|
2014-01-08 12:08:41 +00:00
|
|
|
if (attrs.isEmpty())
|
2013-09-30 11:48:05 +00:00
|
|
|
return Encode::null();
|
|
|
|
|
|
|
|
if (!!name)
|
|
|
|
return name->asReturnedValue();
|
2014-12-02 11:23:49 +00:00
|
|
|
Q_ASSERT(index < UINT_MAX);
|
2015-01-10 21:04:54 +00:00
|
|
|
return Encode(engine->newString(QString::number(index)));
|
2013-09-30 11:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-03 19:50:18 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(ForInIteratorObject);
|
2013-09-30 11:48:05 +00:00
|
|
|
|
2018-05-03 19:50:18 +00:00
|
|
|
void Heap::ForInIteratorObject::markObjects(Heap::Base *that, MarkStack *markStack)
|
2013-09-30 11:48:05 +00:00
|
|
|
{
|
2018-05-03 19:50:18 +00:00
|
|
|
ForInIteratorObject *o = static_cast<ForInIteratorObject *>(that);
|
2018-08-02 13:44:12 +00:00
|
|
|
o->object->mark(markStack);
|
2017-03-09 09:36:16 +00:00
|
|
|
o->workArea[0].mark(markStack);
|
|
|
|
o->workArea[1].mark(markStack);
|
|
|
|
Object::markObjects(that, markStack);
|
2013-09-30 11:48:05 +00:00
|
|
|
}
|
2018-08-02 13:44:12 +00:00
|
|
|
|
|
|
|
void Heap::ForInIteratorObject::destroy()
|
|
|
|
{
|
|
|
|
delete iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnedValue ForInIteratorPrototype::method_next(const FunctionObject *b, const Value *thisObject, const Value *, int)
|
|
|
|
{
|
|
|
|
const ForInIteratorObject *forIn = static_cast<const ForInIteratorObject *>(thisObject);
|
|
|
|
Q_ASSERT(forIn);
|
|
|
|
Scope scope(b);
|
|
|
|
|
|
|
|
ScopedPropertyKey key(scope, forIn->nextProperty());
|
|
|
|
bool done = false;
|
|
|
|
if (!key->isValid())
|
|
|
|
done = true;
|
|
|
|
ScopedStringOrSymbol s(scope, key->toStringOrSymbol(scope.engine));
|
|
|
|
return IteratorPrototype::createIterResultObject(scope.engine, s, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyKey ForInIteratorObject::nextProperty() const
|
|
|
|
{
|
|
|
|
if (!d()->current)
|
|
|
|
return PropertyKey::invalid();
|
|
|
|
|
|
|
|
Scope scope(this);
|
|
|
|
ScopedObject c(scope, d()->current);
|
|
|
|
ScopedObject o(scope);
|
|
|
|
ScopedPropertyKey key(scope);
|
|
|
|
PropertyAttributes attrs;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
while (1) {
|
|
|
|
key = d()->iterator->next(c, nullptr, &attrs);
|
|
|
|
if (!key->isValid())
|
|
|
|
break;
|
|
|
|
if (!attrs.isEnumerable() || key->isSymbol())
|
|
|
|
continue;
|
|
|
|
// check the property is not already defined earlier in the proto chain
|
|
|
|
if (d()->current != d()->object) {
|
|
|
|
o = d()->object;
|
|
|
|
bool shadowed = false;
|
|
|
|
while (o->d() != c->heapObject()) {
|
|
|
|
if (o->getOwnProperty(key) != Attr_Invalid) {
|
|
|
|
shadowed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
o = o->getPrototypeOf();
|
|
|
|
}
|
|
|
|
if (shadowed)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = c->getPrototypeOf();
|
|
|
|
d()->current.set(scope.engine, c->d());
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
delete d()->iterator;
|
|
|
|
d()->iterator = c->ownPropertyKeys();
|
|
|
|
}
|
|
|
|
return PropertyKey::invalid();
|
|
|
|
}
|