2013-01-21 21:03:14 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2013-01-21 21:03:14 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
2013-01-21 21:03:14 +00:00
|
|
|
**
|
|
|
|
** $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-04-15 09:50:16 +00:00
|
|
|
#include "qv4arrayobject_p.h"
|
|
|
|
#include "qv4sparsearray_p.h"
|
2013-08-21 17:06:10 +00:00
|
|
|
#include "qv4objectproto_p.h"
|
2013-09-05 11:22:23 +00:00
|
|
|
#include "qv4scopedvalue_p.h"
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
using namespace QV4;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-14 13:07:57 +00:00
|
|
|
DEFINE_MANAGED_VTABLE(ArrayCtor);
|
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
ArrayCtor::ArrayCtor(ExecutionContext *scope)
|
2013-09-19 14:05:25 +00:00
|
|
|
: FunctionObject(scope, QStringLiteral("Array"))
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-02-14 13:07:57 +00:00
|
|
|
vtbl = &static_vtbl;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 13:09:25 +00:00
|
|
|
ReturnedValue ArrayCtor::construct(Managed *m, CallData *callData)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-06-22 07:29:23 +00:00
|
|
|
ExecutionEngine *v4 = m->engine();
|
2013-09-14 09:25:02 +00:00
|
|
|
Scope scope(v4);
|
|
|
|
Scoped<ArrayObject> a(scope, v4->newArrayObject());
|
2013-02-03 10:36:55 +00:00
|
|
|
uint len;
|
2013-09-06 10:44:12 +00:00
|
|
|
if (callData->argc == 1 && callData->args[0].isNumber()) {
|
2013-01-21 21:03:14 +00:00
|
|
|
bool ok;
|
2013-09-06 10:44:12 +00:00
|
|
|
len = callData->args[0].asArrayLength(&ok);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-14 10:00:39 +00:00
|
|
|
if (!ok)
|
2013-10-21 07:57:58 +00:00
|
|
|
return v4->current->throwRangeError(callData->args[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-09-06 10:44:12 +00:00
|
|
|
len = callData->argc;
|
2013-02-03 21:08:39 +00:00
|
|
|
a->arrayReserve(len);
|
|
|
|
for (unsigned int i = 0; i < len; ++i)
|
2013-09-06 10:44:12 +00:00
|
|
|
a->arrayData[i].value = callData->args[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
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
return a.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
ReturnedValue ArrayCtor::call(Managed *that, CallData *callData)
|
2013-02-14 10:00:39 +00:00
|
|
|
{
|
2013-09-11 13:09:25 +00:00
|
|
|
return construct(that, callData);
|
2013-02-14 10:00:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 12:31:32 +00:00
|
|
|
ArrayPrototype::ArrayPrototype(InternalClass *ic)
|
|
|
|
: ArrayObject(ic)
|
2013-04-30 21:43:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-26 11:05:25 +00:00
|
|
|
void ArrayPrototype::init(ExecutionEngine *engine, ObjectRef ctor)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-26 11:05:25 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject o(scope);
|
|
|
|
ctor->defineReadonlyProperty(engine->id_length, Primitive::fromInt32(1));
|
|
|
|
ctor->defineReadonlyProperty(engine->id_prototype, (o = this));
|
|
|
|
ctor->defineDefaultProperty(QStringLiteral("isArray"), method_isArray, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("constructor"), (o = ctor));
|
2013-09-18 13:34:13 +00:00
|
|
|
defineDefaultProperty(engine->id_toString, method_toString, 0);
|
2013-09-18 10:31:55 +00:00
|
|
|
defineDefaultProperty(QStringLiteral("toLocaleString"), method_toLocaleString, 0);
|
|
|
|
defineDefaultProperty(QStringLiteral("concat"), method_concat, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("join"), method_join, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("pop"), method_pop, 0);
|
|
|
|
defineDefaultProperty(QStringLiteral("push"), method_push, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("reverse"), method_reverse, 0);
|
|
|
|
defineDefaultProperty(QStringLiteral("shift"), method_shift, 0);
|
|
|
|
defineDefaultProperty(QStringLiteral("slice"), method_slice, 2);
|
|
|
|
defineDefaultProperty(QStringLiteral("sort"), method_sort, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("splice"), method_splice, 2);
|
|
|
|
defineDefaultProperty(QStringLiteral("unshift"), method_unshift, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("indexOf"), method_indexOf, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("lastIndexOf"), method_lastIndexOf, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("every"), method_every, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("some"), method_some, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("forEach"), method_forEach, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("map"), method_map, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("filter"), method_filter, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("reduce"), method_reduce, 1);
|
|
|
|
defineDefaultProperty(QStringLiteral("reduceRight"), method_reduceRight, 1);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint ArrayPrototype::getLength(ExecutionContext *ctx, ObjectRef o)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-01-25 12:41:37 +00:00
|
|
|
if (o->isArrayObject())
|
2013-02-03 10:36:55 +00:00
|
|
|
return o->arrayLength();
|
2013-09-11 14:28:17 +00:00
|
|
|
Scope scope(ctx);
|
|
|
|
ScopedValue v(scope, o->get(ctx->engine->id_length));
|
|
|
|
return v->toUInt32();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_isArray(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-10-01 07:55:50 +00:00
|
|
|
bool isArray = ctx->callData->argc && ctx->callData->args[0].asArrayObject();
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(isArray);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_toString(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-18 13:34:13 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject o(scope, ctx->callData->thisObject, ScopedObject::Convert);
|
2013-10-21 07:57:58 +00:00
|
|
|
if (ctx->engine->hasException)
|
|
|
|
return Encode::undefined();
|
2013-09-18 13:34:13 +00:00
|
|
|
ScopedString s(scope, ctx->engine->newString("join"));
|
|
|
|
ScopedFunctionObject f(scope, o->get(s));
|
|
|
|
if (!!f) {
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData d(scope, 0);
|
2013-09-25 08:09:26 +00:00
|
|
|
d->thisObject = ctx->callData->thisObject;
|
2013-09-12 09:13:03 +00:00
|
|
|
return f->call(d);
|
2013-08-21 17:06:10 +00:00
|
|
|
}
|
|
|
|
return ObjectPrototype::method_toString(ctx);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_toLocaleString(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
|
|
|
return method_toString(ctx);
|
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_concat(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-14 09:25:02 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-27 15:04:42 +00:00
|
|
|
ScopedObject result(scope, ctx->engine->newArrayObject());
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject thisObject(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!thisObject)
|
|
|
|
return Encode::undefined();
|
2013-09-19 10:43:12 +00:00
|
|
|
ScopedArrayObject instance(scope, thisObject);
|
|
|
|
if (instance) {
|
|
|
|
result->copyArrayData(instance.getPointer());
|
2013-08-13 08:43:15 +00:00
|
|
|
} else {
|
2013-09-19 10:55:36 +00:00
|
|
|
result->arraySet(0, thisObject);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
ScopedArrayObject elt(scope);
|
2013-09-25 08:09:26 +00:00
|
|
|
for (uint i = 0; i < ctx->callData->argc; ++i) {
|
|
|
|
elt = ctx->callData->args[i];
|
2013-09-20 13:13:14 +00:00
|
|
|
if (elt)
|
|
|
|
result->arrayConcat(elt.getPointer());
|
2013-01-21 21:03:14 +00:00
|
|
|
else
|
2013-09-27 15:04:42 +00:00
|
|
|
result->arraySet(getLength(ctx, result), ctx->callData->args[i]);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
return result.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_join(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 14:28:17 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-16 20:02:27 +00:00
|
|
|
ScopedValue arg(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
QString r4;
|
2013-09-16 20:02:27 +00:00
|
|
|
if (arg->isUndefined())
|
2013-01-21 21:03:14 +00:00
|
|
|
r4 = QStringLiteral(",");
|
|
|
|
else
|
2013-09-16 20:02:27 +00:00
|
|
|
r4 = arg->toQString();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject self(scope, ctx->callData->thisObject);
|
2013-09-11 14:28:17 +00:00
|
|
|
ScopedValue length(scope, self->get(ctx->engine->id_length));
|
2013-09-20 13:13:14 +00:00
|
|
|
const quint32 r2 = length->isUndefined() ? 0 : length->toUInt32();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
if (!r2)
|
|
|
|
return ctx->engine->newString(QString())->asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
QString R;
|
|
|
|
|
|
|
|
// ### FIXME
|
2013-09-11 14:28:17 +00:00
|
|
|
if (ArrayObject *a = self->asArrayObject()) {
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue e(scope);
|
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;
|
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
e = a->getIndexed(i);
|
|
|
|
if (!e->isNullOrUndefined())
|
|
|
|
R += e->toString(ctx)->toQString();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// crazy!
|
|
|
|
//
|
2013-09-18 13:34:13 +00:00
|
|
|
ScopedString name(scope, ctx->engine->newString(QStringLiteral("0")));
|
|
|
|
ScopedValue r6(scope, self->get(name));
|
2013-09-11 14:28:17 +00:00
|
|
|
if (!r6->isNullOrUndefined())
|
|
|
|
R = r6->toString(ctx)->toQString();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 14:28:17 +00:00
|
|
|
ScopedValue r12(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (quint32 k = 1; k < r2; ++k) {
|
|
|
|
R += r4;
|
|
|
|
|
2013-09-25 10:24:36 +00:00
|
|
|
name = Primitive::fromDouble(k).toString(ctx);
|
2013-09-11 14:28:17 +00:00
|
|
|
r12 = self->get(name);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 14:28:17 +00:00
|
|
|
if (!r12->isNullOrUndefined())
|
|
|
|
R += r12->toString(ctx)->toQString();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
return ctx->engine->newString(R)->asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_pop(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 19:48:23 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +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-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromInt32(0)));
|
2013-09-20 13:13:14 +00:00
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue result(scope, instance->getIndexed(len - 1));
|
2013-01-22 09:57:38 +00:00
|
|
|
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(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
|
2013-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromDouble(len - 1)));
|
2013-09-12 09:13:03 +00:00
|
|
|
return result.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_push(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-18 14:36:02 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-22 09:57:38 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
if (len + ctx->callData->argc < len) {
|
2013-01-22 09:57:38 +00:00
|
|
|
// ughh...
|
|
|
|
double l = len;
|
2013-09-20 13:13:14 +00:00
|
|
|
ScopedString s(scope);
|
2013-09-25 08:09:26 +00:00
|
|
|
for (int i = 0; i < ctx->callData->argc; ++i) {
|
2013-09-25 10:24:36 +00:00
|
|
|
s = Primitive::fromDouble(l + i).toString(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
instance->put(s, ctx->callData->args[i]);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-25 08:09:26 +00:00
|
|
|
double newLen = l + ctx->callData->argc;
|
2013-01-25 12:41:37 +00:00
|
|
|
if (!instance->isArrayObject())
|
2013-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromDouble(newLen)));
|
2013-09-25 20:42:58 +00:00
|
|
|
else {
|
|
|
|
ScopedString str(scope, ctx->engine->newString(QStringLiteral("Array.prototype.push: Overflow")));
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwRangeError(str);
|
2013-09-25 20:42:58 +00:00
|
|
|
}
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(newLen);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 13:54:15 +00:00
|
|
|
if (!instance->protoHasArray() && instance->arrayDataLen <= len) {
|
2013-09-25 08:09:26 +00:00
|
|
|
for (uint i = 0; i < ctx->callData->argc; ++i) {
|
2013-02-03 21:08:39 +00:00
|
|
|
if (!instance->sparseArray) {
|
|
|
|
if (len >= instance->arrayAlloc)
|
|
|
|
instance->arrayReserve(len + 1);
|
2013-09-25 08:09:26 +00:00
|
|
|
instance->arrayData[len].value = ctx->callData->args[i];
|
2013-04-10 08:46:23 +00:00
|
|
|
if (instance->arrayAttributes)
|
|
|
|
instance->arrayAttributes[len] = Attr_Data;
|
2013-02-03 21:08:39 +00:00
|
|
|
instance->arrayDataLen = len + 1;
|
|
|
|
} else {
|
2013-09-25 08:09:26 +00:00
|
|
|
uint i = instance->allocArrayValue(ctx->callData->args[i]);
|
2013-02-03 21:08:39 +00:00
|
|
|
instance->sparseArray->push_back(i, len);
|
|
|
|
}
|
|
|
|
++len;
|
2013-01-22 09:57:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-25 08:09:26 +00:00
|
|
|
for (uint i = 0; i < ctx->callData->argc; ++i)
|
|
|
|
instance->putIndexed(len + i, ctx->callData->args[i]);
|
|
|
|
len += ctx->callData->argc;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
if (instance->isArrayObject())
|
|
|
|
instance->setArrayLengthUnchecked(len);
|
|
|
|
else
|
2013-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromDouble(len)));
|
2013-01-22 09:57:38 +00:00
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
return Encode(len);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_reverse(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 19:48:23 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +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
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue lval(scope);
|
|
|
|
ScopedValue hval(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (; lo < hi; ++lo, --hi) {
|
2013-01-22 21:43:07 +00:00
|
|
|
bool loExists, hiExists;
|
2013-09-11 19:48:23 +00:00
|
|
|
lval = instance->getIndexed(lo, &loExists);
|
|
|
|
hval = instance->getIndexed(hi, &hiExists);
|
2013-01-22 21:43:07 +00:00
|
|
|
if (hiExists)
|
2013-06-21 21:42:08 +00:00
|
|
|
instance->putIndexed(lo, hval);
|
2013-01-22 21:43:07 +00:00
|
|
|
else
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(lo);
|
2013-01-22 21:43:07 +00:00
|
|
|
if (loExists)
|
2013-06-21 21:42:08 +00:00
|
|
|
instance->putIndexed(hi, lval);
|
2013-01-22 21:43:07 +00:00
|
|
|
else
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(hi);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-20 13:13:14 +00:00
|
|
|
return instance.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_shift(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 19:48:23 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-22 09:24:58 +00:00
|
|
|
|
|
|
|
if (!len) {
|
2013-01-25 12:41:37 +00:00
|
|
|
if (!instance->isArrayObject())
|
2013-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromInt32(0)));
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode::undefined();
|
2013-01-22 09:24:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *front = 0;
|
|
|
|
uint pidx = instance->propertyIndexFromArrayIndex(0);
|
2013-10-13 20:08:59 +00:00
|
|
|
if (pidx < UINT_MAX && !instance->arrayData[pidx].value.isEmpty())
|
2013-04-10 08:46:23 +00:00
|
|
|
front = instance->arrayData + pidx;
|
2013-02-05 21:13:27 +00:00
|
|
|
|
2013-09-25 13:24:50 +00:00
|
|
|
ScopedValue result(scope, front ? instance->getValue(front, instance->arrayAttributes ? instance->arrayAttributes[pidx] : Attr_Data) : Encode::undefined());
|
2013-01-22 09:24:58 +00:00
|
|
|
|
2013-08-15 13:54:15 +00:00
|
|
|
if (!instance->protoHasArray() && instance->arrayDataLen <= len) {
|
2013-02-03 21:08:39 +00:00
|
|
|
if (!instance->sparseArray) {
|
|
|
|
if (instance->arrayDataLen) {
|
|
|
|
++instance->arrayOffset;
|
|
|
|
++instance->arrayData;
|
|
|
|
--instance->arrayDataLen;
|
|
|
|
--instance->arrayAlloc;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (instance->arrayAttributes)
|
|
|
|
++instance->arrayAttributes;
|
2013-02-03 21:08:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint idx = instance->sparseArray->pop_front();
|
|
|
|
instance->freeArrayValue(idx);
|
|
|
|
}
|
2013-01-22 09:24:58 +00:00
|
|
|
} else {
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-22 09:24:58 +00:00
|
|
|
// do it the slow way
|
|
|
|
for (uint k = 1; k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-01-22 09:24:58 +00:00
|
|
|
if (exists)
|
2013-06-21 21:42:08 +00:00
|
|
|
instance->putIndexed(k - 1, v);
|
2013-01-22 09:24:58 +00:00
|
|
|
else
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(k - 1);
|
2013-01-22 09:24:58 +00:00
|
|
|
}
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(len - 1);
|
2013-01-22 09:24:58 +00:00
|
|
|
}
|
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-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromDouble(len - 1)));
|
2013-09-12 09:13:03 +00:00
|
|
|
return result.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_slice(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 19:48:23 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject o(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!o)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
Scoped<ArrayObject> result(scope, ctx->engine->newArrayObject());
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, o);
|
2013-09-16 20:02:27 +00:00
|
|
|
double s = ScopedValue(scope, ctx->argument(0))->toInteger();
|
2013-01-21 21:03:14 +00:00
|
|
|
uint start;
|
|
|
|
if (s < 0)
|
|
|
|
start = (uint)qMax(len + s, 0.);
|
|
|
|
else if (s > len)
|
|
|
|
start = len;
|
|
|
|
else
|
|
|
|
start = (uint) s;
|
|
|
|
uint end = len;
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc > 1 && !ctx->callData->args[1].isUndefined()) {
|
|
|
|
double e = ctx->callData->args[1].toInteger();
|
2013-01-21 21:03:14 +00:00
|
|
|
if (e < 0)
|
|
|
|
end = (uint)qMax(len + e, 0.);
|
|
|
|
else if (e > len)
|
|
|
|
end = len;
|
|
|
|
else
|
|
|
|
end = (uint) e;
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
uint n = 0;
|
|
|
|
for (uint i = start; i < end; ++i) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = o->getIndexed(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-09-14 09:25:02 +00:00
|
|
|
return result.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_sort(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-16 20:02:27 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
ScopedValue comparefn(scope, ctx->argument(0));
|
2013-09-19 11:17:55 +00:00
|
|
|
instance->arraySort(ctx, instance, comparefn, len);
|
2013-09-25 08:09:26 +00:00
|
|
|
return ctx->callData->thisObject.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_splice(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 19:48:23 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
Scoped<ArrayObject> newArray(scope, ctx->engine->newArrayObject());
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
double rs = ScopedValue(scope, ctx->argument(0))->toInteger();
|
2013-01-21 21:03:14 +00:00
|
|
|
uint start;
|
|
|
|
if (rs < 0)
|
|
|
|
start = (uint) qMax(0., len + rs);
|
|
|
|
else
|
|
|
|
start = (uint) qMin(rs, (double)len);
|
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
uint deleteCount = (uint)qMin(qMax(ScopedValue(scope, ctx->argument(1))->toInteger(), 0.), (double)(len - start));
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-02-03 21:08:39 +00:00
|
|
|
newArray->arrayReserve(deleteCount);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint i = 0; i < deleteCount; ++i) {
|
2013-09-25 13:24:50 +00:00
|
|
|
newArray->arrayData[i].value = instance->getIndexed(start + i);
|
2013-09-20 13:13:14 +00:00
|
|
|
newArray->arrayDataLen = i + 1;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-02-03 10:36:55 +00:00
|
|
|
newArray->setArrayLengthUnchecked(deleteCount);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
uint itemCount = ctx->callData->argc < 2 ? 0 : ctx->callData->argc - 2;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (itemCount < deleteCount) {
|
|
|
|
for (uint k = start; k < len - deleteCount; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k + deleteCount, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (exists)
|
2013-06-13 12:22:49 +00:00
|
|
|
instance->putIndexed(k + itemCount, v);
|
2013-01-21 21:03:14 +00:00
|
|
|
else
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(k + itemCount);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
for (uint k = len; k > len - deleteCount + itemCount; --k)
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(k - 1);
|
2013-01-21 21:03:14 +00:00
|
|
|
} else if (itemCount > deleteCount) {
|
|
|
|
uint k = len - deleteCount;
|
|
|
|
while (k > start) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k + deleteCount - 1, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (exists)
|
2013-06-13 12:22:49 +00:00
|
|
|
instance->putIndexed(k + itemCount - 1, v);
|
2013-01-21 21:03:14 +00:00
|
|
|
else
|
2013-06-21 18:33:39 +00:00
|
|
|
instance->deleteIndexedProperty(k + itemCount - 1);
|
2013-01-21 21:03:14 +00:00
|
|
|
--k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < itemCount; ++i)
|
2013-09-25 08:09:26 +00:00
|
|
|
instance->putIndexed(start + i, ctx->callData->args[i + 2]);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
ctx->strictMode = true;
|
2013-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromDouble(len - deleteCount + itemCount)));
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
return newArray.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_unshift(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 19:48:23 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-22 09:24:58 +00:00
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
ScopedValue v(scope);
|
2013-08-15 13:54:15 +00:00
|
|
|
if (!instance->protoHasArray() && instance->arrayDataLen <= len) {
|
2013-09-25 08:09:26 +00:00
|
|
|
for (int i = ctx->callData->argc - 1; i >= 0; --i) {
|
2013-09-16 20:02:27 +00:00
|
|
|
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;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (instance->arrayAttributes) {
|
|
|
|
--instance->arrayAttributes;
|
|
|
|
*instance->arrayAttributes = Attr_Data;
|
|
|
|
}
|
2013-09-30 20:41:12 +00:00
|
|
|
instance->arrayData->value = v.asReturnedValue();
|
2013-02-03 21:08:39 +00:00
|
|
|
} 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;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k - 1, &exists);
|
2013-01-22 09:24:58 +00:00
|
|
|
if (exists)
|
2013-09-25 08:09:26 +00:00
|
|
|
instance->putIndexed(k + ctx->callData->argc - 1, v);
|
2013-01-22 09:24:58 +00:00
|
|
|
else
|
2013-09-25 08:09:26 +00:00
|
|
|
instance->deleteIndexedProperty(k + ctx->callData->argc - 1);
|
2013-01-22 09:24:58 +00:00
|
|
|
}
|
2013-09-25 08:09:26 +00:00
|
|
|
for (uint i = 0; i < ctx->callData->argc; ++i)
|
|
|
|
instance->putIndexed(i, ctx->callData->args[i]);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-02-03 21:08:39 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
uint newLen = len + ctx->callData->argc;
|
2013-02-03 21:08:39 +00:00
|
|
|
if (instance->isArrayObject())
|
|
|
|
instance->setArrayLengthUnchecked(newLen);
|
|
|
|
else
|
2013-09-25 10:24:36 +00:00
|
|
|
instance->put(ctx->engine->id_length, ScopedValue(scope, Primitive::fromDouble(newLen)));
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
return Encode(newLen);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_indexOf(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-09 11:38:10 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!len)
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(-1);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
ScopedValue searchValue(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
uint fromIndex = 0;
|
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc >= 1)
|
|
|
|
searchValue = ctx->callData->args[0];
|
2013-01-21 21:03:14 +00:00
|
|
|
else
|
2013-09-25 10:24:36 +00:00
|
|
|
searchValue = Primitive::undefinedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc >= 2) {
|
|
|
|
double f = ctx->callData->args[1].toInteger();
|
2013-01-21 21:03:14 +00:00
|
|
|
if (f >= len)
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(-1);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (f < 0)
|
|
|
|
f = qMax(len + f, 0.);
|
|
|
|
fromIndex = (uint) f;
|
|
|
|
}
|
|
|
|
|
2013-01-25 12:41:37 +00:00
|
|
|
if (instance->isStringObject()) {
|
2013-09-09 11:38:10 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint k = fromIndex; k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-09 11:38:10 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-04-14 20:01:20 +00:00
|
|
|
if (exists && __qmljs_strict_equal(v, searchValue))
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(k);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(-1);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:13:14 +00:00
|
|
|
return instance->arrayIndexOf(searchValue, fromIndex, len, ctx, instance.getPointer());
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_lastIndexOf(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-09 11:38:10 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
ScopedObject instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!len)
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(-1);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
ScopedValue searchValue(scope);
|
2013-01-21 22:07:22 +00:00
|
|
|
uint fromIndex = len;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc >= 1)
|
2013-01-21 21:03:14 +00:00
|
|
|
searchValue = ctx->argument(0);
|
|
|
|
else
|
2013-09-25 10:24:36 +00:00
|
|
|
searchValue = Primitive::undefinedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc >= 2) {
|
|
|
|
double f = ctx->callData->args[1].toInteger();
|
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)
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(-1);
|
2013-01-21 22:07:22 +00:00
|
|
|
}
|
|
|
|
fromIndex = (uint) f + 1;
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
ScopedValue v(scope);
|
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;
|
2013-09-09 11:38:10 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-04-14 20:01:20 +00:00
|
|
|
if (exists && __qmljs_strict_equal(v, searchValue))
|
2013-09-20 13:13:14 +00:00
|
|
|
return Encode(k);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(-1);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_every(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 3);
|
2013-09-30 20:41:12 +00:00
|
|
|
callData->args[2] = instance;
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->thisObject = ctx->argument(1);
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedValue r(scope);
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-09-11 12:36:01 +00:00
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
bool ok = true;
|
|
|
|
for (uint k = 0; ok && k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[1] = Primitive::fromDouble(k);
|
2013-09-11 12:36:01 +00:00
|
|
|
r = callback->call(callData);
|
|
|
|
ok = r->toBoolean();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-20 13:13:14 +00:00
|
|
|
return Encode(ok);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_some(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 3);
|
|
|
|
callData->thisObject = ctx->argument(1);
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->args[2] = instance;
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-25 13:24:50 +00:00
|
|
|
ScopedValue r(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[1] = Primitive::fromDouble(k);
|
2013-09-25 13:24:50 +00:00
|
|
|
r = callback->call(callData);
|
|
|
|
if (r->toBoolean())
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(true);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode(false);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_forEach(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 3);
|
|
|
|
callData->thisObject = ctx->argument(1);
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->args[2] = instance;
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[1] = Primitive::fromDouble(k);
|
2013-09-05 11:22:23 +00:00
|
|
|
callback->call(callData);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-16 20:02:27 +00:00
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_map(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
Scoped<ArrayObject> a(scope, ctx->engine->newArrayObject());
|
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
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
ScopedValue mapped(scope);
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 3);
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->thisObject = ctx->argument(1);
|
|
|
|
callData->args[2] = instance;
|
2013-09-11 11:55:01 +00:00
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-01-21 21:03:14 +00:00
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[1] = Primitive::fromDouble(k);
|
2013-09-11 11:55:01 +00:00
|
|
|
mapped = callback->call(callData);
|
2013-02-03 10:36:55 +00:00
|
|
|
a->arraySet(k, mapped);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
2013-09-14 09:25:02 +00:00
|
|
|
return a.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_filter(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-14 09:25:02 +00:00
|
|
|
Scoped<ArrayObject> a(scope, ctx->engine->newArrayObject());
|
2013-02-03 21:08:39 +00:00
|
|
|
a->arrayReserve(len);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
ScopedValue selected(scope);
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 3);
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->thisObject = ctx->argument(1);
|
|
|
|
callData->args[2] = instance;
|
2013-09-11 11:55:01 +00:00
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
uint to = 0;
|
|
|
|
for (uint k = 0; k < len; ++k) {
|
|
|
|
bool exists;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &exists);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!exists)
|
|
|
|
continue;
|
|
|
|
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[1] = Primitive::fromDouble(k);
|
2013-09-11 11:55:01 +00:00
|
|
|
selected = callback->call(callData);
|
|
|
|
if (selected->toBoolean()) {
|
2013-02-03 10:36:55 +00:00
|
|
|
a->arraySet(to, v);
|
2013-01-21 21:03:14 +00:00
|
|
|
++to;
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 09:25:02 +00:00
|
|
|
return a.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_reduce(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
uint k = 0;
|
2013-09-11 11:55:01 +00:00
|
|
|
ScopedValue acc(scope);
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
|
|
|
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc > 1) {
|
2013-01-21 21:03:14 +00:00
|
|
|
acc = ctx->argument(1);
|
|
|
|
} else {
|
|
|
|
bool kPresent = false;
|
|
|
|
while (k < len && !kPresent) {
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &kPresent);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (kPresent)
|
|
|
|
acc = v;
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
if (!kPresent)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 4);
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->thisObject = Primitive::undefinedValue();
|
2013-09-11 11:55:01 +00:00
|
|
|
callData->args[0] = acc;
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->args[3] = instance;
|
2013-09-11 11:55:01 +00:00
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
while (k < len) {
|
|
|
|
bool kPresent;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k, &kPresent);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (kPresent) {
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = acc;
|
|
|
|
callData->args[1] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[2] = Primitive::fromDouble(k);
|
2013-09-05 11:22:23 +00:00
|
|
|
acc = callback->call(callData);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
++k;
|
|
|
|
}
|
2013-09-12 09:13:03 +00:00
|
|
|
return acc.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
ReturnedValue ArrayPrototype::method_reduceRight(SimpleCallContext *ctx)
|
2013-01-21 21:03:14 +00:00
|
|
|
{
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(ctx);
|
2013-09-25 08:09:26 +00:00
|
|
|
Scoped<Object> instance(scope, ctx->callData->thisObject.toObject(ctx));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!instance)
|
|
|
|
return Encode::undefined();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
uint len = getLength(ctx, instance);
|
2013-01-21 21:03:14 +00:00
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
Scoped<FunctionObject> callback(scope, ctx->argument(0));
|
2013-01-21 21:03:14 +00:00
|
|
|
if (!callback)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
|
|
|
|
if (len == 0) {
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc == 1)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-09-16 20:02:27 +00:00
|
|
|
return ctx->argument(1);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint k = len;
|
2013-09-11 11:55:01 +00:00
|
|
|
ScopedValue acc(scope);
|
2013-09-11 19:48:23 +00:00
|
|
|
ScopedValue v(scope);
|
2013-09-25 08:09:26 +00:00
|
|
|
if (ctx->callData->argc > 1) {
|
2013-01-21 21:03:14 +00:00
|
|
|
acc = ctx->argument(1);
|
|
|
|
} else {
|
|
|
|
bool kPresent = false;
|
|
|
|
while (k > 0 && !kPresent) {
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k - 1, &kPresent);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (kPresent)
|
|
|
|
acc = v;
|
|
|
|
--k;
|
|
|
|
}
|
|
|
|
if (!kPresent)
|
2013-10-21 07:57:58 +00:00
|
|
|
return ctx->throwTypeError();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 4);
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->thisObject = Primitive::undefinedValue();
|
2013-09-16 20:02:27 +00:00
|
|
|
callData->args[3] = instance;
|
2013-09-11 11:55:01 +00:00
|
|
|
|
2013-01-21 21:03:14 +00:00
|
|
|
while (k > 0) {
|
|
|
|
bool kPresent;
|
2013-09-11 19:48:23 +00:00
|
|
|
v = instance->getIndexed(k - 1, &kPresent);
|
2013-01-21 21:03:14 +00:00
|
|
|
if (kPresent) {
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = acc;
|
|
|
|
callData->args[1] = v;
|
2013-09-25 10:24:36 +00:00
|
|
|
callData->args[2] = Primitive::fromDouble(k - 1);
|
2013-09-05 11:22:23 +00:00
|
|
|
acc = callback->call(callData);
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
--k;
|
|
|
|
}
|
2013-09-12 09:13:03 +00:00
|
|
|
return acc.asReturnedValue();
|
2013-01-21 21:03:14 +00:00
|
|
|
}
|
|
|
|
|