2012-10-12 08:12:24 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the V4VM module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** 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
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-21 20:54:53 +00:00
|
|
|
#include "qv4object.h"
|
2012-05-07 14:05:05 +00:00
|
|
|
#include "qv4ir_p.h"
|
2012-11-19 12:15:25 +00:00
|
|
|
#include "qv4isel_p.h"
|
2013-01-21 21:17:55 +00:00
|
|
|
#include "qv4objectproto.h"
|
2013-01-21 13:20:19 +00:00
|
|
|
#include "qv4stringobject.h"
|
2013-01-22 21:23:59 +00:00
|
|
|
#include "qv4argumentsobject.h"
|
2012-12-04 12:40:18 +00:00
|
|
|
#include "qv4mm.h"
|
2012-11-16 21:21:34 +00:00
|
|
|
|
|
|
|
#include <private/qqmljsengine_p.h>
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
#include <qv4ir_p.h>
|
|
|
|
#include <qv4codegen_p.h>
|
2013-01-09 13:37:55 +00:00
|
|
|
#include "private/qlocale_tools_p.h"
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
#include <QtCore/qmath.h>
|
2012-05-07 14:05:05 +00:00
|
|
|
#include <QtCore/QDebug>
|
2012-04-16 19:23:25 +00:00
|
|
|
#include <cassert>
|
2012-05-25 15:45:15 +00:00
|
|
|
#include <typeinfo>
|
2012-11-16 21:21:34 +00:00
|
|
|
#include <iostream>
|
2013-02-08 08:30:40 +00:00
|
|
|
#include "qv4alloca_p.h"
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-05-04 13:28:04 +00:00
|
|
|
using namespace QQmlJS::VM;
|
|
|
|
|
2013-01-03 21:17:46 +00:00
|
|
|
|
2013-02-10 21:22:53 +00:00
|
|
|
Object::Object(ExecutionEngine *engine)
|
|
|
|
: prototype(0)
|
|
|
|
, internalClass(engine->emptyClass)
|
|
|
|
, memberDataAlloc(0), memberData(0)
|
2013-02-12 10:05:18 +00:00
|
|
|
, arrayOffset(0), arrayDataLen(0), arrayAlloc(0), arrayData(0), sparseArray(0)
|
2013-02-10 21:22:53 +00:00
|
|
|
{
|
|
|
|
type = Type_Object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-04 13:12:37 +00:00
|
|
|
Object::~Object()
|
|
|
|
{
|
2013-02-03 21:08:39 +00:00
|
|
|
delete [] memberData;
|
|
|
|
delete [] (arrayData - (sparseArray ? 0 : arrayOffset));
|
2013-02-03 10:36:55 +00:00
|
|
|
delete sparseArray;
|
2012-05-04 13:12:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
void Object::__put__(ExecutionContext *ctx, const QString &name, const Value &value)
|
2012-05-14 14:03:10 +00:00
|
|
|
{
|
2013-01-30 13:56:40 +00:00
|
|
|
__put__(ctx, ctx->engine->newString(name), value);
|
2012-05-14 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 13:19:35 +00:00
|
|
|
Value Object::getValue(ExecutionContext *ctx, const PropertyDescriptor *p) const
|
2012-11-10 22:35:06 +00:00
|
|
|
{
|
|
|
|
if (p->isData())
|
|
|
|
return p->value;
|
|
|
|
if (!p->get)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
|
2012-11-18 23:00:50 +00:00
|
|
|
return p->get->call(ctx, Value::fromObject(const_cast<Object *>(this)), 0, 0);
|
2012-11-10 22:35:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 13:19:35 +00:00
|
|
|
Value Object::getValueChecked(ExecutionContext *ctx, const PropertyDescriptor *p) const
|
|
|
|
{
|
|
|
|
if (!p || p->type == PropertyDescriptor::Generic)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
return getValue(ctx, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::getValueChecked(ExecutionContext *ctx, const PropertyDescriptor *p, bool *exists) const
|
|
|
|
{
|
|
|
|
*exists = p && p->type != PropertyDescriptor::Generic;
|
|
|
|
if (!*exists)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
return getValue(ctx, p);
|
|
|
|
}
|
|
|
|
|
2013-01-24 14:32:12 +00:00
|
|
|
void Object::inplaceBinOp(Value rhs, String *name, BinOp op, ExecutionContext *ctx)
|
2012-11-10 22:35:06 +00:00
|
|
|
{
|
2012-12-14 09:25:27 +00:00
|
|
|
bool hasProperty = false;
|
|
|
|
Value v = __get__(ctx, name, &hasProperty);
|
|
|
|
Value result = op(v, rhs, ctx);
|
2012-11-10 22:35:06 +00:00
|
|
|
__put__(ctx, name, result);
|
|
|
|
}
|
|
|
|
|
2013-01-24 14:32:12 +00:00
|
|
|
void Object::inplaceBinOp(Value rhs, Value index, BinOp op, ExecutionContext *ctx)
|
2012-11-11 20:56:38 +00:00
|
|
|
{
|
2013-01-11 09:19:29 +00:00
|
|
|
uint idx = index.asArrayIndex();
|
|
|
|
if (idx < UINT_MAX) {
|
|
|
|
bool hasProperty = false;
|
|
|
|
Value v = __get__(ctx, idx, &hasProperty);
|
|
|
|
v = op(v, rhs, ctx);
|
|
|
|
__put__(ctx, idx, v);
|
2013-01-25 11:16:45 +00:00
|
|
|
return;
|
2013-01-11 09:19:29 +00:00
|
|
|
}
|
2012-11-11 20:56:38 +00:00
|
|
|
String *name = index.toString(ctx);
|
|
|
|
assert(name);
|
2013-01-24 14:32:12 +00:00
|
|
|
inplaceBinOp(rhs, name, op, ctx);
|
2012-11-11 20:56:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 23:53:04 +00:00
|
|
|
void Object::defineDefaultProperty(String *name, Value value)
|
|
|
|
{
|
2013-02-02 08:52:27 +00:00
|
|
|
PropertyDescriptor *pd = insertMember(name);
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->type = PropertyDescriptor::Data;
|
2012-12-13 12:12:10 +00:00
|
|
|
pd->writable = PropertyDescriptor::Enabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Enabled;
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value value)
|
|
|
|
{
|
2013-01-30 13:56:40 +00:00
|
|
|
defineDefaultProperty(context->engine->newIdentifier(name), value);
|
2012-12-12 23:53:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 15:06:43 +00:00
|
|
|
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value (*code)(ExecutionContext *), int argumentCount)
|
2013-01-29 20:23:19 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(argumentCount);
|
2013-01-30 13:56:40 +00:00
|
|
|
String *s = context->engine->newIdentifier(name);
|
2013-01-29 20:23:19 +00:00
|
|
|
FunctionObject* function = context->engine->newBuiltinFunction(context, s, code);
|
|
|
|
function->defineReadonlyProperty(context->engine->id_length, Value::fromInt32(argumentCount));
|
|
|
|
defineDefaultProperty(s, Value::fromObject(function));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value (*code)(ExecutionContext *, Value, Value *, int), int argumentCount)
|
2012-12-12 23:53:04 +00:00
|
|
|
{
|
2013-01-10 15:06:43 +00:00
|
|
|
Q_UNUSED(argumentCount);
|
2013-01-30 13:56:40 +00:00
|
|
|
String *s = context->engine->newIdentifier(name);
|
2013-01-16 10:41:23 +00:00
|
|
|
FunctionObject* function = context->engine->newBuiltinFunction(context, s, code);
|
2013-01-13 15:17:03 +00:00
|
|
|
function->defineReadonlyProperty(context->engine->id_length, Value::fromInt32(argumentCount));
|
2013-01-10 15:06:43 +00:00
|
|
|
defineDefaultProperty(s, Value::fromObject(function));
|
2012-12-12 23:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Object::defineReadonlyProperty(ExecutionEngine *engine, const QString &name, Value value)
|
2013-01-10 15:06:43 +00:00
|
|
|
{
|
2013-01-30 13:56:40 +00:00
|
|
|
defineReadonlyProperty(engine->newIdentifier(name), value);
|
2013-01-10 15:06:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 15:17:03 +00:00
|
|
|
void Object::defineReadonlyProperty(String *name, Value value)
|
2012-12-12 23:53:04 +00:00
|
|
|
{
|
2013-02-02 08:52:27 +00:00
|
|
|
PropertyDescriptor *pd = insertMember(name);
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->type = PropertyDescriptor::Data;
|
2012-12-13 12:12:10 +00:00
|
|
|
pd->writable = PropertyDescriptor::Disabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Disabled;
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->value = value;
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:43:44 +00:00
|
|
|
void Object::markObjects()
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
|
|
|
if (prototype)
|
2013-01-25 11:43:44 +00:00
|
|
|
prototype->mark();
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-02-10 21:22:53 +00:00
|
|
|
for (int i = 0; i < internalClass->size; ++i) {
|
|
|
|
const PropertyDescriptor &pd = memberData[i];
|
|
|
|
if (pd.isData()) {
|
|
|
|
if (Managed *m = pd.value.asManaged())
|
|
|
|
m->mark();
|
|
|
|
} else if (pd.isAccessor()) {
|
|
|
|
if (pd.get)
|
|
|
|
pd.get->mark();
|
|
|
|
if (pd.set)
|
|
|
|
pd.set->mark();
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
markArrayObjects();
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 08:52:27 +00:00
|
|
|
PropertyDescriptor *Object::insertMember(String *s)
|
|
|
|
{
|
2013-02-10 21:22:53 +00:00
|
|
|
uint idx = internalClass->getOrAddMember(this, s);
|
|
|
|
|
|
|
|
if (idx >= memberDataAlloc) {
|
|
|
|
memberDataAlloc = qMax((uint)8, 2*memberDataAlloc);
|
|
|
|
PropertyDescriptor *newMemberData = new PropertyDescriptor[memberDataAlloc];
|
|
|
|
memcpy(newMemberData, memberData, sizeof(PropertyDescriptor)*idx);
|
|
|
|
delete [] memberData;
|
|
|
|
memberData = newMemberData;
|
2013-02-02 08:52:27 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
return memberData + idx;
|
2013-02-02 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 20:56:15 +00:00
|
|
|
// Section 8.12.1
|
2013-01-10 16:33:06 +00:00
|
|
|
PropertyDescriptor *Object::__getOwnProperty__(ExecutionContext *ctx, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 16:33:06 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 16:33:06 +00:00
|
|
|
return __getOwnProperty__(ctx, idx);
|
|
|
|
|
2013-02-10 21:22:53 +00:00
|
|
|
uint member = internalClass->find(name);
|
|
|
|
if (member < UINT_MAX)
|
|
|
|
return memberData + member;
|
|
|
|
|
2012-04-16 19:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:34:46 +00:00
|
|
|
PropertyDescriptor *Object::__getOwnProperty__(ExecutionContext *ctx, uint index)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-02-03 10:36:55 +00:00
|
|
|
PropertyDescriptor *p = arrayAt(index);
|
2013-01-10 14:11:32 +00:00
|
|
|
if(p && p->type != PropertyDescriptor::Generic)
|
|
|
|
return p;
|
2013-01-25 12:41:37 +00:00
|
|
|
if (isStringObject())
|
2013-01-16 12:34:46 +00:00
|
|
|
return static_cast<StringObject *>(this)->getIndex(ctx, index);
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.2
|
2012-12-14 09:25:27 +00:00
|
|
|
PropertyDescriptor *Object::__getPropertyDescriptor__(ExecutionContext *ctx, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 16:33:06 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 16:33:06 +00:00
|
|
|
return __getPropertyDescriptor__(ctx, idx);
|
|
|
|
|
2012-12-18 08:06:03 +00:00
|
|
|
|
2012-12-17 08:52:04 +00:00
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
2013-02-10 21:22:53 +00:00
|
|
|
uint idx = o->internalClass->find(name);
|
|
|
|
if (idx < UINT_MAX)
|
|
|
|
return o->memberData + idx;
|
|
|
|
|
2012-12-17 08:52:04 +00:00
|
|
|
o = o->prototype;
|
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:34:46 +00:00
|
|
|
PropertyDescriptor *Object::__getPropertyDescriptor__(ExecutionContext *ctx, uint index)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
2013-02-03 10:36:55 +00:00
|
|
|
PropertyDescriptor *p = o->arrayAt(index);
|
2013-01-10 14:11:32 +00:00
|
|
|
if(p && p->type != PropertyDescriptor::Generic)
|
|
|
|
return p;
|
2013-01-25 12:41:37 +00:00
|
|
|
if (o->isStringObject()) {
|
2013-01-16 12:34:46 +00:00
|
|
|
p = static_cast<StringObject *>(o)->getIndex(ctx, index);
|
|
|
|
if (p)
|
|
|
|
return p;
|
|
|
|
}
|
2013-01-10 14:11:32 +00:00
|
|
|
o = o->prototype;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.3
|
2012-12-14 08:57:02 +00:00
|
|
|
Value Object::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 14:11:32 +00:00
|
|
|
return __get__(ctx, idx, hasProperty);
|
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2012-12-14 08:57:02 +00:00
|
|
|
if (name->isEqualTo(ctx->engine->id___proto__)) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2012-10-29 14:37:02 +00:00
|
|
|
return Value::fromObject(prototype);
|
2012-12-14 08:57:02 +00:00
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2013-01-30 14:43:22 +00:00
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
2013-02-10 21:22:53 +00:00
|
|
|
uint idx = o->internalClass->find(name);
|
|
|
|
if (idx < UINT_MAX) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
|
|
|
return getValue(ctx, o->memberData + idx);
|
2013-01-30 14:43:22 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
|
2013-01-30 14:43:22 +00:00
|
|
|
o = o->prototype;
|
2012-12-14 08:57:02 +00:00
|
|
|
}
|
2012-11-10 22:35:06 +00:00
|
|
|
|
2012-12-14 08:57:02 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
2012-10-29 14:37:02 +00:00
|
|
|
return Value::undefinedValue();
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
Value Object::__get__(ExecutionContext *ctx, uint index, bool *hasProperty)
|
|
|
|
{
|
2013-01-30 14:43:22 +00:00
|
|
|
PropertyDescriptor *pd = 0;
|
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
2013-02-03 10:36:55 +00:00
|
|
|
PropertyDescriptor *p = o->arrayAt(index);
|
2013-01-30 14:43:22 +00:00
|
|
|
if (p && p->type != PropertyDescriptor::Generic) {
|
|
|
|
pd = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (o->isStringObject()) {
|
|
|
|
p = static_cast<StringObject *>(o)->getIndex(ctx, index);
|
|
|
|
if (p) {
|
|
|
|
pd = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o = o->prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pd) {
|
2013-01-10 14:11:32 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2013-01-30 14:43:22 +00:00
|
|
|
return getValue(ctx, pd);
|
2013-01-10 14:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
|
|
|
return Value::undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.5
|
2012-11-27 23:12:33 +00:00
|
|
|
void Object::__put__(ExecutionContext *ctx, String *name, Value value)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 14:11:32 +00:00
|
|
|
return __put__(ctx, idx, value);
|
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
PropertyDescriptor *pd = __getOwnProperty__(ctx, name);
|
2012-10-29 14:37:02 +00:00
|
|
|
// clause 1
|
2013-01-16 23:45:11 +00:00
|
|
|
if (pd) {
|
|
|
|
if (pd->isAccessor()) {
|
|
|
|
if (pd->set)
|
|
|
|
goto cont;
|
|
|
|
goto reject;
|
|
|
|
} else if (!pd->isWritable())
|
|
|
|
goto reject;
|
2013-01-25 12:41:37 +00:00
|
|
|
else if (isArrayObject() && name->isEqualTo(ctx->engine->id_length)) {
|
2013-01-16 23:45:11 +00:00
|
|
|
bool ok;
|
|
|
|
uint l = value.asArrayLength(ctx, &ok);
|
|
|
|
if (!ok)
|
|
|
|
ctx->throwRangeError(value);
|
2013-02-03 10:36:55 +00:00
|
|
|
ok = setArrayLength(l);
|
2013-01-16 23:45:11 +00:00
|
|
|
if (!ok)
|
|
|
|
goto reject;
|
|
|
|
} else {
|
|
|
|
pd->value = value;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (!prototype) {
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
} else {
|
|
|
|
if (PropertyDescriptor *p = prototype->__getPropertyDescriptor__(ctx, name)) {
|
|
|
|
if (p->isAccessor()) {
|
|
|
|
if (p->set)
|
|
|
|
goto cont;
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
if (!p->isWritable())
|
|
|
|
goto reject;
|
|
|
|
} else {
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cont:
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
// clause 4
|
|
|
|
if (!pd && prototype)
|
|
|
|
pd = prototype->__getPropertyDescriptor__(ctx, name);
|
2012-10-29 14:37:02 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
// Clause 5
|
|
|
|
if (pd && pd->isAccessor()) {
|
|
|
|
assert(pd->set != 0);
|
2012-10-29 14:37:02 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
|
|
|
pd->set->call(ctx, Value::fromObject(this), args, 1);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-29 14:37:02 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
{
|
2013-02-02 08:52:27 +00:00
|
|
|
PropertyDescriptor *p = insertMember(name);
|
2013-01-16 23:45:11 +00:00
|
|
|
p->type = PropertyDescriptor::Data;
|
|
|
|
p->value = value;
|
2012-12-13 12:12:10 +00:00
|
|
|
p->configurable = PropertyDescriptor::Enabled;
|
|
|
|
p->enumberable = PropertyDescriptor::Enabled;
|
|
|
|
p->writable = PropertyDescriptor::Enabled;
|
2012-11-27 23:12:33 +00:00
|
|
|
return;
|
2012-10-29 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reject:
|
2012-12-01 13:05:07 +00:00
|
|
|
if (ctx->strictMode)
|
2012-10-29 14:37:02 +00:00
|
|
|
__qmljs_throw_type_error(ctx);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
void Object::__put__(ExecutionContext *ctx, uint index, Value value)
|
|
|
|
{
|
2013-01-16 23:45:11 +00:00
|
|
|
PropertyDescriptor *pd = __getOwnProperty__(ctx, index);
|
2013-01-10 14:11:32 +00:00
|
|
|
// clause 1
|
2013-01-16 23:45:11 +00:00
|
|
|
if (pd) {
|
|
|
|
if (pd->isAccessor()) {
|
|
|
|
if (pd->set)
|
|
|
|
goto cont;
|
|
|
|
goto reject;
|
|
|
|
} else if (!pd->isWritable())
|
|
|
|
goto reject;
|
|
|
|
else
|
|
|
|
pd->value = value;
|
|
|
|
return;
|
|
|
|
} else if (!prototype) {
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
} else {
|
|
|
|
if (PropertyDescriptor *p = prototype->__getPropertyDescriptor__(ctx, index)) {
|
|
|
|
if (p->isAccessor()) {
|
|
|
|
if (p->set)
|
|
|
|
goto cont;
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
if (!p->isWritable())
|
|
|
|
goto reject;
|
|
|
|
} else {
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
2013-01-10 14:11:32 +00:00
|
|
|
}
|
2013-01-16 23:45:11 +00:00
|
|
|
}
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
cont:
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
// clause 4
|
|
|
|
if (!pd && prototype)
|
|
|
|
pd = prototype->__getPropertyDescriptor__(ctx, index);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
// Clause 5
|
|
|
|
if (pd && pd->isAccessor()) {
|
|
|
|
assert(pd->set != 0);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
|
|
|
pd->set->call(ctx, Value::fromObject(this), args, 1);
|
2013-01-10 14:11:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
arraySet(index, value);
|
2013-01-16 23:45:11 +00:00
|
|
|
return;
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.6
|
2012-12-04 21:46:48 +00:00
|
|
|
bool Object::__hasProperty__(const ExecutionContext *ctx, String *name) const
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 14:11:32 +00:00
|
|
|
return __hasProperty__(ctx, idx);
|
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2013-02-10 21:22:53 +00:00
|
|
|
if (internalClass->find(name) != UINT_MAX)
|
2012-12-12 08:00:19 +00:00
|
|
|
return true;
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
return prototype ? prototype->__hasProperty__(ctx, name) : false;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
bool Object::__hasProperty__(const ExecutionContext *ctx, uint index) const
|
|
|
|
{
|
2013-02-03 10:36:55 +00:00
|
|
|
const PropertyDescriptor *p = arrayAt(index);
|
2013-01-10 14:11:32 +00:00
|
|
|
if (p && p->type != PropertyDescriptor::Generic)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return prototype ? prototype->__hasProperty__(ctx, index) : false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.7
|
2012-11-27 22:23:04 +00:00
|
|
|
bool Object::__delete__(ExecutionContext *ctx, String *name)
|
2012-10-29 14:37:02 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 14:11:32 +00:00
|
|
|
return __delete__(ctx, idx);
|
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2013-02-10 21:22:53 +00:00
|
|
|
uint memberIdx = internalClass->find(name);
|
|
|
|
if (memberIdx != UINT_MAX) {
|
|
|
|
PropertyDescriptor &pd = memberData[memberIdx];
|
|
|
|
if (pd.isConfigurable()) {
|
2013-02-12 10:05:18 +00:00
|
|
|
internalClass->removeMember(this, name->identifier);
|
2013-02-10 21:22:53 +00:00
|
|
|
memmove(memberData + memberIdx, memberData + memberIdx + 1, (internalClass->size - memberIdx)*sizeof(PropertyDescriptor));
|
|
|
|
return true;
|
2012-10-29 14:37:02 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
2012-10-29 14:37:02 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
bool Object::__delete__(ExecutionContext *ctx, uint index)
|
|
|
|
{
|
2013-02-05 21:13:27 +00:00
|
|
|
PropertyDescriptor *pd = 0;
|
|
|
|
if (!sparseArray) {
|
|
|
|
if (index >= arrayDataLen)
|
|
|
|
return true;
|
|
|
|
pd = arrayAt(index);
|
|
|
|
} else {
|
|
|
|
SparseArrayNode *n = sparseArray->findNode(index);
|
|
|
|
if (n)
|
|
|
|
pd = arrayDecriptor(n->value);
|
|
|
|
}
|
|
|
|
if (!pd || pd->type == PropertyDescriptor::Generic)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (pd->isConfigurable()) {
|
|
|
|
pd->type = PropertyDescriptor::Generic;
|
|
|
|
pd->value = Value::undefinedValue();
|
|
|
|
if (sparseArray) {
|
|
|
|
pd->value.int_32 = arrayFreeList;
|
|
|
|
arrayFreeList = pd - arrayData;
|
|
|
|
}
|
2013-01-10 14:11:32 +00:00
|
|
|
return true;
|
2013-02-05 21:13:27 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 22:22:04 +00:00
|
|
|
if (ctx->strictMode)
|
2013-01-10 14:11:32 +00:00
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.9
|
2013-01-18 11:47:43 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, String *name, const PropertyDescriptor *desc)
|
2012-05-20 17:59:47 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
2013-02-12 10:05:18 +00:00
|
|
|
if (idx != UINT_MAX)
|
2013-01-10 14:11:32 +00:00
|
|
|
return __defineOwnProperty__(ctx, idx, desc);
|
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2013-01-10 22:22:04 +00:00
|
|
|
PropertyDescriptor *current;
|
|
|
|
|
2013-01-25 12:41:37 +00:00
|
|
|
if (isArrayObject() && name->isEqualTo(ctx->engine->id_length)) {
|
2013-02-03 11:34:32 +00:00
|
|
|
PropertyDescriptor *lp = memberData + ArrayObject::LengthPropertyIndex;
|
2013-02-10 21:22:53 +00:00
|
|
|
assert(0 == internalClass->find(ctx->engine->id_length));
|
2013-01-14 21:45:00 +00:00
|
|
|
if (desc->isEmpty() || desc->isSubset(lp))
|
|
|
|
return true;
|
2013-01-11 15:28:05 +00:00
|
|
|
if (!lp->isWritable() || desc->type == PropertyDescriptor::Accessor || desc->isConfigurable() || desc->isEnumerable())
|
2013-01-10 22:22:04 +00:00
|
|
|
goto reject;
|
2013-01-14 21:45:00 +00:00
|
|
|
bool succeeded = true;
|
2013-01-11 15:28:05 +00:00
|
|
|
if (desc->type == PropertyDescriptor::Data) {
|
2013-01-11 13:33:10 +00:00
|
|
|
bool ok;
|
2013-01-11 15:28:05 +00:00
|
|
|
uint l = desc->value.asArrayLength(ctx, &ok);
|
2013-01-11 13:33:10 +00:00
|
|
|
if (!ok)
|
|
|
|
ctx->throwRangeError(desc->value);
|
2013-02-03 10:36:55 +00:00
|
|
|
succeeded = setArrayLength(l);
|
2013-01-11 13:33:10 +00:00
|
|
|
}
|
2013-01-11 15:28:05 +00:00
|
|
|
if (desc->writable == PropertyDescriptor::Disabled)
|
2013-01-11 13:33:10 +00:00
|
|
|
lp->writable = PropertyDescriptor::Disabled;
|
2013-01-14 16:41:53 +00:00
|
|
|
if (!succeeded)
|
|
|
|
goto reject;
|
2013-01-11 13:33:10 +00:00
|
|
|
return true;
|
2013-01-10 22:22:04 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Clause 1
|
2013-01-10 22:22:04 +00:00
|
|
|
current = __getOwnProperty__(ctx, name);
|
2012-10-29 14:37:02 +00:00
|
|
|
if (!current) {
|
|
|
|
// clause 3
|
2012-10-28 20:56:15 +00:00
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
2012-10-29 14:37:02 +00:00
|
|
|
// clause 4
|
2013-02-02 08:52:27 +00:00
|
|
|
PropertyDescriptor *pd = insertMember(name);
|
2012-11-26 22:26:39 +00:00
|
|
|
*pd = *desc;
|
|
|
|
pd->fullyPopulated();
|
2012-10-29 14:37:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
return __defineOwnProperty__(ctx, current, desc);
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
2012-05-25 10:54:36 +00:00
|
|
|
}
|
2012-05-20 17:59:47 +00:00
|
|
|
|
2013-01-18 11:47:43 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, uint index, const PropertyDescriptor *desc)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-01-14 22:32:57 +00:00
|
|
|
PropertyDescriptor *current;
|
|
|
|
|
|
|
|
// 15.4.5.1, 4b
|
2013-02-03 11:34:32 +00:00
|
|
|
if (isArrayObject() && index >= arrayLength() && !memberData[ArrayObject::LengthPropertyIndex].isWritable())
|
2013-01-14 22:32:57 +00:00
|
|
|
goto reject;
|
|
|
|
|
2013-01-25 12:41:37 +00:00
|
|
|
if (isNonStrictArgumentsObject)
|
2013-01-22 21:23:59 +00:00
|
|
|
return static_cast<ArgumentsObject *>(this)->defineOwnProperty(ctx, index, desc);
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
// Clause 1
|
2013-01-14 22:32:57 +00:00
|
|
|
current = __getOwnProperty__(ctx, index);
|
2013-01-10 14:11:32 +00:00
|
|
|
if (!current) {
|
|
|
|
// clause 3
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
// clause 4
|
2013-02-03 10:36:55 +00:00
|
|
|
PropertyDescriptor *pd = arrayInsert(index);
|
2013-01-10 14:11:32 +00:00
|
|
|
*pd = *desc;
|
|
|
|
pd->fullyPopulated();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
return __defineOwnProperty__(ctx, current, desc);
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 11:47:43 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, PropertyDescriptor *current, const PropertyDescriptor *desc)
|
2013-01-14 22:32:57 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
// clause 5
|
|
|
|
if (desc->isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// clause 6
|
|
|
|
if (desc->isSubset(current))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// clause 7
|
|
|
|
if (!current->isConfigurable()) {
|
|
|
|
if (desc->isConfigurable())
|
|
|
|
goto reject;
|
|
|
|
if (desc->enumberable != PropertyDescriptor::Undefined && desc->enumberable != current->enumberable)
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clause 8
|
|
|
|
if (desc->isGeneric())
|
|
|
|
goto accept;
|
|
|
|
|
|
|
|
// clause 9
|
|
|
|
if (current->isData() != desc->isData()) {
|
|
|
|
// 9a
|
|
|
|
if (!current->isConfigurable())
|
|
|
|
goto reject;
|
|
|
|
if (current->isData()) {
|
|
|
|
// 9b
|
|
|
|
current->type = PropertyDescriptor::Accessor;
|
|
|
|
current->writable = PropertyDescriptor::Undefined;
|
|
|
|
current->get = 0;
|
|
|
|
current->set = 0;
|
|
|
|
} else {
|
|
|
|
// 9c
|
|
|
|
current->type = PropertyDescriptor::Data;
|
|
|
|
current->writable = PropertyDescriptor::Disabled;
|
|
|
|
current->value = Value::undefinedValue();
|
|
|
|
}
|
|
|
|
} else if (current->isData() && desc->isData()) { // clause 10
|
|
|
|
if (!current->isConfigurable() && !current->isWritable()) {
|
|
|
|
if (desc->isWritable() || !current->value.sameValue(desc->value))
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
} else { // clause 10
|
|
|
|
assert(current->isAccessor() && desc->isAccessor());
|
|
|
|
if (!current->isConfigurable()) {
|
2013-01-16 10:00:56 +00:00
|
|
|
if (desc->get && !(current->get == desc->get || (!current->get && (quintptr)desc->get == 0x1)))
|
|
|
|
goto reject;
|
|
|
|
if (desc->set && !(current->set == desc->set || (!current->set && (quintptr)desc->set == 0x1)))
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accept:
|
|
|
|
|
|
|
|
*current += *desc;
|
|
|
|
return true;
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
|
2013-01-18 11:47:43 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, const QString &name, const PropertyDescriptor *desc)
|
2012-12-11 22:58:40 +00:00
|
|
|
{
|
2013-01-30 13:56:40 +00:00
|
|
|
return __defineOwnProperty__(ctx, ctx->engine->newString(name), desc);
|
2012-12-11 22:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
void Object::copyArrayData(Object *other)
|
|
|
|
{
|
2013-02-03 21:08:39 +00:00
|
|
|
arrayReserve(other->arrayDataLen);
|
|
|
|
arrayDataLen = other->arrayDataLen;
|
|
|
|
memcpy(arrayData, other->arrayData, arrayDataLen*sizeof(PropertyDescriptor));
|
|
|
|
arrayOffset = 0;
|
|
|
|
if (other->sparseArray) {
|
2013-02-03 10:36:55 +00:00
|
|
|
sparseArray = new SparseArray(*other->sparseArray);
|
2013-02-03 21:08:39 +00:00
|
|
|
arrayFreeList = other->arrayFreeList;
|
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
if (isArrayObject())
|
2013-02-03 21:08:39 +00:00
|
|
|
setArrayLengthUnchecked(other->arrayLength());
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Value Object::arrayIndexOf(Value v, uint fromIndex, uint endIndex, ExecutionContext *ctx, Object *o)
|
|
|
|
{
|
|
|
|
bool protoHasArray = false;
|
|
|
|
Object *p = o;
|
|
|
|
while ((p = p->prototype))
|
2013-02-03 21:08:39 +00:00
|
|
|
if (p->arrayDataLen)
|
2013-02-03 10:36:55 +00:00
|
|
|
protoHasArray = true;
|
|
|
|
|
|
|
|
if (protoHasArray) {
|
|
|
|
// lets be safe and slow
|
|
|
|
for (uint i = fromIndex; i < endIndex; ++i) {
|
|
|
|
bool exists;
|
|
|
|
Value value = o->__get__(ctx, i, &exists);
|
|
|
|
if (exists && __qmljs_strict_equal(value, v))
|
|
|
|
return Value::fromDouble(i);
|
|
|
|
}
|
|
|
|
} else if (sparseArray) {
|
2013-02-03 21:08:39 +00:00
|
|
|
for (SparseArrayNode *n = sparseArray->lowerBound(fromIndex); n != sparseArray->end() && n->key() < endIndex; n = n->nextNode()) {
|
2013-02-03 10:36:55 +00:00
|
|
|
bool exists;
|
|
|
|
Value value = o->getValueChecked(ctx, arrayDecriptor(n->value), &exists);
|
|
|
|
if (exists && __qmljs_strict_equal(value, v))
|
|
|
|
return Value::fromDouble(n->key());
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-03 21:08:39 +00:00
|
|
|
if ((int) endIndex > arrayDataLen)
|
|
|
|
endIndex = arrayDataLen;
|
|
|
|
PropertyDescriptor *pd = arrayData;
|
2013-02-03 10:36:55 +00:00
|
|
|
PropertyDescriptor *end = pd + endIndex;
|
|
|
|
pd += fromIndex;
|
|
|
|
while (pd < end) {
|
|
|
|
bool exists;
|
|
|
|
Value value = o->getValueChecked(ctx, pd, &exists);
|
|
|
|
if (exists && __qmljs_strict_equal(value, v))
|
2013-02-03 21:08:39 +00:00
|
|
|
return Value::fromDouble(pd - arrayData);
|
2013-02-03 10:36:55 +00:00
|
|
|
++pd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::arrayConcat(const ArrayObject *other)
|
|
|
|
{
|
2013-02-03 21:08:39 +00:00
|
|
|
int newLen = arrayDataLen + other->arrayLength();
|
2013-02-03 10:36:55 +00:00
|
|
|
if (other->sparseArray)
|
|
|
|
initSparse();
|
|
|
|
if (sparseArray) {
|
|
|
|
if (other->sparseArray) {
|
|
|
|
for (const SparseArrayNode *it = other->sparseArray->begin(); it != other->sparseArray->end(); it = it->nextNode())
|
2013-02-03 21:08:39 +00:00
|
|
|
arraySet(arrayDataLen + it->key(), other->arrayDecriptor(it->value));
|
2013-02-03 10:36:55 +00:00
|
|
|
} else {
|
2013-02-03 21:08:39 +00:00
|
|
|
int oldSize = arrayDataLen;
|
|
|
|
arrayReserve(oldSize + other->arrayLength());
|
|
|
|
memcpy(arrayData + oldSize, other->arrayData, other->arrayLength()*sizeof(PropertyDescriptor));
|
2013-02-03 10:36:55 +00:00
|
|
|
for (uint i = 0; i < other->arrayLength(); ++i) {
|
2013-02-03 21:08:39 +00:00
|
|
|
SparseArrayNode *n = sparseArray->insert(arrayDataLen + i);
|
2013-02-03 10:36:55 +00:00
|
|
|
n->value = oldSize + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-03 21:08:39 +00:00
|
|
|
int oldSize = arrayLength();
|
|
|
|
arrayReserve(oldSize + other->arrayDataLen);
|
|
|
|
if (oldSize > arrayDataLen) {
|
|
|
|
PropertyDescriptor generic;
|
|
|
|
generic.type = PropertyDescriptor::Generic;
|
|
|
|
generic.writable = PropertyDescriptor::Undefined;
|
|
|
|
generic.value = Value::undefinedValue();
|
|
|
|
std::fill(arrayData + arrayDataLen, arrayData + oldSize, generic);
|
|
|
|
}
|
|
|
|
arrayDataLen = oldSize + other->arrayDataLen;
|
|
|
|
memcpy(arrayData + oldSize, other->arrayData, other->arrayDataLen*sizeof(PropertyDescriptor));
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
setArrayLengthUnchecked(newLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::arraySort(ExecutionContext *context, Object *thisObject, const Value &comparefn, uint len)
|
|
|
|
{
|
2013-02-03 21:08:39 +00:00
|
|
|
if (!arrayDataLen)
|
|
|
|
return;
|
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
if (sparseArray) {
|
|
|
|
context->throwUnimplemented("Object::sort unimplemented for sparse arrays");
|
|
|
|
return;
|
|
|
|
delete sparseArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayElementLessThan lessThan(context, thisObject, comparefn);
|
2013-02-03 21:08:39 +00:00
|
|
|
if (len > arrayDataLen)
|
|
|
|
len = arrayDataLen;
|
|
|
|
|
|
|
|
PropertyDescriptor *begin = arrayData;
|
2013-02-03 10:36:55 +00:00
|
|
|
std::sort(begin, begin + len, lessThan);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Object::initSparse()
|
|
|
|
{
|
|
|
|
if (!sparseArray) {
|
|
|
|
sparseArray = new SparseArray;
|
2013-02-03 21:08:39 +00:00
|
|
|
for (int i = 0; i < arrayDataLen; ++i) {
|
2013-02-05 21:14:03 +00:00
|
|
|
if (arrayData[i].type != PropertyDescriptor::Generic) {
|
|
|
|
SparseArrayNode *n = sparseArray->insert(i);
|
|
|
|
n->value = i + arrayOffset;
|
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
uint off = arrayOffset;
|
|
|
|
if (!arrayOffset) {
|
|
|
|
arrayFreeList = arrayDataLen;
|
|
|
|
} else {
|
|
|
|
arrayFreeList = 0;
|
|
|
|
arrayData -= off;
|
|
|
|
arrayAlloc += off;
|
|
|
|
int o = off;
|
2013-02-03 10:36:55 +00:00
|
|
|
for (int i = 0; i < o - 1; ++i) {
|
|
|
|
arrayData[i].type = PropertyDescriptor::Generic;
|
|
|
|
arrayData[i].value = Value::fromInt32(i + 1);
|
|
|
|
}
|
|
|
|
arrayData[o - 1].type = PropertyDescriptor::Generic;
|
2013-02-03 21:08:39 +00:00
|
|
|
arrayData[o - 1].value = Value::fromInt32(arrayDataLen + off);
|
|
|
|
}
|
|
|
|
for (int i = arrayDataLen + off; i < arrayAlloc; ++i) {
|
|
|
|
arrayData[i].type = PropertyDescriptor::Generic;
|
|
|
|
arrayData[i].value = Value::fromInt32(i + 1);
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
void Object::arrayReserve(uint n)
|
2013-02-03 10:36:55 +00:00
|
|
|
{
|
2013-02-03 21:08:39 +00:00
|
|
|
if (n < 8)
|
|
|
|
n = 8;
|
|
|
|
if (n >= arrayAlloc) {
|
|
|
|
uint off;
|
|
|
|
if (sparseArray) {
|
|
|
|
assert(arrayFreeList == arrayAlloc);
|
|
|
|
// ### FIXME
|
|
|
|
arrayDataLen = arrayAlloc;
|
|
|
|
off = 0;
|
|
|
|
} else {
|
|
|
|
off = arrayOffset;
|
|
|
|
}
|
|
|
|
arrayAlloc = qMax(n, 2*arrayAlloc);
|
|
|
|
PropertyDescriptor *newArrayData = new PropertyDescriptor[arrayAlloc];
|
|
|
|
memcpy(newArrayData, arrayData, sizeof(PropertyDescriptor)*arrayDataLen);
|
|
|
|
delete [] (arrayData - off);
|
|
|
|
arrayData = newArrayData;
|
|
|
|
if (sparseArray) {
|
|
|
|
for (uint i = arrayFreeList; i < arrayAlloc; ++i) {
|
|
|
|
arrayData[i].type = PropertyDescriptor::Generic;
|
|
|
|
arrayData[i].value = Value::fromInt32(i + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arrayOffset = 0;
|
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
bool Object::setArrayLength(uint newLen) {
|
|
|
|
assert(isArrayObject());
|
2013-02-03 11:34:32 +00:00
|
|
|
const PropertyDescriptor *lengthProperty = memberData + ArrayObject::LengthPropertyIndex;
|
2013-02-03 10:36:55 +00:00
|
|
|
if (lengthProperty && !lengthProperty->isWritable())
|
|
|
|
return false;
|
|
|
|
uint oldLen = arrayLength();
|
|
|
|
bool ok = true;
|
|
|
|
if (newLen < oldLen) {
|
|
|
|
if (sparseArray) {
|
|
|
|
SparseArrayNode *begin = sparseArray->lowerBound(newLen);
|
2013-02-03 21:08:39 +00:00
|
|
|
if (begin != sparseArray->end()) {
|
|
|
|
SparseArrayNode *it = sparseArray->end()->previousNode();
|
|
|
|
while (1) {
|
|
|
|
PropertyDescriptor &pd = arrayData[it->value];
|
|
|
|
if (pd.type != PropertyDescriptor::Generic && !pd.isConfigurable()) {
|
|
|
|
ok = false;
|
|
|
|
newLen = it->key() + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pd.type = PropertyDescriptor::Generic;
|
|
|
|
pd.value.tag = Value::_Undefined_Type;
|
|
|
|
pd.value.int_32 = arrayFreeList;
|
|
|
|
arrayFreeList = it->value;
|
|
|
|
bool brk = (it == begin);
|
|
|
|
SparseArrayNode *prev = it->previousNode();
|
|
|
|
sparseArray->erase(it);
|
|
|
|
if (brk)
|
|
|
|
break;
|
|
|
|
it = prev;
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-03 21:08:39 +00:00
|
|
|
PropertyDescriptor *it = arrayData + arrayDataLen;
|
|
|
|
const PropertyDescriptor *begin = arrayData + newLen;
|
2013-02-03 10:36:55 +00:00
|
|
|
while (--it >= begin) {
|
|
|
|
if (it->type != PropertyDescriptor::Generic && !it->isConfigurable()) {
|
|
|
|
ok = false;
|
2013-02-03 21:08:39 +00:00
|
|
|
newLen = it - arrayData + 1;
|
2013-02-03 10:36:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
arrayDataLen = newLen;
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (newLen >= 0x100000)
|
|
|
|
initSparse();
|
|
|
|
}
|
|
|
|
setArrayLengthUnchecked(newLen);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::markArrayObjects() const
|
|
|
|
{
|
2013-02-03 21:08:39 +00:00
|
|
|
for (uint i = 0; i < arrayDataLen; ++i) {
|
|
|
|
const PropertyDescriptor &pd = arrayData[i];
|
2013-02-03 10:36:55 +00:00
|
|
|
if (pd.isData()) {
|
|
|
|
if (Managed *m = pd.value.asManaged())
|
|
|
|
m->mark();
|
|
|
|
} else if (pd.isAccessor()) {
|
|
|
|
if (pd.get)
|
|
|
|
pd.get->mark();
|
|
|
|
if (pd.set)
|
|
|
|
pd.set->mark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-11 13:33:10 +00:00
|
|
|
void ArrayObject::init(ExecutionContext *context)
|
|
|
|
{
|
2013-01-25 12:13:06 +00:00
|
|
|
type = Type_ArrayObject;
|
|
|
|
|
2013-02-02 08:52:27 +00:00
|
|
|
PropertyDescriptor *pd = insertMember(context->engine->id_length);
|
2013-02-03 11:34:32 +00:00
|
|
|
assert(pd == memberData + LengthPropertyIndex);
|
2013-01-11 13:33:10 +00:00
|
|
|
pd->type = PropertyDescriptor::Data;
|
|
|
|
pd->writable = PropertyDescriptor::Enabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Disabled;
|
|
|
|
pd->value = Value::fromInt32(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-25 11:43:44 +00:00
|
|
|
void ForEachIteratorObject::markObjects()
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2013-01-25 11:43:44 +00:00
|
|
|
Object::markObjects();
|
2013-01-10 21:42:58 +00:00
|
|
|
if (it.object)
|
2013-01-25 11:43:44 +00:00
|
|
|
it.object->mark();
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|