2013-01-21 21:03:14 +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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qv4arrayobject.h"
|
2013-02-03 10:43:38 +00:00
|
|
|
#include "qv4sparsearray.h"
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
using namespace QQmlJS::VM;
|
|
|
|
|
2013-02-14 13:07:57 +00:00
|
|
|
DEFINE_MANAGED_VTABLE(ArrayCtor);
|
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
ArrayCtor::ArrayCtor(ExecutionContext *scope)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
{
|
2013-02-14 13:07:57 +00:00
|
|
|
vtbl = &static_vtbl;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:07:57 +00:00
|
|
|
Value ArrayCtor::construct(Managed *, ExecutionContext *ctx, Value *argv, int argc)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
|
|
|
ArrayObject *a = ctx->engine->newArrayObject(ctx);
|
2013-02-03 10:36:55 +00:00
|
|
|
uint len;
|
2013-02-14 10:00:39 +00:00
|
|
|
if (argc == 1 && argv[0].isNumber()) {
|
2013-01-21 21:03:14 +00:00
|
|
|
bool ok;
|
2013-02-14 10:00:39 +00:00
|
|
|
len = argv[0].asArrayLength(ctx, &ok);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-14 10:00:39 +00:00
|
|
|
if (!ok)
|
|
|
|
ctx->throwRangeError(argv[0]);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (len < 0x1000)
|
|
|
|
a->arrayReserve(len);
|
2013-01-21 21:03:14 +00:00
|
|
|
} else {
|
2013-02-14 10:00:39 +00:00
|
|
|
len = argc;
|
2013-02-03 21:08:39 +00:00
|
|
|
a->arrayReserve(len);
|
|
|
|
for (unsigned int i = 0; i < len; ++i)
|
2013-02-14 10:00:39 +00:00
|
|
|
fillDescriptor(a->arrayData + i, argv[i]);
|
2013-02-03 21:08:39 +00:00
|
|
|
a->arrayDataLen = len;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
a->setArrayLengthUnchecked(len);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
return Value::fromObject(a);
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:07:57 +00:00
|
|
|
Value ArrayCtor::call(Managed *that, ExecutionContext *ctx, const Value &thisObject, Value *argv, int argc)
|
2013-02-14 10:00:39 +00:00
|
|
|
{
|
2013-02-14 13:07:57 +00:00
|
|
|
return construct(that, ctx, argv, argc);
|
2013-02-14 10:00:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
void ArrayPrototype::init(ExecutionContext *ctx, const Value &ctor)
|
|
|
|
{
|
|
|
|
ctor.objectValue()->defineReadonlyProperty(ctx->engine->id_length, Value::fromInt32(1));
|
2013-02-10 21:22:53 +00:00
|
|
|
ctor.objectValue()->defineReadonlyProperty(ctx->engine->id_prototype, Value::fromObject(this));
|
2013-01-21 21:03:14 +00:00
|
|
|
ctor.objectValue()->defineDefaultProperty(ctx, QStringLiteral("isArray"), method_isArray, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("constructor"), ctor);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("toString"), method_toString, 0);
|
2013-01-22 13:06:22 +00:00
|
|
|
defineDefaultProperty(ctx, QStringLiteral("toLocaleString"), method_toLocaleString, 0);
|
2013-01-21 21:03:14 +00:00
|
|
|
defineDefaultProperty(ctx, QStringLiteral("concat"), method_concat, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("join"), method_join, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("pop"), method_pop, 0);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("push"), method_push, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("reverse"), method_reverse, 0);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("shift"), method_shift, 0);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("slice"), method_slice, 2);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("sort"), method_sort, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("splice"), method_splice, 2);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("unshift"), method_unshift, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("indexOf"), method_indexOf, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("lastIndexOf"), method_lastIndexOf, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("every"), method_every, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("some"), method_some, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("forEach"), method_forEach, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("map"), method_map, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("filter"), method_filter, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("reduce"), method_reduce, 1);
|
|
|
|
defineDefaultProperty(ctx, QStringLiteral("reduceRight"), method_reduceRight, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint ArrayPrototype::getLength(ExecutionContext *ctx, Object *o)
|
|
|
|
{
|
2013-01-25 12:41:37 +00:00
|
|
|
if (o->isArrayObject())
|
2013-02-03 10:36:55 +00:00
|
|
|
return o->arrayLength();
|
2013-01-21 21:03:14 +00:00
|
|
|
return o->__get__(ctx, ctx->engine->id_length).toUInt32(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_isArray(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
Value arg = ctx->argument(0);
|
|
|
|
bool isArray = arg.asArrayObject();
|
|
|
|
return Value::fromBoolean(isArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_toString(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
return method_join(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_toLocaleString(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
return method_toString(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_concat(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-03 10:36:55 +00:00
|
|
|
ArrayObject *result = ctx->engine->newArrayObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
if (ArrayObject *instance = ctx->thisObject.asArrayObject()) {
|
|
|
|
result->copyArrayData(instance);
|
|
|
|
} else {
|
2013-01-21 21:03:14 +00:00
|
|
|
QString v = ctx->thisObject.toString(ctx)->toQString();
|
2013-02-03 10:36:55 +00:00
|
|
|
result->arraySet(0, Value::fromString(ctx, v));
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < ctx->argumentCount; ++i) {
|
|
|
|
Value arg = ctx->argument(i);
|
|
|
|
|
|
|
|
if (ArrayObject *elt = arg.asArrayObject())
|
2013-02-03 10:36:55 +00:00
|
|
|
result->arrayConcat(elt);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
else
|
2013-02-03 21:08:39 +00:00
|
|
|
result->arraySet(getLength(ctx, result), arg);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
return Value::fromObject(result);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_join(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
Value arg = ctx->argument(0);
|
|
|
|
|
|
|
|
QString r4;
|
|
|
|
if (arg.isUndefined())
|
|
|
|
r4 = QStringLiteral(",");
|
|
|
|
else
|
|
|
|
r4 = arg.toString(ctx)->toQString();
|
|
|
|
|
|
|
|
Value self = ctx->thisObject;
|
|
|
|
const Value length = self.property(ctx, ctx->engine->id_length);
|
|
|
|
const quint32 r2 = Value::toUInt32(length.isUndefined() ? 0 : length.toNumber(ctx));
|
|
|
|
|
|
|
|
static QSet<Object *> visitedArrayElements;
|
|
|
|
|
|
|
|
if (! r2 || visitedArrayElements.contains(self.objectValue()))
|
|
|
|
return Value::fromString(ctx, QString());
|
|
|
|
|
|
|
|
// avoid infinite recursion
|
|
|
|
visitedArrayElements.insert(self.objectValue());
|
|
|
|
|
|
|
|
QString R;
|
|
|
|
|
|
|
|
// ### FIXME
|
|
|
|
if (ArrayObject *a = self.asArrayObject()) {
|
2013-02-03 10:36:55 +00:00
|
|
|
for (uint i = 0; i < a->arrayLength(); ++i) {
|
2013-01-21 21:03:14 +00:00
|
|
|
if (i)
|
|
|
|
R += r4;
|
|
|
|
|
|
|
|
Value e = a->__get__(ctx, i);
|
|
|
|
if (! (e.isUndefined() || e.isNull()))
|
|
|
|
R += e.toString(ctx)->toQString();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// crazy!
|
|
|
|
//
|
2013-01-30 13:56:40 +00:00
|
|
|
Value r6 = self.property(ctx, ctx->engine->newString(QStringLiteral("0")));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!(r6.isUndefined() || r6.isNull()))
|
|
|
|
R = r6.toString(ctx)->toQString();
|
|
|
|
|
|
|
|
for (quint32 k = 1; k < r2; ++k) {
|
|
|
|
R += r4;
|
|
|
|
|
|
|
|
String *name = Value::fromDouble(k).toString(ctx);
|
|
|
|
Value r12 = self.property(ctx, name);
|
|
|
|
|
|
|
|
if (! (r12.isUndefined() || r12.isNull()))
|
|
|
|
R += r12.toString(ctx)->toQString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visitedArrayElements.remove(self.objectValue());
|
|
|
|
return Value::fromString(ctx, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_pop(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-22 09:57:38 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-01-22 09:57:38 +00:00
|
|
|
if (!len) {
|
2013-01-25 12:41:37 +00:00
|
|
|
if (!instance->isArrayObject())
|
2013-01-22 09:57:38 +00:00
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromInt32(0));
|
|
|
|
return Value::undefinedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 09:57:38 +00:00
|
|
|
Value result = instance->__get__(ctx, len - 1);
|
|
|
|
|
|
|
|
instance->__delete__(ctx, len - 1);
|
2013-01-25 12:41:37 +00:00
|
|
|
if (instance->isArrayObject())
|
2013-02-03 10:36:55 +00:00
|
|
|
instance->setArrayLengthUnchecked(len - 1);
|
2013-01-22 09:57:38 +00:00
|
|
|
else
|
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromDouble(len - 1));
|
|
|
|
return result;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_push(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-22 09:57:38 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
if (len + ctx->argumentCount < len) {
|
|
|
|
// ughh...
|
|
|
|
double l = len;
|
|
|
|
for (double i = 0; i < ctx->argumentCount; ++i) {
|
|
|
|
Value idx = Value::fromDouble(l + i);
|
|
|
|
instance->__put__(ctx, idx.toString(ctx), ctx->argument(i));
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-01-22 09:57:38 +00:00
|
|
|
double newLen = l + ctx->argumentCount;
|
2013-01-25 12:41:37 +00:00
|
|
|
if (!instance->isArrayObject())
|
2013-01-22 09:57:38 +00:00
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromDouble(newLen));
|
|
|
|
else
|
|
|
|
ctx->throwRangeError(Value::fromString(ctx, QStringLiteral("Array.prototype.push: Overflow")));
|
|
|
|
return Value::fromDouble(newLen);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 09:57:38 +00:00
|
|
|
bool protoHasArray = false;
|
|
|
|
Object *p = instance;
|
|
|
|
while ((p = p->prototype))
|
2013-02-03 21:08:39 +00:00
|
|
|
if (p->arrayDataLen)
|
2013-01-22 09:57:38 +00:00
|
|
|
protoHasArray = true;
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (!protoHasArray && instance->arrayDataLen <= len) {
|
2013-01-22 09:57:38 +00:00
|
|
|
for (uint i = 0; i < ctx->argumentCount; ++i) {
|
|
|
|
Value v = ctx->argument(i);
|
2013-02-03 21:08:39 +00:00
|
|
|
|
|
|
|
if (!instance->sparseArray) {
|
|
|
|
if (len >= instance->arrayAlloc)
|
|
|
|
instance->arrayReserve(len + 1);
|
|
|
|
fillDescriptor(instance->arrayData + len, v);
|
|
|
|
instance->arrayDataLen = len + 1;
|
|
|
|
} else {
|
|
|
|
uint i = instance->allocArrayValue(v);
|
|
|
|
instance->sparseArray->push_back(i, len);
|
|
|
|
}
|
|
|
|
++len;
|
2013-01-22 09:57:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint i = 0; i < ctx->argumentCount; ++i)
|
|
|
|
instance->__put__(ctx, len + i, ctx->argument(i));
|
2013-02-03 21:08:39 +00:00
|
|
|
len += ctx->argumentCount;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
if (instance->isArrayObject())
|
|
|
|
instance->setArrayLengthUnchecked(len);
|
|
|
|
else
|
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromDouble(len));
|
2013-01-22 09:57:38 +00:00
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (len < INT_MAX)
|
|
|
|
return Value::fromInt32(len);
|
|
|
|
return Value::fromDouble((double)len);
|
2013-01-22 09:57:38 +00:00
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_reverse(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-22 21:43:07 +00:00
|
|
|
uint length = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-01-22 21:43:07 +00:00
|
|
|
int lo = 0, hi = length - 1;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
for (; lo < hi; ++lo, --hi) {
|
2013-01-22 21:43:07 +00:00
|
|
|
bool loExists, hiExists;
|
|
|
|
Value lval = instance->__get__(ctx, lo, &loExists);
|
|
|
|
Value hval = instance->__get__(ctx, hi, &hiExists);
|
|
|
|
if (hiExists)
|
|
|
|
instance->__put__(ctx, lo, hval);
|
|
|
|
else
|
|
|
|
instance->__delete__(ctx, lo);
|
|
|
|
if (loExists)
|
|
|
|
instance->__put__(ctx, hi, lval);
|
|
|
|
else
|
|
|
|
instance->__delete__(ctx, hi);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-01-22 21:43:07 +00:00
|
|
|
return Value::fromObject(instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_shift(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-22 09:24:58 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
if (!len) {
|
2013-01-25 12:41:37 +00:00
|
|
|
if (!instance->isArrayObject())
|
2013-01-22 09:24:58 +00:00
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromInt32(0));
|
|
|
|
return Value::undefinedValue();
|
|
|
|
}
|
|
|
|
|
2013-02-05 21:13:27 +00:00
|
|
|
PropertyDescriptor *front = 0;
|
|
|
|
if (!instance->sparseArray) {
|
|
|
|
if (instance->arrayDataLen)
|
|
|
|
front = instance->arrayData;
|
|
|
|
} else {
|
|
|
|
SparseArrayNode *n = instance->sparseArray->findNode(0);
|
|
|
|
if (n)
|
|
|
|
front = instance->arrayDecriptor(n->value);
|
|
|
|
}
|
|
|
|
if (front && front->type == PropertyDescriptor::Generic)
|
|
|
|
front = 0;
|
|
|
|
|
|
|
|
Value result = instance->getValueChecked(ctx, front);
|
2013-01-22 09:24:58 +00:00
|
|
|
|
|
|
|
bool protoHasArray = false;
|
|
|
|
Object *p = instance;
|
|
|
|
while ((p = p->prototype))
|
2013-02-03 21:08:39 +00:00
|
|
|
if (p->arrayDataLen)
|
2013-01-22 09:24:58 +00:00
|
|
|
protoHasArray = true;
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (!protoHasArray && instance->arrayDataLen <= len) {
|
|
|
|
if (!instance->sparseArray) {
|
|
|
|
if (instance->arrayDataLen) {
|
|
|
|
++instance->arrayOffset;
|
|
|
|
++instance->arrayData;
|
|
|
|
--instance->arrayDataLen;
|
|
|
|
--instance->arrayAlloc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint idx = instance->sparseArray->pop_front();
|
|
|
|
instance->freeArrayValue(idx);
|
|
|
|
}
|
2013-01-22 09:24:58 +00:00
|
|
|
} else {
|
|
|
|
// do it the slow way
|
|
|
|
for (uint k = 1; k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (exists)
|
|
|
|
instance->__put__(ctx, k - 1, v);
|
|
|
|
else
|
|
|
|
instance->__delete__(ctx, k - 1);
|
|
|
|
}
|
|
|
|
instance->__delete__(ctx, len - 1);
|
|
|
|
}
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (instance->isArrayObject())
|
|
|
|
instance->setArrayLengthUnchecked(len - 1);
|
|
|
|
else
|
2013-01-22 09:24:58 +00:00
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromDouble(len - 1));
|
|
|
|
return result;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_slice(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *o = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
ArrayObject *result = ctx->engine->newArrayObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
uint len = o->__get__(ctx, ctx->engine->id_length).toUInt32(ctx);
|
|
|
|
double s = ctx->argument(0).toInteger(ctx);
|
|
|
|
uint start;
|
|
|
|
if (s < 0)
|
|
|
|
start = (uint)qMax(len + s, 0.);
|
|
|
|
else if (s > len)
|
|
|
|
start = len;
|
|
|
|
else
|
|
|
|
start = (uint) s;
|
|
|
|
uint end = len;
|
|
|
|
if (!ctx->argument(1).isUndefined()) {
|
|
|
|
double e = ctx->argument(1).toInteger(ctx);
|
|
|
|
if (e < 0)
|
|
|
|
end = (uint)qMax(len + e, 0.);
|
|
|
|
else if (e > len)
|
|
|
|
end = len;
|
|
|
|
else
|
|
|
|
end = (uint) e;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint n = 0;
|
|
|
|
for (uint i = start; i < end; ++i) {
|
|
|
|
bool exists;
|
|
|
|
Value v = o->__get__(ctx, i, &exists);
|
2013-02-03 21:08:39 +00:00
|
|
|
if (exists) {
|
2013-02-03 10:36:55 +00:00
|
|
|
result->arraySet(n, v);
|
2013-02-03 21:08:39 +00:00
|
|
|
}
|
2013-01-21 21:03:14 +00:00
|
|
|
++n;
|
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
return Value::fromObject(result);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_sort(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
Value comparefn = ctx->argument(0);
|
2013-02-03 10:36:55 +00:00
|
|
|
instance->arraySort(ctx, instance, comparefn, len);
|
2013-01-21 21:03:14 +00:00
|
|
|
return ctx->thisObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_splice(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
ArrayObject *newArray = ctx->engine->newArrayObject(ctx);
|
|
|
|
|
|
|
|
double rs = ctx->argument(0).toInteger(ctx);
|
|
|
|
uint start;
|
|
|
|
if (rs < 0)
|
|
|
|
start = (uint) qMax(0., len + rs);
|
|
|
|
else
|
|
|
|
start = (uint) qMin(rs, (double)len);
|
|
|
|
|
|
|
|
uint deleteCount = (uint)qMin(qMax(ctx->argument(1).toInteger(ctx), 0.), (double)(len - start));
|
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
newArray->arrayReserve(deleteCount);
|
|
|
|
PropertyDescriptor *pd = newArray->arrayData;
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint i = 0; i < deleteCount; ++i) {
|
|
|
|
pd->type = PropertyDescriptor::Data;
|
|
|
|
pd->writable = PropertyDescriptor::Enabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Enabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Enabled;
|
|
|
|
pd->value = instance->__get__(ctx, start + i);
|
|
|
|
++pd;
|
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
newArray->arrayDataLen = deleteCount;
|
2013-02-03 10:36:55 +00:00
|
|
|
newArray->setArrayLengthUnchecked(deleteCount);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint itemCount = ctx->argumentCount < 2 ? 0 : ctx->argumentCount - 2;
|
|
|
|
|
|
|
|
if (itemCount < deleteCount) {
|
|
|
|
for (uint k = start; k < len - deleteCount; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k + deleteCount, &exists);
|
|
|
|
if (exists)
|
2013-02-03 10:36:55 +00:00
|
|
|
instance->arraySet(k + itemCount, v);
|
2013-01-21 21:03:14 +00:00
|
|
|
else
|
|
|
|
instance->__delete__(ctx, k + itemCount);
|
|
|
|
}
|
|
|
|
for (uint k = len; k > len - deleteCount + itemCount; --k)
|
|
|
|
instance->__delete__(ctx, k - 1);
|
|
|
|
} else if (itemCount > deleteCount) {
|
|
|
|
uint k = len - deleteCount;
|
|
|
|
while (k > start) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k + deleteCount - 1, &exists);
|
|
|
|
if (exists)
|
2013-02-03 10:36:55 +00:00
|
|
|
instance->arraySet(k + itemCount - 1, v);
|
2013-01-21 21:03:14 +00:00
|
|
|
else
|
|
|
|
instance->__delete__(ctx, k + itemCount - 1);
|
|
|
|
--k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < itemCount; ++i)
|
2013-02-03 10:36:55 +00:00
|
|
|
instance->arraySet(start + i, ctx->argument(i + 2));
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
ctx->strictMode = true;
|
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromDouble(len - deleteCount + itemCount));
|
|
|
|
|
|
|
|
return Value::fromObject(newArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_unshift(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-22 09:24:58 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
bool protoHasArray = false;
|
|
|
|
Object *p = instance;
|
|
|
|
while ((p = p->prototype))
|
2013-02-03 21:08:39 +00:00
|
|
|
if (p->arrayDataLen)
|
2013-01-22 09:24:58 +00:00
|
|
|
protoHasArray = true;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
if (!protoHasArray && instance->arrayDataLen <= len) {
|
2013-01-22 09:24:58 +00:00
|
|
|
for (int i = ctx->argumentCount - 1; i >= 0; --i) {
|
|
|
|
Value v = ctx->argument(i);
|
2013-02-03 21:08:39 +00:00
|
|
|
|
|
|
|
if (!instance->sparseArray) {
|
|
|
|
if (!instance->arrayOffset)
|
|
|
|
instance->getArrayHeadRoom();
|
|
|
|
|
|
|
|
--instance->arrayOffset;
|
|
|
|
--instance->arrayData;
|
|
|
|
++instance->arrayDataLen;
|
|
|
|
fillDescriptor(instance->arrayData, v);
|
|
|
|
} else {
|
|
|
|
uint idx = instance->allocArrayValue(v);
|
|
|
|
instance->sparseArray->push_front(idx);
|
|
|
|
}
|
2013-01-22 09:24:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint k = len; k > 0; --k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k - 1, &exists);
|
|
|
|
if (exists)
|
|
|
|
instance->__put__(ctx, k + ctx->argumentCount - 1, v);
|
|
|
|
else
|
|
|
|
instance->__delete__(ctx, k + ctx->argumentCount - 1);
|
|
|
|
}
|
|
|
|
for (uint i = 0; i < ctx->argumentCount; ++i)
|
|
|
|
instance->__put__(ctx, i, ctx->argument(i));
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
|
2013-01-22 09:24:58 +00:00
|
|
|
uint newLen = len + ctx->argumentCount;
|
2013-02-03 21:08:39 +00:00
|
|
|
if (instance->isArrayObject())
|
|
|
|
instance->setArrayLengthUnchecked(newLen);
|
|
|
|
else
|
2013-01-22 09:24:58 +00:00
|
|
|
instance->__put__(ctx, ctx->engine->id_length, Value::fromDouble(newLen));
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-01-22 09:24:58 +00:00
|
|
|
if (newLen < INT_MAX)
|
|
|
|
return Value::fromInt32(newLen);
|
|
|
|
return Value::fromDouble((double)newLen);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_indexOf(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
if (!len)
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
|
|
|
|
Value searchValue;
|
|
|
|
uint fromIndex = 0;
|
|
|
|
|
|
|
|
if (ctx->argumentCount >= 1)
|
|
|
|
searchValue = ctx->argument(0);
|
|
|
|
else
|
|
|
|
searchValue = Value::undefinedValue();
|
|
|
|
|
|
|
|
if (ctx->argumentCount >= 2) {
|
|
|
|
double f = ctx->argument(1).toInteger(ctx);
|
|
|
|
if (f >= len)
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
if (f < 0)
|
|
|
|
f = qMax(len + f, 0.);
|
|
|
|
fromIndex = (uint) f;
|
|
|
|
}
|
|
|
|
|
2013-01-25 12:41:37 +00:00
|
|
|
if (instance->isStringObject()) {
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint k = fromIndex; k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (exists && __qmljs_strict_equal(v, searchValue))
|
|
|
|
return Value::fromDouble(k);
|
|
|
|
}
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
}
|
|
|
|
|
2013-02-03 10:36:55 +00:00
|
|
|
return instance->arrayIndexOf(searchValue, fromIndex, len, ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_lastIndexOf(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
if (!len)
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
|
|
|
|
Value searchValue;
|
2013-01-21 22:07:22 +00:00
|
|
|
uint fromIndex = len;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
if (ctx->argumentCount >= 1)
|
|
|
|
searchValue = ctx->argument(0);
|
|
|
|
else
|
|
|
|
searchValue = Value::undefinedValue();
|
|
|
|
|
|
|
|
if (ctx->argumentCount >= 2) {
|
|
|
|
double f = ctx->argument(1).toInteger(ctx);
|
2013-01-21 22:07:22 +00:00
|
|
|
if (f > 0)
|
|
|
|
f = qMin(f, (double)(len - 1));
|
|
|
|
else if (f < 0) {
|
|
|
|
f = len + f;
|
|
|
|
if (f < 0)
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
}
|
|
|
|
fromIndex = (uint) f + 1;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 22:07:22 +00:00
|
|
|
for (uint k = fromIndex; k > 0;) {
|
|
|
|
--k;
|
2013-01-21 21:03:14 +00:00
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (exists && __qmljs_strict_equal(v, searchValue))
|
|
|
|
return Value::fromDouble(k);
|
|
|
|
}
|
|
|
|
return Value::fromInt32(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_every(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
Value thisArg = ctx->argument(1);
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
for (uint k = 0; ok && k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value args[3];
|
|
|
|
args[0] = v;
|
|
|
|
args[1] = Value::fromDouble(k);
|
|
|
|
args[2] = ctx->thisObject;
|
|
|
|
Value r = callback->call(ctx, thisArg, args, 3);
|
|
|
|
ok = __qmljs_to_boolean(r, ctx);
|
|
|
|
}
|
|
|
|
return Value::fromBoolean(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_some(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
Value thisArg = ctx->argument(1);
|
|
|
|
|
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value args[3];
|
|
|
|
args[0] = v;
|
|
|
|
args[1] = Value::fromDouble(k);
|
|
|
|
args[2] = ctx->thisObject;
|
|
|
|
Value r = callback->call(ctx, thisArg, args, 3);
|
|
|
|
if (__qmljs_to_boolean(r, ctx))
|
|
|
|
return Value::fromBoolean(true);
|
|
|
|
}
|
|
|
|
return Value::fromBoolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_forEach(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
Value thisArg = ctx->argument(1);
|
|
|
|
|
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value args[3];
|
|
|
|
args[0] = v;
|
|
|
|
args[1] = Value::fromDouble(k);
|
|
|
|
args[2] = ctx->thisObject;
|
|
|
|
callback->call(ctx, thisArg, args, 3);
|
|
|
|
}
|
|
|
|
return Value::undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_map(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
Value thisArg = ctx->argument(1);
|
|
|
|
|
|
|
|
ArrayObject *a = ctx->engine->newArrayObject(ctx);
|
2013-02-03 21:08:39 +00:00
|
|
|
a->arrayReserve(len);
|
2013-02-03 10:36:55 +00:00
|
|
|
a->setArrayLengthUnchecked(len);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value args[3];
|
|
|
|
args[0] = v;
|
|
|
|
args[1] = Value::fromDouble(k);
|
|
|
|
args[2] = ctx->thisObject;
|
|
|
|
Value mapped = callback->call(ctx, thisArg, args, 3);
|
2013-02-03 10:36:55 +00:00
|
|
|
a->arraySet(k, mapped);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
return Value::fromObject(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_filter(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
Value thisArg = ctx->argument(1);
|
|
|
|
|
|
|
|
ArrayObject *a = ctx->engine->newArrayObject(ctx);
|
2013-02-03 21:08:39 +00:00
|
|
|
a->arrayReserve(len);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint to = 0;
|
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
|
|
|
Value v = instance->__get__(ctx, k, &exists);
|
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value args[3];
|
|
|
|
args[0] = v;
|
|
|
|
args[1] = Value::fromDouble(k);
|
|
|
|
args[2] = ctx->thisObject;
|
|
|
|
Value selected = callback->call(ctx, thisArg, args, 3);
|
|
|
|
if (__qmljs_to_boolean(selected, ctx)) {
|
2013-02-03 10:36:55 +00:00
|
|
|
a->arraySet(to, v);
|
2013-01-21 21:03:14 +00:00
|
|
|
++to;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Value::fromObject(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_reduce(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint k = 0;
|
|
|
|
Value acc;
|
|
|
|
if (ctx->argumentCount > 1) {
|
|
|
|
acc = ctx->argument(1);
|
|
|
|
} else {
|
|
|
|
bool kPresent = false;
|
|
|
|
while (k < len && !kPresent) {
|
|
|
|
Value v = instance->__get__(ctx, k, &kPresent);
|
|
|
|
if (kPresent)
|
|
|
|
acc = v;
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
if (!kPresent)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (k < len) {
|
|
|
|
bool kPresent;
|
|
|
|
Value v = instance->__get__(ctx, k, &kPresent);
|
|
|
|
if (kPresent) {
|
|
|
|
Value args[4];
|
|
|
|
args[0] = acc;
|
|
|
|
args[1] = v;
|
|
|
|
args[2] = Value::fromDouble(k);
|
|
|
|
args[3] = ctx->thisObject;
|
|
|
|
acc = callback->call(ctx, Value::undefinedValue(), args, 4);
|
|
|
|
}
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayPrototype::method_reduceRight(ExecutionContext *ctx)
|
|
|
|
{
|
2013-02-14 20:41:49 +00:00
|
|
|
Object *instance = ctx->thisObject.toObject(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint len = getLength(ctx, instance);
|
|
|
|
|
|
|
|
FunctionObject *callback = ctx->argument(0).asFunctionObject();
|
|
|
|
if (!callback)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
if (ctx->argumentCount == 1)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
return ctx->argument(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint k = len;
|
|
|
|
Value acc;
|
|
|
|
if (ctx->argumentCount > 1) {
|
|
|
|
acc = ctx->argument(1);
|
|
|
|
} else {
|
|
|
|
bool kPresent = false;
|
|
|
|
while (k > 0 && !kPresent) {
|
|
|
|
Value v = instance->__get__(ctx, k - 1, &kPresent);
|
|
|
|
if (kPresent)
|
|
|
|
acc = v;
|
|
|
|
--k;
|
|
|
|
}
|
|
|
|
if (!kPresent)
|
2013-02-14 22:04:12 +00:00
|
|
|
ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (k > 0) {
|
|
|
|
bool kPresent;
|
|
|
|
Value v = instance->__get__(ctx, k - 1, &kPresent);
|
|
|
|
if (kPresent) {
|
|
|
|
Value args[4];
|
|
|
|
args[0] = acc;
|
|
|
|
args[1] = v;
|
|
|
|
args[2] = Value::fromDouble(k - 1);
|
|
|
|
args[3] = ctx->thisObject;
|
|
|
|
acc = callback->call(ctx, Value::undefinedValue(), args, 4);
|
|
|
|
}
|
|
|
|
--k;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|