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"
|
2013-03-05 11:49:35 +00:00
|
|
|
#include "qv4jsir_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>
|
2013-03-05 11:49:35 +00:00
|
|
|
#include <qv4jsir_p.h>
|
2012-11-16 21:21:34 +00:00
|
|
|
#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-02-14 13:07:57 +00:00
|
|
|
DEFINE_MANAGED_VTABLE(Object);
|
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-04-10 08:46:23 +00:00
|
|
|
, arrayOffset(0), arrayDataLen(0), arrayAlloc(0), arrayAttributes(0), arrayData(0), sparseArray(0)
|
2013-03-05 12:12:37 +00:00
|
|
|
, externalResource(0)
|
2013-02-10 21:22:53 +00:00
|
|
|
{
|
2013-02-13 22:00:10 +00:00
|
|
|
vtbl = &static_vtbl;
|
2013-02-10 21:22:53 +00:00
|
|
|
type = Type_Object;
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:47:26 +00:00
|
|
|
Object::Object(ExecutionContext *context)
|
|
|
|
: prototype(0)
|
|
|
|
, internalClass(context->engine->emptyClass)
|
|
|
|
, memberDataAlloc(0), memberData(0)
|
2013-04-10 08:46:23 +00:00
|
|
|
, arrayOffset(0), arrayDataLen(0), arrayAlloc(0), arrayAttributes(0), arrayData(0), sparseArray(0)
|
2013-03-08 10:47:26 +00:00
|
|
|
, externalResource(0)
|
|
|
|
{
|
|
|
|
vtbl = &static_vtbl;
|
|
|
|
type = Type_Object;
|
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
|
2012-05-04 13:12:37 +00:00
|
|
|
Object::~Object()
|
|
|
|
{
|
2013-03-05 12:12:37 +00:00
|
|
|
delete externalResource;
|
2013-02-03 21:08:39 +00:00
|
|
|
delete [] memberData;
|
|
|
|
delete [] (arrayData - (sparseArray ? 0 : arrayOffset));
|
2013-04-10 08:46:23 +00:00
|
|
|
if (arrayAttributes)
|
|
|
|
delete [] (arrayAttributes - (sparseArray ? 0 : arrayOffset));
|
2013-02-03 10:36:55 +00:00
|
|
|
delete sparseArray;
|
2013-02-14 14:00:06 +00:00
|
|
|
_data = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::destroy(Managed *that)
|
|
|
|
{
|
|
|
|
static_cast<Object *>(that)->~Object();
|
2012-05-04 13:12:37 +00:00
|
|
|
}
|
|
|
|
|
2013-03-07 11:55:02 +00:00
|
|
|
void Object::put(ExecutionContext *ctx, const QString &name, const Value &value)
|
2012-05-14 14:03:10 +00:00
|
|
|
{
|
2013-03-07 11:55:02 +00:00
|
|
|
put(ctx, ctx->engine->newString(name), value);
|
2012-05-14 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Value Object::getValue(const Value &thisObject, ExecutionContext *ctx, const Property *p, PropertyAttributes attrs)
|
2012-11-10 22:35:06 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!attrs.isAccessor())
|
2012-11-10 22:35:06 +00:00
|
|
|
return p->value;
|
2013-04-10 08:46:23 +00:00
|
|
|
FunctionObject *getter = p->getter();
|
|
|
|
if (!getter)
|
2012-11-10 22:35:06 +00:00
|
|
|
return Value::undefinedValue();
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
return getter->call(ctx, thisObject, 0, 0);
|
2013-03-07 11:25:59 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
void Object::putValue(ExecutionContext *ctx, Property *pd, PropertyAttributes attrs, const Value &value)
|
2013-02-12 15:23:52 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isAccessor()) {
|
|
|
|
if (pd->set) {
|
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
|
|
|
pd->set->call(ctx, Value::fromObject(this), args, 1);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-12 15:23:52 +00:00
|
|
|
goto reject;
|
2013-03-07 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!attrs.isWritable())
|
2013-03-07 11:55:02 +00:00
|
|
|
goto reject;
|
|
|
|
|
|
|
|
pd->value = value;
|
|
|
|
return;
|
|
|
|
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
ctx->throwTypeError();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-14 09:45:49 +00:00
|
|
|
void Object::inplaceBinOp(ExecutionContext *ctx, BinOp op, String *name, const Value &rhs)
|
2012-11-10 22:35:06 +00:00
|
|
|
{
|
2013-03-08 08:17:11 +00:00
|
|
|
Value v = get(ctx, name);
|
2013-02-13 15:22:10 +00:00
|
|
|
Value result;
|
2013-02-14 09:45:49 +00:00
|
|
|
op(ctx, &result, v, rhs);
|
2013-03-07 11:55:02 +00:00
|
|
|
put(ctx, name, result);
|
2012-11-10 22:35:06 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 09:45:49 +00:00
|
|
|
void Object::inplaceBinOp(ExecutionContext *ctx, BinOp op, const Value &index, const Value &rhs)
|
2012-11-11 20:56:38 +00:00
|
|
|
{
|
2013-02-14 09:45:49 +00:00
|
|
|
uint idx = index.asArrayIndex();
|
2013-01-11 09:19:29 +00:00
|
|
|
if (idx < UINT_MAX) {
|
|
|
|
bool hasProperty = false;
|
2013-03-07 11:25:59 +00:00
|
|
|
Value v = getIndexed(ctx, idx, &hasProperty);
|
2013-02-13 15:22:10 +00:00
|
|
|
Value result;
|
2013-02-14 09:45:49 +00:00
|
|
|
op(ctx, &result, v, rhs);
|
2013-03-07 11:55:02 +00:00
|
|
|
putIndexed(ctx, idx, result);
|
2013-01-25 11:16:45 +00:00
|
|
|
return;
|
2013-01-11 09:19:29 +00:00
|
|
|
}
|
2013-02-14 09:45:49 +00:00
|
|
|
String *name = index.toString(ctx);
|
2012-11-11 20:56:38 +00:00
|
|
|
assert(name);
|
2013-02-13 20:14:17 +00:00
|
|
|
inplaceBinOp(ctx, op, name, rhs);
|
2012-11-11 20:56:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 23:53:04 +00:00
|
|
|
void Object::defineDefaultProperty(String *name, Value value)
|
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *pd = insertMember(name, Attr_Data|Attr_NotEnumerable);
|
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-04-05 19:15:58 +00:00
|
|
|
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value (*code)(SimpleCallContext *), 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));
|
|
|
|
}
|
|
|
|
|
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-04-10 08:46:23 +00:00
|
|
|
Property *pd = insertMember(name, Attr_ReadOnly);
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->value = value;
|
|
|
|
}
|
|
|
|
|
2013-02-13 22:00:10 +00:00
|
|
|
void Object::markObjects(Managed *that)
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2013-02-13 22:00:10 +00:00
|
|
|
Object *o = static_cast<Object *>(that);
|
|
|
|
if (o->prototype)
|
|
|
|
o->prototype->mark();
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-02-13 22:00:10 +00:00
|
|
|
for (int i = 0; i < o->internalClass->size; ++i) {
|
2013-04-10 08:46:23 +00:00
|
|
|
const Property &pd = o->memberData[i];
|
|
|
|
if (o->internalClass->propertyData[i].isData()) {
|
2013-02-10 21:22:53 +00:00
|
|
|
if (Managed *m = pd.value.asManaged())
|
|
|
|
m->mark();
|
2013-04-10 08:46:23 +00:00
|
|
|
} else {
|
|
|
|
if (pd.getter())
|
|
|
|
pd.getter()->mark();
|
|
|
|
if (pd.setter())
|
|
|
|
pd.setter()->mark();
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-13 22:00:10 +00:00
|
|
|
o->markArrayObjects();
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *Object::insertMember(String *s, PropertyAttributes attributes)
|
2013-02-02 08:52:27 +00:00
|
|
|
{
|
2013-04-08 11:53:26 +00:00
|
|
|
uint idx;
|
2013-04-08 13:38:30 +00:00
|
|
|
internalClass = internalClass->addMember(s, attributes, &idx);
|
2013-02-10 21:22:53 +00:00
|
|
|
|
|
|
|
if (idx >= memberDataAlloc) {
|
|
|
|
memberDataAlloc = qMax((uint)8, 2*memberDataAlloc);
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *newMemberData = new Property[memberDataAlloc];
|
|
|
|
memcpy(newMemberData, memberData, sizeof(Property)*idx);
|
2013-02-10 21:22:53 +00:00
|
|
|
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-04-10 08:46:23 +00:00
|
|
|
Property *Object::__getOwnProperty__(ExecutionContext *ctx, String *name, PropertyAttributes *attrs)
|
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-04-10 08:46:23 +00:00
|
|
|
return __getOwnProperty__(ctx, idx, attrs);
|
2013-01-10 16:33:06 +00:00
|
|
|
|
2013-02-10 21:22:53 +00:00
|
|
|
uint member = internalClass->find(name);
|
2013-04-10 08:46:23 +00:00
|
|
|
if (member < UINT_MAX) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = internalClass->propertyData[member];
|
2013-02-10 21:22:53 +00:00
|
|
|
return memberData + member;
|
2013-04-10 08:46:23 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = Attr_Invalid;
|
2012-04-16 19:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *Object::__getOwnProperty__(ExecutionContext *ctx, uint index, PropertyAttributes *attrs)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
uint pidx = propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx < UINT_MAX) {
|
|
|
|
Property *p = arrayData + pidx;
|
|
|
|
if (!arrayAttributes || arrayAttributes[pidx].isData()) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = arrayAttributes ? arrayAttributes[pidx] : PropertyAttributes(Attr_Data);
|
|
|
|
return p;
|
|
|
|
} else if (arrayAttributes[pidx].isAccessor()) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = arrayAttributes ? arrayAttributes[pidx] : PropertyAttributes(Attr_Accessor);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isStringObject()) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = Attr_NotConfigurable|Attr_NotWritable;
|
2013-01-16 12:34:46 +00:00
|
|
|
return static_cast<StringObject *>(this)->getIndex(ctx, index);
|
2013-04-10 08:46:23 +00:00
|
|
|
}
|
2013-01-16 12:34:46 +00:00
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = Attr_Invalid;
|
2013-01-10 14:11:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.2
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *Object::__getPropertyDescriptor__(const ExecutionContext *ctx, String *name, PropertyAttributes *attrs) const
|
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
|
|
|
|
2013-03-07 09:57:48 +00:00
|
|
|
const Object *o = this;
|
2012-12-17 08:52:04 +00:00
|
|
|
while (o) {
|
2013-02-10 21:22:53 +00:00
|
|
|
uint idx = o->internalClass->find(name);
|
2013-04-10 08:46:23 +00:00
|
|
|
if (idx < UINT_MAX) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = o->internalClass->propertyData[idx];
|
2013-02-10 21:22:53 +00:00
|
|
|
return o->memberData + idx;
|
2013-04-10 08:46:23 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
|
2012-12-17 08:52:04 +00:00
|
|
|
o = o->prototype;
|
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = Attr_Invalid;
|
2012-04-16 19:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *Object::__getPropertyDescriptor__(const ExecutionContext *ctx, uint index, PropertyAttributes *attrs) const
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-03-07 09:57:48 +00:00
|
|
|
const Object *o = this;
|
2013-01-10 14:11:32 +00:00
|
|
|
while (o) {
|
2013-04-10 08:46:23 +00:00
|
|
|
uint pidx = o->propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx < UINT_MAX) {
|
|
|
|
Property *p = o->arrayData + pidx;
|
|
|
|
if (!o->arrayAttributes || !o->arrayAttributes[pidx].isGeneric()) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = o->arrayAttributes ? o->arrayAttributes[pidx] : PropertyAttributes(Attr_Data);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 12:41:37 +00:00
|
|
|
if (o->isStringObject()) {
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *p = static_cast<const StringObject *>(o)->getIndex(ctx, index);
|
|
|
|
if (p) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = (Attr_NotWritable|Attr_NotConfigurable);
|
2013-01-16 12:34:46 +00:00
|
|
|
return p;
|
2013-04-10 08:46:23 +00:00
|
|
|
}
|
2013-01-16 12:34:46 +00:00
|
|
|
}
|
2013-01-10 14:11:32 +00:00
|
|
|
o = o->prototype;
|
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = Attr_Invalid;
|
2013-01-10 14:11:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-07 11:55:02 +00:00
|
|
|
Value Object::get(Managed *m, ExecutionContext *ctx, String *name, bool *hasProperty)
|
|
|
|
{
|
|
|
|
return static_cast<Object *>(m)->internalGet(ctx, name, hasProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::getIndexed(Managed *m, ExecutionContext *ctx, uint index, bool *hasProperty)
|
|
|
|
{
|
|
|
|
return static_cast<Object *>(m)->internalGetIndexed(ctx, index, hasProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::put(Managed *m, ExecutionContext *ctx, String *name, const Value &value)
|
|
|
|
{
|
|
|
|
static_cast<Object *>(m)->internalPut(ctx, name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::putIndexed(Managed *m, ExecutionContext *ctx, uint index, const Value &value)
|
|
|
|
{
|
|
|
|
static_cast<Object *>(m)->internalPutIndexed(ctx, index, value);
|
|
|
|
}
|
|
|
|
|
2013-04-08 13:38:30 +00:00
|
|
|
PropertyAttributes Object::query(Managed *m, ExecutionContext *ctx, String *name)
|
2013-03-07 11:55:02 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != UINT_MAX)
|
|
|
|
return queryIndexed(m, ctx, idx);
|
|
|
|
|
|
|
|
const Object *o = static_cast<Object *>(m);
|
|
|
|
while (o) {
|
|
|
|
uint idx = o->internalClass->find(name);
|
|
|
|
if (idx < UINT_MAX)
|
|
|
|
return o->internalClass->propertyData[idx];
|
|
|
|
|
|
|
|
o = o->prototype;
|
|
|
|
}
|
|
|
|
return Attr_Invalid;
|
2013-03-07 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 13:38:30 +00:00
|
|
|
PropertyAttributes Object::queryIndexed(Managed *m, ExecutionContext *ctx, uint index)
|
2013-03-07 11:55:02 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
const Object *o = static_cast<Object *>(m);
|
|
|
|
while (o) {
|
|
|
|
uint pidx = o->propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx < UINT_MAX) {
|
|
|
|
if (o->arrayAttributes)
|
|
|
|
return o->arrayAttributes[pidx];
|
|
|
|
return Attr_Data;
|
|
|
|
}
|
|
|
|
if (o->isStringObject()) {
|
|
|
|
Property *p = static_cast<const StringObject *>(o)->getIndex(ctx, index);
|
|
|
|
if (p)
|
|
|
|
return Attr_Data;
|
|
|
|
}
|
|
|
|
o = o->prototype;
|
|
|
|
}
|
|
|
|
return Attr_Invalid;
|
2013-03-07 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Object::deleteProperty(Managed *m, ExecutionContext *ctx, String *name)
|
|
|
|
{
|
2013-03-07 12:14:34 +00:00
|
|
|
return static_cast<Object *>(m)->internalDeleteProperty(ctx, name);
|
2013-03-07 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Object::deleteIndexedProperty(Managed *m, ExecutionContext *ctx, uint index)
|
|
|
|
{
|
2013-03-07 12:14:34 +00:00
|
|
|
return static_cast<Object *>(m)->internalDeleteIndexedProperty(ctx, index);
|
2013-03-07 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.3
|
2013-03-07 11:55:02 +00:00
|
|
|
Value Object::internalGet(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-03-07 11:25:59 +00:00
|
|
|
return getIndexed(ctx, idx, hasProperty);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
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;
|
2013-04-10 08:46:23 +00:00
|
|
|
return getValue(ctx, o->memberData + idx, o->internalClass->propertyData.at(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-03-07 11:55:02 +00:00
|
|
|
Value Object::internalGetIndexed(ExecutionContext *ctx, uint index, bool *hasProperty)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *pd = 0;
|
|
|
|
PropertyAttributes attrs = Attr_Data;
|
2013-01-30 14:43:22 +00:00
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
2013-04-10 08:46:23 +00:00
|
|
|
uint pidx = o->propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx < UINT_MAX) {
|
|
|
|
if (!o->arrayAttributes || !o->arrayAttributes[pidx].isGeneric()) {
|
|
|
|
pd = o->arrayData + pidx;
|
|
|
|
if (o->arrayAttributes)
|
|
|
|
attrs = o->arrayAttributes[pidx];
|
|
|
|
break;
|
|
|
|
}
|
2013-01-30 14:43:22 +00:00
|
|
|
}
|
|
|
|
if (o->isStringObject()) {
|
2013-04-10 08:46:23 +00:00
|
|
|
pd = static_cast<StringObject *>(o)->getIndex(ctx, index);
|
|
|
|
if (pd) {
|
|
|
|
attrs = (Attr_NotWritable|Attr_NotConfigurable);
|
2013-01-30 14:43:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o = o->prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pd) {
|
2013-01-10 14:11:32 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2013-04-10 08:46:23 +00:00
|
|
|
return getValue(ctx, pd, attrs);
|
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
|
2013-03-07 11:55:02 +00:00
|
|
|
void Object::internalPut(ExecutionContext *ctx, String *name, const 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-03-07 11:55:02 +00:00
|
|
|
return putIndexed(ctx, idx, value);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2013-03-08 08:38:46 +00:00
|
|
|
uint member = internalClass->find(name);
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *pd = 0;
|
|
|
|
PropertyAttributes attrs;
|
|
|
|
if (member < UINT_MAX) {
|
|
|
|
pd = memberData + member;
|
|
|
|
attrs = internalClass->propertyData[member];
|
|
|
|
}
|
2013-03-08 08:38:46 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// clause 1
|
2013-01-16 23:45:11 +00:00
|
|
|
if (pd) {
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isAccessor()) {
|
|
|
|
if (pd->setter())
|
|
|
|
goto cont;
|
|
|
|
goto reject;
|
|
|
|
} else if (!attrs.isWritable())
|
2013-01-16 23:45:11 +00:00
|
|
|
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 {
|
2013-04-12 12:36:41 +00:00
|
|
|
// clause 4
|
|
|
|
if ((pd = prototype->__getPropertyDescriptor__(ctx, name, &attrs))) {
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isAccessor()) {
|
2013-04-12 12:36:41 +00:00
|
|
|
if (!pd->setter())
|
|
|
|
goto reject;
|
|
|
|
} else if (!extensible || !attrs.isWritable()) {
|
2013-01-16 23:45:11 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
2013-04-12 12:36:41 +00:00
|
|
|
} else if (!extensible) {
|
|
|
|
goto reject;
|
2013-01-16 23:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cont:
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
// Clause 5
|
2013-04-10 08:46:23 +00:00
|
|
|
if (pd && attrs.isAccessor()) {
|
|
|
|
assert(pd->setter() != 0);
|
2012-10-29 14:37:02 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
2013-04-10 08:46:23 +00:00
|
|
|
pd->setter()->call(ctx, Value::fromObject(this), args, 1);
|
2013-01-16 23:45:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-10-29 14:37:02 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *p = insertMember(name, Attr_Data);
|
2013-01-16 23:45:11 +00:00
|
|
|
p->value = value;
|
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)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-03-07 11:55:02 +00:00
|
|
|
void Object::internalPutIndexed(ExecutionContext *ctx, uint index, const Value &value)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *pd = 0;
|
|
|
|
PropertyAttributes attrs;
|
|
|
|
|
|
|
|
uint pidx = propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx < UINT_MAX) {
|
|
|
|
if (arrayAttributes && arrayAttributes[pidx].isGeneric()) {
|
|
|
|
pidx = UINT_MAX;
|
|
|
|
} else {
|
|
|
|
pd = arrayData + pidx;
|
|
|
|
attrs = arrayAttributes ? arrayAttributes[pidx] : PropertyAttributes(Attr_Data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pd && isStringObject()) {
|
2013-03-08 08:38:46 +00:00
|
|
|
pd = static_cast<StringObject *>(this)->getIndex(ctx, index);
|
2013-04-10 08:46:23 +00:00
|
|
|
if (pd)
|
|
|
|
// not writable
|
|
|
|
goto reject;
|
|
|
|
}
|
2013-03-08 08:38:46 +00:00
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
// clause 1
|
2013-01-16 23:45:11 +00:00
|
|
|
if (pd) {
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isAccessor()) {
|
|
|
|
if (pd->setter())
|
|
|
|
goto cont;
|
|
|
|
goto reject;
|
|
|
|
} else if (!attrs.isWritable())
|
2013-01-16 23:45:11 +00:00
|
|
|
goto reject;
|
|
|
|
else
|
|
|
|
pd->value = value;
|
|
|
|
return;
|
|
|
|
} else if (!prototype) {
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
} else {
|
2013-04-12 12:36:41 +00:00
|
|
|
// clause 4
|
|
|
|
if ((pd = prototype->__getPropertyDescriptor__(ctx, index, &attrs))) {
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isAccessor()) {
|
2013-04-12 12:36:41 +00:00
|
|
|
if (!pd->setter())
|
|
|
|
goto reject;
|
|
|
|
} else if (!extensible || !attrs.isWritable()) {
|
2013-01-16 23:45:11 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
2013-04-12 12:36:41 +00:00
|
|
|
} 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 5
|
2013-04-10 08:46:23 +00:00
|
|
|
if (pd && attrs.isAccessor()) {
|
|
|
|
assert(pd->setter() != 0);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-16 23:45:11 +00:00
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
2013-04-10 08:46:23 +00:00
|
|
|
pd->setter()->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)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-10 14:11:32 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.7
|
2013-03-07 12:14:34 +00:00
|
|
|
bool Object::internalDeleteProperty(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-03-07 12:14:34 +00:00
|
|
|
return deleteIndexedProperty(ctx, idx);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
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) {
|
2013-04-10 08:46:23 +00:00
|
|
|
if (internalClass->propertyData[memberIdx].isConfigurable()) {
|
2013-02-12 10:05:18 +00:00
|
|
|
internalClass->removeMember(this, name->identifier);
|
2013-04-10 08:46:23 +00:00
|
|
|
memmove(memberData + memberIdx, memberData + memberIdx + 1, (internalClass->size - memberIdx)*sizeof(Property));
|
2013-02-10 21:22:53 +00:00
|
|
|
return true;
|
2012-10-29 14:37:02 +00:00
|
|
|
}
|
2013-02-10 21:22:53 +00:00
|
|
|
if (ctx->strictMode)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-02-10 21:22:53 +00:00
|
|
|
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-03-07 12:14:34 +00:00
|
|
|
bool Object::internalDeleteIndexedProperty(ExecutionContext *ctx, uint index)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
uint pidx = propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx == UINT_MAX)
|
|
|
|
return true;
|
|
|
|
if (arrayAttributes && arrayAttributes[pidx].isGeneric())
|
2013-02-05 21:13:27 +00:00
|
|
|
return true;
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!arrayAttributes || arrayAttributes[pidx].isConfigurable()) {
|
|
|
|
arrayData[pidx].value = Value::undefinedValue();
|
|
|
|
if (!arrayAttributes)
|
|
|
|
ensureArrayAttributes();
|
|
|
|
arrayAttributes[pidx].clear();
|
2013-02-05 21:13:27 +00:00
|
|
|
if (sparseArray) {
|
2013-04-10 08:46:23 +00:00
|
|
|
arrayData[pidx].value.int_32 = arrayFreeList;
|
|
|
|
arrayFreeList = pidx;
|
2013-02-05 21:13:27 +00:00
|
|
|
}
|
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-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-10 14:11:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.9
|
2013-04-10 08:46:23 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, String *name, const Property &p, PropertyAttributes attrs)
|
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-04-10 08:46:23 +00:00
|
|
|
return __defineOwnProperty__(ctx, idx, p, attrs);
|
2013-01-10 14:11:32 +00:00
|
|
|
|
2013-01-30 13:56:40 +00:00
|
|
|
name->makeIdentifier(ctx);
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *current;
|
|
|
|
PropertyAttributes *cattrs;
|
2013-01-10 22:22:04 +00:00
|
|
|
|
2013-01-25 12:41:37 +00:00
|
|
|
if (isArrayObject() && name->isEqualTo(ctx->engine->id_length)) {
|
2013-04-10 08:46:23 +00:00
|
|
|
assert(ArrayObject::LengthPropertyIndex == internalClass->find(ctx->engine->id_length));
|
|
|
|
Property *lp = memberData + ArrayObject::LengthPropertyIndex;
|
|
|
|
cattrs = internalClass->propertyData.data() + ArrayObject::LengthPropertyIndex;
|
|
|
|
if (attrs.isEmpty() || p.isSubset(attrs, *lp, *cattrs))
|
2013-01-14 21:45:00 +00:00
|
|
|
return true;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!cattrs->isWritable() || attrs.type() == PropertyAttributes::Accessor || attrs.isConfigurable() || attrs.isEnumerable())
|
2013-01-10 22:22:04 +00:00
|
|
|
goto reject;
|
2013-01-14 21:45:00 +00:00
|
|
|
bool succeeded = true;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.type() == PropertyAttributes::Data) {
|
2013-01-11 13:33:10 +00:00
|
|
|
bool ok;
|
2013-04-10 08:46:23 +00:00
|
|
|
uint l = p.value.asArrayLength(ctx, &ok);
|
2013-01-11 13:33:10 +00:00
|
|
|
if (!ok)
|
2013-04-10 08:46:23 +00:00
|
|
|
ctx->throwRangeError(p.value);
|
2013-02-03 10:36:55 +00:00
|
|
|
succeeded = setArrayLength(l);
|
2013-01-11 13:33:10 +00:00
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.hasWritable() && !attrs.isWritable())
|
|
|
|
cattrs->setWritable(false);
|
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-03-08 08:38:46 +00:00
|
|
|
{
|
|
|
|
uint member = internalClass->find(name);
|
|
|
|
current = (member < UINT_MAX) ? memberData + member : 0;
|
2013-04-10 08:46:23 +00:00
|
|
|
cattrs = internalClass->propertyData.data() + member;
|
2013-03-08 08:38:46 +00:00
|
|
|
}
|
|
|
|
|
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-04-10 08:46:23 +00:00
|
|
|
Property *pd = insertMember(name, attrs);
|
|
|
|
*pd = p;
|
|
|
|
pd->fullyPopulated(&attrs);
|
2012-10-29 14:37:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
return __defineOwnProperty__(ctx, current, name, p, attrs);
|
2013-01-14 22:32:57 +00:00
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-14 22:32:57 +00:00
|
|
|
return false;
|
2012-05-25 10:54:36 +00:00
|
|
|
}
|
2012-05-20 17:59:47 +00:00
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, uint index, const Property &p, PropertyAttributes attrs)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *current = 0;
|
2013-01-14 22:32:57 +00:00
|
|
|
|
|
|
|
// 15.4.5.1, 4b
|
2013-04-10 08:46:23 +00:00
|
|
|
if (isArrayObject() && index >= arrayLength() && !internalClass->propertyData[ArrayObject::LengthPropertyIndex].isWritable())
|
2013-01-14 22:32:57 +00:00
|
|
|
goto reject;
|
|
|
|
|
2013-01-25 12:41:37 +00:00
|
|
|
if (isNonStrictArgumentsObject)
|
2013-04-10 08:46:23 +00:00
|
|
|
return static_cast<ArgumentsObject *>(this)->defineOwnProperty(ctx, index, p, attrs);
|
2013-01-22 21:23:59 +00:00
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
// Clause 1
|
2013-04-10 08:46:23 +00:00
|
|
|
{
|
|
|
|
uint pidx = propertyIndexFromArrayIndex(index);
|
|
|
|
if (pidx < UINT_MAX && (!arrayAttributes || !arrayAttributes[pidx].isGeneric()))
|
|
|
|
current = arrayData + pidx;
|
|
|
|
if (!current && isStringObject())
|
|
|
|
current = static_cast<StringObject *>(this)->getIndex(ctx, index);
|
|
|
|
}
|
2013-03-08 08:38:46 +00:00
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
if (!current) {
|
|
|
|
// clause 3
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
// clause 4
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *pd = arrayInsert(index, attrs);
|
|
|
|
*pd = p;
|
|
|
|
pd->fullyPopulated(&attrs);
|
2013-01-10 14:11:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
return __defineOwnProperty__(ctx, current, 0 /*member*/, p, attrs);
|
2013-01-14 22:32:57 +00:00
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-14 22:32:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, Property *current, String *member, const Property &p, PropertyAttributes attrs)
|
2013-01-14 22:32:57 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
// clause 5
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isEmpty())
|
2013-01-10 14:11:32 +00:00
|
|
|
return true;
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
PropertyAttributes cattrs = Attr_Data;
|
|
|
|
if (member)
|
|
|
|
cattrs = internalClass->propertyData[current - memberData];
|
|
|
|
else if (arrayAttributes)
|
|
|
|
cattrs = arrayAttributes[current - arrayData];
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
// clause 6
|
2013-04-10 08:46:23 +00:00
|
|
|
if (p.isSubset(attrs, *current, cattrs))
|
2013-01-10 14:11:32 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// clause 7
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!cattrs.isConfigurable()) {
|
|
|
|
if (attrs.isConfigurable())
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.hasEnumerable() && attrs.isEnumerable() != cattrs.isEnumerable())
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clause 8
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs.isGeneric())
|
2013-01-10 14:11:32 +00:00
|
|
|
goto accept;
|
|
|
|
|
|
|
|
// clause 9
|
2013-04-10 08:46:23 +00:00
|
|
|
if (cattrs.isData() != attrs.isData()) {
|
2013-01-10 14:11:32 +00:00
|
|
|
// 9a
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!cattrs.isConfigurable())
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (cattrs.isData()) {
|
2013-01-10 14:11:32 +00:00
|
|
|
// 9b
|
2013-04-10 08:46:23 +00:00
|
|
|
cattrs.setType(PropertyAttributes::Accessor);
|
|
|
|
cattrs.clearWritable();
|
|
|
|
current->setGetter(0);
|
|
|
|
current->setSetter(0);
|
2013-01-10 14:11:32 +00:00
|
|
|
} else {
|
|
|
|
// 9c
|
2013-04-10 08:46:23 +00:00
|
|
|
cattrs.setType(PropertyAttributes::Data);
|
|
|
|
cattrs.setWritable(false);
|
2013-01-10 14:11:32 +00:00
|
|
|
current->value = Value::undefinedValue();
|
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
} else if (cattrs.isData() && attrs.isData()) { // clause 10
|
|
|
|
if (!cattrs.isConfigurable() && !cattrs.isWritable()) {
|
|
|
|
if (attrs.isWritable() || !current->value.sameValue(p.value))
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
} else { // clause 10
|
2013-04-10 08:46:23 +00:00
|
|
|
assert(cattrs.isAccessor() && attrs.isAccessor());
|
|
|
|
if (!cattrs.isConfigurable()) {
|
|
|
|
if (p.getter() && !(current->getter() == p.getter() || (!current->getter() && (quintptr)p.getter() == 0x1)))
|
2013-01-16 10:00:56 +00:00
|
|
|
goto reject;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (p.setter() && !(current->setter() == p.setter() || (!current->setter() && (quintptr)p.setter() == 0x1)))
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accept:
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
current->merge(cattrs, p, attrs);
|
|
|
|
if (member) {
|
|
|
|
internalClass = internalClass->changeMember(member, cattrs);
|
|
|
|
} else {
|
|
|
|
if (cattrs != Attr_Data)
|
|
|
|
ensureArrayAttributes();
|
|
|
|
if (arrayAttributes)
|
|
|
|
arrayAttributes[current - arrayData] = cattrs;
|
|
|
|
}
|
2013-01-10 14:11:32 +00:00
|
|
|
return true;
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-10 14:11:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, const QString &name, const Property &p, PropertyAttributes attrs)
|
2012-12-11 22:58:40 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
return __defineOwnProperty__(ctx, ctx->engine->newString(name), p, attrs);
|
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;
|
2013-04-10 08:46:23 +00:00
|
|
|
memcpy(arrayData, other->arrayData, arrayDataLen*sizeof(Property));
|
2013-02-03 21:08:39 +00:00
|
|
|
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;
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
if (protoHasArray || o->arrayAttributes) {
|
2013-02-03 10:36:55 +00:00
|
|
|
// lets be safe and slow
|
|
|
|
for (uint i = fromIndex; i < endIndex; ++i) {
|
|
|
|
bool exists;
|
2013-03-07 11:25:59 +00:00
|
|
|
Value value = o->getIndexed(ctx, i, &exists);
|
2013-03-06 13:01:07 +00:00
|
|
|
if (exists && __qmljs_strict_equal(value, v, ctx))
|
2013-02-03 10:36:55 +00:00
|
|
|
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;
|
2013-04-10 08:46:23 +00:00
|
|
|
Value value = o->getValueChecked(ctx, arrayData + n->value, arrayAttributes ? arrayAttributes[n->value] : Attr_Data, &exists);
|
2013-03-06 13:01:07 +00:00
|
|
|
if (exists && __qmljs_strict_equal(value, v, ctx))
|
2013-02-03 10:36:55 +00:00
|
|
|
return Value::fromDouble(n->key());
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-03 21:08:39 +00:00
|
|
|
if ((int) endIndex > arrayDataLen)
|
|
|
|
endIndex = arrayDataLen;
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *pd = arrayData;
|
|
|
|
Property *end = pd + endIndex;
|
2013-02-03 10:36:55 +00:00
|
|
|
pd += fromIndex;
|
|
|
|
while (pd < end) {
|
|
|
|
bool exists;
|
2013-04-10 08:46:23 +00:00
|
|
|
Value value = o->getValueChecked(ctx, pd, arrayAttributes ? arrayAttributes[pd - arrayData] : Attr_Data, &exists);
|
2013-03-06 13:01:07 +00:00
|
|
|
if (exists && __qmljs_strict_equal(value, v, ctx))
|
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();
|
2013-04-10 08:46:23 +00:00
|
|
|
// ### copy attributes as well!
|
2013-02-03 10:36:55 +00:00
|
|
|
if (sparseArray) {
|
|
|
|
if (other->sparseArray) {
|
|
|
|
for (const SparseArrayNode *it = other->sparseArray->begin(); it != other->sparseArray->end(); it = it->nextNode())
|
2013-04-10 08:46:23 +00:00
|
|
|
arraySet(arrayDataLen + it->key(), other->arrayData + 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());
|
2013-04-10 08:46:23 +00:00
|
|
|
memcpy(arrayData + oldSize, other->arrayData, other->arrayLength()*sizeof(Property));
|
|
|
|
if (arrayAttributes)
|
|
|
|
std::fill(arrayAttributes + oldSize, arrayAttributes + oldSize + other->arrayLength(), PropertyAttributes(Attr_Data));
|
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) {
|
2013-04-10 08:46:23 +00:00
|
|
|
ensureArrayAttributes();
|
|
|
|
std::fill(arrayAttributes + arrayDataLen, arrayAttributes + oldSize, PropertyAttributes());
|
2013-02-03 21:08:39 +00:00
|
|
|
}
|
|
|
|
arrayDataLen = oldSize + other->arrayDataLen;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (other->arrayAttributes) {
|
|
|
|
for (int i = 0; i < arrayDataLen; ++i) {
|
|
|
|
bool exists;
|
|
|
|
arrayData[oldSize + i].value = const_cast<ArrayObject *>(other)->getIndexed(internalClass->engine->current, i, &exists);
|
|
|
|
if (arrayAttributes)
|
|
|
|
arrayAttributes[oldSize + i] = Attr_Data;
|
|
|
|
if (!exists) {
|
|
|
|
ensureArrayAttributes();
|
|
|
|
arrayAttributes[oldSize + i].clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(arrayData + oldSize, other->arrayData, other->arrayDataLen*sizeof(Property));
|
|
|
|
if (arrayAttributes)
|
|
|
|
std::fill(arrayAttributes + oldSize, arrayAttributes + oldSize + other->arrayDataLen, PropertyAttributes(Attr_Data));
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (len > arrayDataLen)
|
|
|
|
len = arrayDataLen;
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
// The spec says the sorting goes through a series of get,put and delete operations.
|
|
|
|
// this implies that the attributes don't get sorted around.
|
|
|
|
// behavior of accessor properties is implementation defined. We simply turn them all
|
|
|
|
// into data properties and then sort. This is in line with the sentence above.
|
|
|
|
if (arrayAttributes) {
|
|
|
|
for (uint i = 0; i < len; i++) {
|
|
|
|
if (arrayAttributes[i].isGeneric()) {
|
|
|
|
while (--len > i)
|
|
|
|
if (!arrayAttributes[len].isGeneric())
|
|
|
|
break;
|
|
|
|
arrayData[i].value = getValue(context, arrayData + len, arrayAttributes[len]);
|
|
|
|
arrayAttributes[i] = Attr_Data;
|
|
|
|
arrayAttributes[len].clear();
|
|
|
|
} else if (arrayAttributes[i].isAccessor()) {
|
|
|
|
arrayData[i].value = getValue(context, arrayData + i, arrayAttributes[i]);
|
|
|
|
arrayAttributes[i] = Attr_Data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayElementLessThan lessThan(context, thisObject, comparefn);
|
|
|
|
|
|
|
|
Property *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-04-10 08:46:23 +00:00
|
|
|
if (!arrayAttributes || !arrayAttributes[i].isGeneric()) {
|
2013-02-05 21:14:03 +00:00
|
|
|
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].value = Value::fromInt32(i + 1);
|
|
|
|
}
|
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].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);
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *newArrayData = new Property[arrayAlloc];
|
2013-03-01 14:02:46 +00:00
|
|
|
if (arrayData) {
|
2013-04-10 08:46:23 +00:00
|
|
|
memcpy(newArrayData, arrayData, sizeof(Property)*arrayDataLen);
|
2013-03-01 14:02:46 +00:00
|
|
|
delete [] (arrayData - off);
|
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
arrayData = newArrayData;
|
|
|
|
if (sparseArray) {
|
|
|
|
for (uint i = arrayFreeList; i < arrayAlloc; ++i) {
|
2013-04-10 08:46:23 +00:00
|
|
|
arrayData[i].value = Value::deletedValue();
|
2013-02-03 21:08:39 +00:00
|
|
|
arrayData[i].value = Value::fromInt32(i + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arrayOffset = 0;
|
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
|
|
|
|
if (arrayAttributes) {
|
|
|
|
PropertyAttributes *newAttrs = new PropertyAttributes[arrayAlloc];
|
|
|
|
memcpy(newAttrs, arrayAttributes, sizeof(PropertyAttributes)*arrayDataLen);
|
|
|
|
delete [] (arrayAttributes - off);
|
|
|
|
|
|
|
|
arrayAttributes = newAttrs;
|
|
|
|
if (sparseArray) {
|
|
|
|
for (uint i = arrayFreeList; i < arrayAlloc; ++i)
|
|
|
|
arrayAttributes[i] = Attr_Invalid;
|
|
|
|
}
|
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
void Object::ensureArrayAttributes()
|
|
|
|
{
|
|
|
|
if (arrayAttributes)
|
|
|
|
return;
|
|
|
|
|
|
|
|
arrayAttributes = new PropertyAttributes[arrayAlloc];
|
|
|
|
for (uint i = 0; i < arrayDataLen; ++i)
|
|
|
|
arrayAttributes[i] = Attr_Data;
|
|
|
|
for (uint i = arrayDataLen; i < arrayAlloc; ++i)
|
|
|
|
arrayAttributes[i] = Attr_Invalid;
|
|
|
|
}
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
bool Object::setArrayLength(uint newLen) {
|
|
|
|
assert(isArrayObject());
|
2013-04-10 08:46:23 +00:00
|
|
|
const Property *lengthProperty = memberData + ArrayObject::LengthPropertyIndex;
|
|
|
|
if (lengthProperty && !internalClass->propertyData[ArrayObject::LengthPropertyIndex].isWritable())
|
2013-02-03 10:36:55 +00:00
|
|
|
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) {
|
2013-04-10 08:46:23 +00:00
|
|
|
Property &pd = arrayData[it->value];
|
|
|
|
if (arrayAttributes) {
|
|
|
|
if (!arrayAttributes[it->value].isConfigurable()) {
|
|
|
|
ok = false;
|
|
|
|
newLen = it->key() + 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
arrayAttributes[it->value].clear();
|
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
pd.value.tag = Value::_Deleted_Type;
|
2013-02-03 21:08:39 +00:00
|
|
|
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-04-10 08:46:23 +00:00
|
|
|
Property *it = arrayData + arrayDataLen;
|
|
|
|
const Property *begin = arrayData + newLen;
|
2013-02-03 10:36:55 +00:00
|
|
|
while (--it >= begin) {
|
2013-04-10 08:46:23 +00:00
|
|
|
if (arrayAttributes) {
|
|
|
|
if (!arrayAttributes[it - arrayData].isEmpty() && !arrayAttributes[it - arrayData].isConfigurable()) {
|
|
|
|
ok = false;
|
|
|
|
newLen = it - arrayData + 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
arrayAttributes[it - arrayData].clear();
|
|
|
|
}
|
|
|
|
it->value = Value::deletedValue();
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2013-04-10 08:46:23 +00:00
|
|
|
const Property &pd = arrayData[i];
|
|
|
|
if (!arrayAttributes || arrayAttributes[i].isData()) {
|
2013-02-03 10:36:55 +00:00
|
|
|
if (Managed *m = pd.value.asManaged())
|
|
|
|
m->mark();
|
2013-04-10 08:46:23 +00:00
|
|
|
} else if (arrayAttributes[i].isAccessor()) {
|
|
|
|
if (pd.getter())
|
|
|
|
pd.getter()->mark();
|
|
|
|
if (pd.setter())
|
|
|
|
pd.setter()->mark();
|
2013-02-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-04 10:48:49 +00:00
|
|
|
internalClass = context->engine->arrayClass;
|
2013-01-25 12:13:06 +00:00
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
memberData = new Property[4];
|
|
|
|
memberData[LengthPropertyIndex].value = Value::fromInt32(0);
|
2013-01-11 13:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-14 13:07:57 +00:00
|
|
|
DEFINE_MANAGED_VTABLE(ForEachIteratorObject);
|
2013-01-11 13:33:10 +00:00
|
|
|
|
2013-02-13 22:00:10 +00:00
|
|
|
void ForEachIteratorObject::markObjects(Managed *that)
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2013-02-13 22:00:10 +00:00
|
|
|
ForEachIteratorObject *o = static_cast<ForEachIteratorObject *>(that);
|
|
|
|
Object::markObjects(that);
|
|
|
|
if (o->it.object)
|
|
|
|
o->it.object->mark();
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|