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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-04-16 19:23:25 +00:00
|
|
|
#include "qmljs_objects.h"
|
2012-05-07 14:05:05 +00:00
|
|
|
#include "qv4ir_p.h"
|
2012-11-19 12:15:25 +00:00
|
|
|
#include "qv4isel_p.h"
|
2012-05-14 15:12:25 +00:00
|
|
|
#include "qv4ecmaobjects_p.h"
|
2012-12-04 12:40:18 +00:00
|
|
|
#include "qv4mm.h"
|
2012-11-16 21:21:34 +00:00
|
|
|
|
|
|
|
#include <private/qqmljsengine_p.h>
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
#include <qv4ir_p.h>
|
|
|
|
#include <qv4codegen_p.h>
|
2013-01-09 13:37:55 +00:00
|
|
|
#include "private/qlocale_tools_p.h"
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
#include <QtCore/qmath.h>
|
2012-05-07 14:05:05 +00:00
|
|
|
#include <QtCore/QDebug>
|
2012-04-16 19:23:25 +00:00
|
|
|
#include <cassert>
|
2012-05-25 15:45:15 +00:00
|
|
|
#include <typeinfo>
|
2012-11-16 21:21:34 +00:00
|
|
|
#include <iostream>
|
2013-01-13 22:57:35 +00:00
|
|
|
#include <alloca.h>
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-05-04 13:28:04 +00:00
|
|
|
using namespace QQmlJS::VM;
|
|
|
|
|
2013-01-03 21:17:46 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
//
|
|
|
|
// Object
|
|
|
|
//
|
2012-05-04 13:12:37 +00:00
|
|
|
Object::~Object()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
void Object::__put__(ExecutionContext *ctx, const QString &name, const Value &value)
|
2012-05-14 14:03:10 +00:00
|
|
|
{
|
2012-10-29 14:37:02 +00:00
|
|
|
__put__(ctx, ctx->engine->identifier(name), value);
|
2012-05-14 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 13:19:35 +00:00
|
|
|
Value Object::getValue(ExecutionContext *ctx, const PropertyDescriptor *p) const
|
2012-11-10 22:35:06 +00:00
|
|
|
{
|
|
|
|
if (p->isData())
|
|
|
|
return p->value;
|
|
|
|
if (!p->get)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
|
2012-11-18 23:00:50 +00:00
|
|
|
return p->get->call(ctx, Value::fromObject(const_cast<Object *>(this)), 0, 0);
|
2012-11-10 22:35:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 13:19:35 +00:00
|
|
|
Value Object::getValueChecked(ExecutionContext *ctx, const PropertyDescriptor *p) const
|
|
|
|
{
|
|
|
|
if (!p || p->type == PropertyDescriptor::Generic)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
return getValue(ctx, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::getValueChecked(ExecutionContext *ctx, const PropertyDescriptor *p, bool *exists) const
|
|
|
|
{
|
|
|
|
*exists = p && p->type != PropertyDescriptor::Generic;
|
|
|
|
if (!*exists)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
return getValue(ctx, p);
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
bool Object::inplaceBinOp(Value rhs, String *name, BinOp op, ExecutionContext *ctx)
|
2012-11-10 22:35:06 +00:00
|
|
|
{
|
2012-12-14 09:25:27 +00:00
|
|
|
bool hasProperty = false;
|
|
|
|
Value v = __get__(ctx, name, &hasProperty);
|
|
|
|
if (!hasProperty)
|
2012-11-10 22:35:06 +00:00
|
|
|
return false;
|
2012-12-14 09:25:27 +00:00
|
|
|
Value result = op(v, rhs, ctx);
|
2012-11-10 22:35:06 +00:00
|
|
|
__put__(ctx, name, result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
bool Object::inplaceBinOp(Value rhs, Value index, BinOp op, ExecutionContext *ctx)
|
2012-11-11 20:56:38 +00:00
|
|
|
{
|
2013-01-11 09:19:29 +00:00
|
|
|
uint idx = index.asArrayIndex();
|
|
|
|
if (idx < UINT_MAX) {
|
|
|
|
bool hasProperty = false;
|
|
|
|
Value v = __get__(ctx, idx, &hasProperty);
|
|
|
|
if (!hasProperty)
|
|
|
|
return false;
|
|
|
|
v = op(v, rhs, ctx);
|
|
|
|
__put__(ctx, idx, v);
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-11 20:56:38 +00:00
|
|
|
String *name = index.toString(ctx);
|
|
|
|
assert(name);
|
2012-11-11 21:08:39 +00:00
|
|
|
return inplaceBinOp(rhs, name, op, ctx);
|
2012-11-11 20:56:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 23:53:04 +00:00
|
|
|
void Object::defineDefaultProperty(String *name, Value value)
|
|
|
|
{
|
|
|
|
if (!members)
|
2012-12-13 09:19:14 +00:00
|
|
|
members.reset(new PropertyTable());
|
2012-12-12 23:53:04 +00:00
|
|
|
PropertyDescriptor *pd = members->insert(name);
|
|
|
|
pd->type = PropertyDescriptor::Data;
|
2012-12-13 12:12:10 +00:00
|
|
|
pd->writable = PropertyDescriptor::Enabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Enabled;
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value value)
|
|
|
|
{
|
|
|
|
defineDefaultProperty(context->engine->identifier(name), value);
|
|
|
|
}
|
|
|
|
|
2013-01-10 15:06:43 +00:00
|
|
|
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value (*code)(ExecutionContext *), int argumentCount)
|
2012-12-12 23:53:04 +00:00
|
|
|
{
|
2013-01-10 15:06:43 +00:00
|
|
|
Q_UNUSED(argumentCount);
|
2012-12-12 23:53:04 +00:00
|
|
|
String *s = context->engine->identifier(name);
|
2013-01-10 15:06:43 +00:00
|
|
|
FunctionObject* function = context->engine->newNativeFunction(context, s, code);
|
2013-01-13 15:17:03 +00:00
|
|
|
function->defineReadonlyProperty(context->engine->id_length, Value::fromInt32(argumentCount));
|
2013-01-10 15:06:43 +00:00
|
|
|
defineDefaultProperty(s, Value::fromObject(function));
|
2012-12-12 23:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Object::defineReadonlyProperty(ExecutionEngine *engine, const QString &name, Value value)
|
2013-01-10 15:06:43 +00:00
|
|
|
{
|
2013-01-13 15:17:03 +00:00
|
|
|
defineReadonlyProperty(engine->identifier(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
|
|
|
{
|
|
|
|
if (!members)
|
2012-12-13 09:19:14 +00:00
|
|
|
members.reset(new PropertyTable());
|
2013-01-10 15:06:43 +00:00
|
|
|
PropertyDescriptor *pd = members->insert(name);
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->type = PropertyDescriptor::Data;
|
2012-12-13 12:12:10 +00:00
|
|
|
pd->writable = PropertyDescriptor::Disabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Disabled;
|
2012-12-12 23:53:04 +00:00
|
|
|
pd->value = value;
|
|
|
|
}
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
void Object::getCollectables(QVector<Object *> &objects)
|
|
|
|
{
|
|
|
|
if (prototype)
|
|
|
|
objects.append(prototype);
|
|
|
|
|
|
|
|
if (members) {
|
|
|
|
for (PropertyTable::iterator it = members->begin(), eit = members->end(); it < eit; ++it) {
|
2013-01-16 11:18:47 +00:00
|
|
|
if (!(*it))
|
|
|
|
continue;
|
|
|
|
PropertyDescriptor &pd = (*it)->descriptor;
|
|
|
|
if (pd.isData()) {
|
|
|
|
if (Object *o = pd.value.asObject())
|
2012-12-04 12:40:18 +00:00
|
|
|
objects.append(o);
|
2013-01-16 11:18:47 +00:00
|
|
|
} else if (pd.isAccessor()) {
|
|
|
|
if (pd.get)
|
|
|
|
objects.append(pd.get);
|
|
|
|
if (pd.set)
|
|
|
|
objects.append(pd.set);
|
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 21:52:51 +00:00
|
|
|
array.getCollectables(objects);
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 20:56:15 +00:00
|
|
|
// Section 8.12.1
|
2013-01-10 16:33:06 +00:00
|
|
|
PropertyDescriptor *Object::__getOwnProperty__(ExecutionContext *ctx, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 16:33:06 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __getOwnProperty__(ctx, idx);
|
|
|
|
|
2012-10-28 20:56:15 +00:00
|
|
|
if (members)
|
|
|
|
return members->find(name);
|
2012-04-16 19:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:34:46 +00:00
|
|
|
PropertyDescriptor *Object::__getOwnProperty__(ExecutionContext *ctx, uint index)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
|
|
|
PropertyDescriptor *p = array.at(index);
|
|
|
|
if(p && p->type != PropertyDescriptor::Generic)
|
|
|
|
return p;
|
2013-01-16 12:34:46 +00:00
|
|
|
if (isString)
|
|
|
|
return static_cast<StringObject *>(this)->getIndex(ctx, index);
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.2
|
2012-12-14 09:25:27 +00:00
|
|
|
PropertyDescriptor *Object::__getPropertyDescriptor__(ExecutionContext *ctx, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 16:33:06 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __getPropertyDescriptor__(ctx, idx);
|
|
|
|
|
2012-12-18 08:06:03 +00:00
|
|
|
|
2012-12-17 08:52:04 +00:00
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
|
|
|
if (o->members) {
|
|
|
|
if (PropertyDescriptor *p = o->members->find(name))
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
o = o->prototype;
|
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:34:46 +00:00
|
|
|
PropertyDescriptor *Object::__getPropertyDescriptor__(ExecutionContext *ctx, uint index)
|
2013-01-10 14:11:32 +00:00
|
|
|
{
|
|
|
|
Object *o = this;
|
|
|
|
while (o) {
|
2013-01-14 11:21:30 +00:00
|
|
|
PropertyDescriptor *p = o->array.at(index);
|
2013-01-10 14:11:32 +00:00
|
|
|
if(p && p->type != PropertyDescriptor::Generic)
|
|
|
|
return p;
|
2013-01-16 12:34:46 +00:00
|
|
|
if (o->isString) {
|
|
|
|
p = static_cast<StringObject *>(o)->getIndex(ctx, index);
|
|
|
|
if (p)
|
|
|
|
return p;
|
|
|
|
}
|
2013-01-10 14:11:32 +00:00
|
|
|
o = o->prototype;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.3
|
2012-12-14 08:57:02 +00:00
|
|
|
Value Object::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __get__(ctx, idx, hasProperty);
|
|
|
|
|
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
|
|
|
|
2012-12-14 09:25:27 +00:00
|
|
|
if (PropertyDescriptor *p = __getPropertyDescriptor__(ctx, name)) {
|
2012-12-14 08:57:02 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2012-11-10 22:35:06 +00:00
|
|
|
return getValue(ctx, p);
|
2012-12-14 08:57:02 +00:00
|
|
|
}
|
2012-11-10 22:35:06 +00:00
|
|
|
|
2012-12-14 08:57:02 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
2012-10-29 14:37:02 +00:00
|
|
|
return Value::undefinedValue();
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
Value Object::__get__(ExecutionContext *ctx, uint index, bool *hasProperty)
|
|
|
|
{
|
|
|
|
if (PropertyDescriptor *p = __getPropertyDescriptor__(ctx, index)) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
|
|
|
return getValue(ctx, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
|
|
|
return Value::undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-28 20:56:15 +00:00
|
|
|
// Section 8.12.4
|
2012-11-16 22:07:10 +00:00
|
|
|
bool Object::__canPut__(ExecutionContext *ctx, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __canPut__(ctx, idx);
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
if (PropertyDescriptor *p = __getOwnProperty__(ctx, name)) {
|
2012-10-28 20:56:15 +00:00
|
|
|
if (p->isAccessor())
|
2012-11-28 21:45:43 +00:00
|
|
|
return p->set != 0;
|
2012-10-28 20:56:15 +00:00
|
|
|
return p->isWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! prototype)
|
2012-04-16 19:23:25 +00:00
|
|
|
return extensible;
|
2012-10-28 20:56:15 +00:00
|
|
|
|
2012-12-14 09:25:27 +00:00
|
|
|
if (PropertyDescriptor *p = prototype->__getPropertyDescriptor__(ctx, name)) {
|
2012-10-28 20:56:15 +00:00
|
|
|
if (p->isAccessor())
|
2012-11-28 21:45:43 +00:00
|
|
|
return p->set != 0;
|
2012-10-28 20:56:15 +00:00
|
|
|
if (!extensible)
|
|
|
|
return false;
|
|
|
|
return p->isWritable();
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
2012-12-17 08:52:04 +00:00
|
|
|
|
|
|
|
return extensible;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
bool Object::__canPut__(ExecutionContext *ctx, uint index)
|
|
|
|
{
|
|
|
|
if (PropertyDescriptor *p = __getOwnProperty__(ctx, index)) {
|
|
|
|
if (p->isAccessor())
|
|
|
|
return p->set != 0;
|
|
|
|
return p->isWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! prototype)
|
|
|
|
return extensible;
|
|
|
|
|
|
|
|
if (PropertyDescriptor *p = prototype->__getPropertyDescriptor__(ctx, index)) {
|
|
|
|
if (p->isAccessor())
|
|
|
|
return p->set != 0;
|
|
|
|
if (!extensible)
|
|
|
|
return false;
|
|
|
|
return p->isWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return extensible;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.5
|
2012-11-27 23:12:33 +00:00
|
|
|
void Object::__put__(ExecutionContext *ctx, String *name, Value value)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __put__(ctx, idx, value);
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// clause 1
|
|
|
|
if (!__canPut__(ctx, name))
|
|
|
|
goto reject;
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
if (!members)
|
2012-12-13 09:19:14 +00:00
|
|
|
members.reset(new PropertyTable());
|
2012-10-29 14:37:02 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Clause 2
|
|
|
|
PropertyDescriptor *pd = __getOwnProperty__(ctx, name);
|
|
|
|
// Clause 3
|
|
|
|
if (pd && pd->isData()) {
|
|
|
|
// spec says to call [[DefineOwnProperty]] with { [[Value]]: value }
|
|
|
|
|
|
|
|
// ### to simplify and speed up we should expand the relevant parts here (clauses 6,7,9,10,12,13)
|
|
|
|
PropertyDescriptor desc = PropertyDescriptor::fromValue(value);
|
2012-11-27 22:23:04 +00:00
|
|
|
__defineOwnProperty__(ctx, name, &desc);
|
2012-10-29 14:37:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clause 4
|
2012-11-28 21:45:43 +00:00
|
|
|
if (!pd && prototype)
|
2012-12-14 09:25:27 +00:00
|
|
|
pd = prototype->__getPropertyDescriptor__(ctx, name);
|
2012-10-29 14:37:02 +00:00
|
|
|
|
|
|
|
// Clause 5
|
|
|
|
if (pd && pd->isAccessor()) {
|
|
|
|
assert(pd->set != 0);
|
|
|
|
|
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
2012-11-10 11:45:00 +00:00
|
|
|
pd->set->call(ctx, Value::fromObject(this), args, 1);
|
2012-10-29 14:37:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyDescriptor *p = members->insert(name);
|
|
|
|
*p = PropertyDescriptor::fromValue(value);
|
2012-12-13 12:12:10 +00:00
|
|
|
p->configurable = PropertyDescriptor::Enabled;
|
|
|
|
p->enumberable = PropertyDescriptor::Enabled;
|
|
|
|
p->writable = PropertyDescriptor::Enabled;
|
2012-11-27 23:12:33 +00:00
|
|
|
return;
|
2012-10-29 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reject:
|
2012-12-01 13:05:07 +00:00
|
|
|
if (ctx->strictMode)
|
2012-10-29 14:37:02 +00:00
|
|
|
__qmljs_throw_type_error(ctx);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
void Object::__put__(ExecutionContext *ctx, uint index, Value value)
|
|
|
|
{
|
|
|
|
// clause 1
|
|
|
|
if (!__canPut__(ctx, index))
|
|
|
|
goto reject;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Clause 2
|
|
|
|
PropertyDescriptor *pd = __getOwnProperty__(ctx, index);
|
|
|
|
// Clause 3
|
|
|
|
if (pd && pd->isData()) {
|
|
|
|
// spec says to call [[DefineOwnProperty]] with { [[Value]]: value }
|
|
|
|
|
|
|
|
// ### to simplify and speed up we should expand the relevant parts here (clauses 6,7,9,10,12,13)
|
|
|
|
PropertyDescriptor desc = PropertyDescriptor::fromValue(value);
|
|
|
|
__defineOwnProperty__(ctx, index, &desc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clause 4
|
|
|
|
if (!pd && prototype)
|
|
|
|
pd = prototype->__getPropertyDescriptor__(ctx, index);
|
|
|
|
|
|
|
|
// Clause 5
|
|
|
|
if (pd && pd->isAccessor()) {
|
|
|
|
assert(pd->set != 0);
|
|
|
|
|
|
|
|
Value args[1];
|
|
|
|
args[0] = value;
|
|
|
|
pd->set->call(ctx, Value::fromObject(this), args, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
array.set(index, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.6
|
2012-12-04 21:46:48 +00:00
|
|
|
bool Object::__hasProperty__(const ExecutionContext *ctx, String *name) const
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __hasProperty__(ctx, idx);
|
|
|
|
|
2012-12-12 08:00:19 +00:00
|
|
|
if (members && members->find(name) != 0)
|
|
|
|
return true;
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
return prototype ? prototype->__hasProperty__(ctx, name) : false;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
bool Object::__hasProperty__(const ExecutionContext *ctx, uint index) const
|
|
|
|
{
|
|
|
|
const PropertyDescriptor *p = array.at(index);
|
|
|
|
if (p && p->type != PropertyDescriptor::Generic)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return prototype ? prototype->__hasProperty__(ctx, index) : false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.7
|
2012-11-27 22:23:04 +00:00
|
|
|
bool Object::__delete__(ExecutionContext *ctx, String *name)
|
2012-10-29 14:37:02 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __delete__(ctx, idx);
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
if (members) {
|
|
|
|
if (PropertyTableEntry *entry = members->findEntry(name)) {
|
|
|
|
if (entry->descriptor.isConfigurable()) {
|
|
|
|
members->remove(entry);
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-01 13:05:07 +00:00
|
|
|
if (ctx->strictMode)
|
2012-10-29 14:37:02 +00:00
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
bool Object::__delete__(ExecutionContext *ctx, uint index)
|
|
|
|
{
|
|
|
|
if (array.deleteIndex(index))
|
|
|
|
return true;
|
2013-01-10 22:22:04 +00:00
|
|
|
if (ctx->strictMode)
|
2013-01-10 14:11:32 +00:00
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Section 8.12.9
|
2012-11-27 22:23:04 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, String *name, PropertyDescriptor *desc)
|
2012-05-20 17:59:47 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
uint idx = name->asArrayIndex();
|
|
|
|
if (idx != String::InvalidArrayIndex)
|
|
|
|
return __defineOwnProperty__(ctx, idx, desc);
|
|
|
|
|
2013-01-10 22:22:04 +00:00
|
|
|
PropertyDescriptor *current;
|
|
|
|
|
|
|
|
if (isArray && name->isEqualTo(ctx->engine->id_length)) {
|
2013-01-11 13:33:10 +00:00
|
|
|
PropertyDescriptor *lp = array.getLengthProperty();
|
2013-01-14 21:45:00 +00:00
|
|
|
if (desc->isEmpty() || desc->isSubset(lp))
|
|
|
|
return true;
|
2013-01-11 15:28:05 +00:00
|
|
|
if (!lp->isWritable() || desc->type == PropertyDescriptor::Accessor || desc->isConfigurable() || desc->isEnumerable())
|
2013-01-10 22:22:04 +00:00
|
|
|
goto reject;
|
2013-01-14 21:45:00 +00:00
|
|
|
bool succeeded = true;
|
2013-01-11 15:28:05 +00:00
|
|
|
if (desc->type == PropertyDescriptor::Data) {
|
2013-01-11 13:33:10 +00:00
|
|
|
bool ok;
|
2013-01-11 15:28:05 +00:00
|
|
|
uint l = desc->value.asArrayLength(ctx, &ok);
|
2013-01-11 13:33:10 +00:00
|
|
|
if (!ok)
|
|
|
|
ctx->throwRangeError(desc->value);
|
2013-01-14 16:41:53 +00:00
|
|
|
succeeded = array.setLength(l);
|
2013-01-11 13:33:10 +00:00
|
|
|
}
|
2013-01-11 15:28:05 +00:00
|
|
|
if (desc->writable == PropertyDescriptor::Disabled)
|
2013-01-11 13:33:10 +00:00
|
|
|
lp->writable = PropertyDescriptor::Disabled;
|
2013-01-14 16:41:53 +00:00
|
|
|
if (!succeeded)
|
|
|
|
goto reject;
|
2013-01-11 13:33:10 +00:00
|
|
|
return true;
|
2013-01-10 22:22:04 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 20:56:15 +00:00
|
|
|
if (!members)
|
2012-12-13 09:19:14 +00:00
|
|
|
members.reset(new PropertyTable());
|
2012-10-28 20:56:15 +00:00
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
// Clause 1
|
2013-01-10 22:22:04 +00:00
|
|
|
current = __getOwnProperty__(ctx, name);
|
2012-10-29 14:37:02 +00:00
|
|
|
if (!current) {
|
|
|
|
// clause 3
|
2012-10-28 20:56:15 +00:00
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
2012-10-29 14:37:02 +00:00
|
|
|
// clause 4
|
2012-11-26 22:26:39 +00:00
|
|
|
PropertyDescriptor *pd = members->insert(name);
|
|
|
|
*pd = *desc;
|
|
|
|
pd->fullyPopulated();
|
2012-10-29 14:37:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
return __defineOwnProperty__(ctx, current, desc);
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
2012-05-25 10:54:36 +00:00
|
|
|
}
|
2012-05-20 17:59:47 +00:00
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, uint index, PropertyDescriptor *desc)
|
|
|
|
{
|
2013-01-14 22:32:57 +00:00
|
|
|
PropertyDescriptor *current;
|
|
|
|
|
|
|
|
// 15.4.5.1, 4b
|
|
|
|
if (isArray && index >= array.length() && !array.getLengthProperty()->isWritable())
|
|
|
|
goto reject;
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
// Clause 1
|
2013-01-14 22:32:57 +00:00
|
|
|
current = __getOwnProperty__(ctx, index);
|
2013-01-10 14:11:32 +00:00
|
|
|
if (!current) {
|
|
|
|
// clause 3
|
|
|
|
if (!extensible)
|
|
|
|
goto reject;
|
|
|
|
// clause 4
|
|
|
|
PropertyDescriptor *pd = array.insert(index);
|
|
|
|
*pd = *desc;
|
|
|
|
pd->fullyPopulated();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
return __defineOwnProperty__(ctx, current, desc);
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, PropertyDescriptor *current, PropertyDescriptor *desc)
|
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
// clause 5
|
|
|
|
if (desc->isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// clause 6
|
|
|
|
if (desc->isSubset(current))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// clause 7
|
|
|
|
if (!current->isConfigurable()) {
|
|
|
|
if (desc->isConfigurable())
|
|
|
|
goto reject;
|
|
|
|
if (desc->enumberable != PropertyDescriptor::Undefined && desc->enumberable != current->enumberable)
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clause 8
|
|
|
|
if (desc->isGeneric())
|
|
|
|
goto accept;
|
|
|
|
|
|
|
|
// clause 9
|
|
|
|
if (current->isData() != desc->isData()) {
|
|
|
|
// 9a
|
|
|
|
if (!current->isConfigurable())
|
|
|
|
goto reject;
|
|
|
|
if (current->isData()) {
|
|
|
|
// 9b
|
|
|
|
current->type = PropertyDescriptor::Accessor;
|
|
|
|
current->writable = PropertyDescriptor::Undefined;
|
|
|
|
current->get = 0;
|
|
|
|
current->set = 0;
|
|
|
|
} else {
|
|
|
|
// 9c
|
|
|
|
current->type = PropertyDescriptor::Data;
|
|
|
|
current->writable = PropertyDescriptor::Disabled;
|
|
|
|
current->value = Value::undefinedValue();
|
|
|
|
}
|
|
|
|
} else if (current->isData() && desc->isData()) { // clause 10
|
|
|
|
if (!current->isConfigurable() && !current->isWritable()) {
|
|
|
|
if (desc->isWritable() || !current->value.sameValue(desc->value))
|
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
} else { // clause 10
|
|
|
|
assert(current->isAccessor() && desc->isAccessor());
|
|
|
|
if (!current->isConfigurable()) {
|
2013-01-16 10:00:56 +00:00
|
|
|
if (desc->get && !(current->get == desc->get || (!current->get && (quintptr)desc->get == 0x1)))
|
|
|
|
goto reject;
|
|
|
|
if (desc->set && !(current->set == desc->set || (!current->set && (quintptr)desc->set == 0x1)))
|
2013-01-10 14:11:32 +00:00
|
|
|
goto reject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accept:
|
|
|
|
|
|
|
|
*current += *desc;
|
|
|
|
return true;
|
|
|
|
reject:
|
|
|
|
if (ctx->strictMode)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:32:57 +00:00
|
|
|
|
2012-12-11 22:58:40 +00:00
|
|
|
bool Object::__defineOwnProperty__(ExecutionContext *ctx, const QString &name, PropertyDescriptor *desc)
|
|
|
|
{
|
|
|
|
return __defineOwnProperty__(ctx, ctx->engine->identifier(name), desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-01 19:08:26 +00:00
|
|
|
Value Object::call(ExecutionContext *context, Value , Value *, int)
|
|
|
|
{
|
|
|
|
context->throwTypeError();
|
|
|
|
return Value::undefinedValue();
|
|
|
|
}
|
|
|
|
|
2013-01-11 13:33:10 +00:00
|
|
|
void ArrayObject::init(ExecutionContext *context)
|
|
|
|
{
|
|
|
|
isArray = true;
|
|
|
|
if (!members)
|
|
|
|
members.reset(new PropertyTable());
|
|
|
|
PropertyDescriptor *pd = members->insert(context->engine->id_length);
|
|
|
|
pd->type = PropertyDescriptor::Data;
|
|
|
|
pd->writable = PropertyDescriptor::Enabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Disabled;
|
|
|
|
pd->value = Value::fromInt32(0);
|
|
|
|
array.setLengthProperty(pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
void ForEachIteratorObject::getCollectables(QVector<Object *> &objects)
|
|
|
|
{
|
|
|
|
Object::getCollectables(objects);
|
2013-01-10 21:42:58 +00:00
|
|
|
if (it.object)
|
|
|
|
objects.append(it.object);
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-11 09:03:40 +00:00
|
|
|
Function::~Function()
|
|
|
|
{
|
|
|
|
delete[] codeData;
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
bool FunctionObject::hasInstance(ExecutionContext *ctx, const Value &value)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-01-13 16:18:14 +00:00
|
|
|
if (! value.isObject())
|
2012-05-28 18:17:13 +00:00
|
|
|
return false;
|
|
|
|
|
2012-10-29 14:37:02 +00:00
|
|
|
Value o = __get__(ctx, ctx->engine->id_prototype);
|
2012-05-28 18:17:13 +00:00
|
|
|
if (! o.isObject()) {
|
|
|
|
ctx->throwTypeError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-19 19:08:47 +00:00
|
|
|
Object *v = value.objectValue();
|
2012-05-28 18:17:13 +00:00
|
|
|
while (v) {
|
|
|
|
v = v->prototype;
|
|
|
|
|
|
|
|
if (! v)
|
|
|
|
break;
|
2012-09-19 19:08:47 +00:00
|
|
|
else if (o.objectValue() == v)
|
2012-05-28 18:17:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-16 19:23:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
Value FunctionObject::construct(ExecutionContext *context, Value *args, int argc)
|
2012-11-10 11:45:00 +00:00
|
|
|
{
|
2012-12-16 22:08:07 +00:00
|
|
|
Object *obj = context->engine->newObject();
|
|
|
|
Value proto = __get__(context, context->engine->id_prototype);
|
|
|
|
if (proto.isObject())
|
|
|
|
obj->prototype = proto.objectValue();
|
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
ExecutionContext k;
|
|
|
|
ExecutionContext *ctx = needsActivation ? context->engine->newContext() : &k;
|
2012-12-16 22:08:07 +00:00
|
|
|
|
|
|
|
ctx->initCallContext(context, Value::fromObject(obj), this, args, argc);
|
2012-11-18 23:00:50 +00:00
|
|
|
Value result = construct(ctx);
|
2012-12-01 13:25:54 +00:00
|
|
|
ctx->leaveCallContext();
|
2012-12-16 22:08:07 +00:00
|
|
|
|
|
|
|
if (result.isObject())
|
|
|
|
return result;
|
|
|
|
return Value::fromObject(obj);
|
2012-11-10 11:45:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 21:42:20 +00:00
|
|
|
Value FunctionObject::call(ExecutionContext *context, Value thisObject, Value *args, int argc)
|
2012-11-10 11:45:00 +00:00
|
|
|
{
|
2012-11-16 22:07:10 +00:00
|
|
|
ExecutionContext k;
|
|
|
|
ExecutionContext *ctx = needsActivation ? context->engine->newContext() : &k;
|
2012-11-15 12:31:57 +00:00
|
|
|
|
|
|
|
ctx->initCallContext(context, thisObject, this, args, argc);
|
2012-11-18 23:00:50 +00:00
|
|
|
Value result = call(ctx);
|
2012-11-10 11:45:00 +00:00
|
|
|
ctx->leaveCallContext();
|
2012-11-18 23:00:50 +00:00
|
|
|
return result;
|
2012-11-10 11:45:00 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 15:51:34 +00:00
|
|
|
Value FunctionObject::callDirect(ExecutionContext *context, Value thisObject, Value *args, int argc)
|
|
|
|
{
|
|
|
|
ExecutionContext k;
|
|
|
|
ExecutionContext *ctx = needsActivation ? context->engine->newContext() : &k;
|
|
|
|
|
|
|
|
ctx->initCallContext(context, thisObject, this, args, argc);
|
|
|
|
maybeAdjustThisObjectForDirectCall(ctx, thisObject);
|
|
|
|
Value result = call(ctx);
|
|
|
|
ctx->leaveCallContext();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-11-18 23:00:50 +00:00
|
|
|
Value FunctionObject::call(ExecutionContext *ctx)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2012-05-07 14:05:05 +00:00
|
|
|
Q_UNUSED(ctx);
|
2012-11-18 23:00:50 +00:00
|
|
|
return Value::undefinedValue();
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-18 23:00:50 +00:00
|
|
|
Value FunctionObject::construct(ExecutionContext *ctx)
|
2012-05-07 14:05:05 +00:00
|
|
|
{
|
2012-12-16 22:08:07 +00:00
|
|
|
return call(ctx);
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-12-11 09:03:40 +00:00
|
|
|
ScriptFunction::ScriptFunction(ExecutionContext *scope, VM::Function *function)
|
2012-05-13 11:50:55 +00:00
|
|
|
: FunctionObject(scope)
|
2012-05-09 13:47:55 +00:00
|
|
|
, function(function)
|
2012-05-07 14:05:05 +00:00
|
|
|
{
|
2012-12-11 09:03:40 +00:00
|
|
|
assert(function);
|
|
|
|
assert(function->code);
|
|
|
|
|
2012-11-30 08:48:46 +00:00
|
|
|
// global function
|
|
|
|
if (!scope)
|
|
|
|
return;
|
|
|
|
|
2013-01-13 21:13:48 +00:00
|
|
|
MemoryManager::GCBlocker gcBlocker(scope->engine->memoryManager);
|
|
|
|
|
2012-12-11 09:03:40 +00:00
|
|
|
if (!function->name.isEmpty())
|
|
|
|
name = scope->engine->identifier(function->name);
|
2012-05-15 08:02:21 +00:00
|
|
|
needsActivation = function->needsActivation();
|
2012-12-14 10:32:52 +00:00
|
|
|
usesArgumentsObject = function->usesArgumentsObject;
|
2012-11-27 21:42:20 +00:00
|
|
|
strictMode = function->isStrict;
|
2012-05-08 09:13:02 +00:00
|
|
|
formalParameterCount = function->formals.size();
|
|
|
|
if (formalParameterCount) {
|
|
|
|
formalParameterList = new String*[formalParameterCount];
|
2012-06-05 08:47:04 +00:00
|
|
|
for (unsigned int i = 0; i < formalParameterCount; ++i) {
|
2012-12-11 09:03:40 +00:00
|
|
|
formalParameterList[i] = scope->engine->identifier(function->formals.at(i));
|
2012-05-08 09:13:02 +00:00
|
|
|
}
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
2013-01-13 21:13:48 +00:00
|
|
|
defineReadonlyProperty(scope->engine->id_length, Value::fromInt32(formalParameterCount));
|
2012-05-16 09:58:07 +00:00
|
|
|
|
|
|
|
varCount = function->locals.size();
|
|
|
|
if (varCount) {
|
|
|
|
varList = new String*[varCount];
|
2012-06-05 08:47:04 +00:00
|
|
|
for (unsigned int i = 0; i < varCount; ++i) {
|
2012-12-11 09:03:40 +00:00
|
|
|
varList[i] = scope->engine->identifier(function->locals.at(i));
|
2012-05-16 09:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-13 21:13:48 +00:00
|
|
|
|
|
|
|
Object *proto = scope->engine->newObject();
|
|
|
|
proto->defineDefaultProperty(scope->engine->id_constructor, Value::fromObject(this));
|
|
|
|
PropertyDescriptor *pd = members->insert(scope->engine->id_prototype);
|
|
|
|
pd->type = PropertyDescriptor::Data;
|
|
|
|
pd->writable = PropertyDescriptor::Enabled;
|
|
|
|
pd->enumberable = PropertyDescriptor::Disabled;
|
|
|
|
pd->configurable = PropertyDescriptor::Disabled;
|
|
|
|
pd->value = Value::fromObject(proto);
|
|
|
|
|
|
|
|
prototype = scope->engine->functionPrototype;
|
|
|
|
|
|
|
|
if (scope->strictMode) {
|
2013-01-13 22:57:35 +00:00
|
|
|
FunctionObject *thrower = scope->engine->newNativeFunction(scope, 0, __qmljs_throw_type_error);
|
|
|
|
PropertyDescriptor pd = PropertyDescriptor::fromAccessor(thrower, thrower);
|
2013-01-13 21:13:48 +00:00
|
|
|
pd.configurable = PropertyDescriptor::Disabled;
|
|
|
|
pd.enumberable = PropertyDescriptor::Disabled;
|
|
|
|
__defineOwnProperty__(scope, QStringLiteral("caller"), &pd);
|
|
|
|
__defineOwnProperty__(scope, QStringLiteral("arguments"), &pd);
|
|
|
|
}
|
2012-05-08 09:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptFunction::~ScriptFunction()
|
|
|
|
{
|
|
|
|
delete[] formalParameterList;
|
2012-05-16 09:58:07 +00:00
|
|
|
delete[] varList;
|
2012-05-08 09:13:02 +00:00
|
|
|
}
|
|
|
|
|
2012-11-18 23:00:50 +00:00
|
|
|
Value ScriptFunction::call(VM::ExecutionContext *ctx)
|
2012-05-08 09:13:02 +00:00
|
|
|
{
|
2012-12-11 09:03:40 +00:00
|
|
|
assert(function->code);
|
2012-11-18 23:00:50 +00:00
|
|
|
return function->code(ctx, function->codeData);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-11-27 21:42:20 +00:00
|
|
|
Value EvalFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc)
|
2012-11-16 21:21:34 +00:00
|
|
|
{
|
2012-11-25 19:53:57 +00:00
|
|
|
if (argc < 1)
|
|
|
|
return Value::undefinedValue();
|
2012-11-18 23:00:50 +00:00
|
|
|
|
2012-11-25 19:53:57 +00:00
|
|
|
if (!args[0].isString())
|
|
|
|
return args[0];
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-11-25 19:53:57 +00:00
|
|
|
// ### how to determine this correctly?
|
2012-11-16 21:21:34 +00:00
|
|
|
bool directCall = true;
|
|
|
|
|
2012-11-27 21:42:20 +00:00
|
|
|
const QString code = args[0].stringValue()->toQString();
|
2012-12-11 09:03:40 +00:00
|
|
|
QQmlJS::VM::Function *f = parseSource(context, QStringLiteral("eval code"), code, QQmlJS::Codegen::EvalCode);
|
2012-11-27 21:42:20 +00:00
|
|
|
if (!f)
|
|
|
|
return Value::undefinedValue();
|
|
|
|
|
2012-12-01 13:05:07 +00:00
|
|
|
bool strict = f->isStrict || context->strictMode;
|
2012-11-27 21:42:20 +00:00
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
ExecutionContext k, *ctx;
|
2012-11-16 21:21:34 +00:00
|
|
|
if (!directCall) {
|
2012-11-25 19:53:57 +00:00
|
|
|
qDebug() << "!direct";
|
2012-11-16 21:21:34 +00:00
|
|
|
// ###
|
2012-11-27 21:42:20 +00:00
|
|
|
} else if (strict) {
|
2012-11-16 21:21:34 +00:00
|
|
|
ctx = &k;
|
|
|
|
ctx->initCallContext(context, context->thisObject, this, args, argc);
|
|
|
|
} else {
|
2012-12-01 13:05:07 +00:00
|
|
|
// use the surrounding context
|
|
|
|
ctx = context;
|
2012-11-16 21:21:34 +00:00
|
|
|
}
|
2012-11-25 19:53:57 +00:00
|
|
|
|
2012-12-04 19:02:26 +00:00
|
|
|
// set the correct strict mode flag on the context
|
|
|
|
bool cstrict = ctx->strictMode;
|
|
|
|
ctx->strictMode = strict;
|
|
|
|
|
2012-11-27 21:42:20 +00:00
|
|
|
Value result = f->code(ctx, f->codeData);
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-12-04 19:02:26 +00:00
|
|
|
ctx->strictMode = cstrict;
|
|
|
|
|
2012-11-27 21:42:20 +00:00
|
|
|
if (strict)
|
2012-11-16 21:21:34 +00:00
|
|
|
ctx->leaveCallContext();
|
2012-11-19 12:15:25 +00:00
|
|
|
|
2012-11-18 23:00:50 +00:00
|
|
|
return result;
|
2012-11-16 21:21:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 13:39:19 +00:00
|
|
|
EvalFunction::EvalFunction(ExecutionContext *scope)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
{
|
|
|
|
name = scope->engine->newString(QLatin1String("eval"));
|
|
|
|
}
|
|
|
|
|
2012-12-11 09:03:40 +00:00
|
|
|
QQmlJS::VM::Function *EvalFunction::parseSource(QQmlJS::VM::ExecutionContext *ctx,
|
2012-11-27 21:42:20 +00:00
|
|
|
const QString &fileName, const QString &source,
|
|
|
|
QQmlJS::Codegen::Mode mode)
|
2012-11-16 21:21:34 +00:00
|
|
|
{
|
|
|
|
using namespace QQmlJS;
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
MemoryManager::GCBlocker gcBlocker(ctx->engine->memoryManager);
|
|
|
|
|
2012-11-16 21:21:34 +00:00
|
|
|
VM::ExecutionEngine *vm = ctx->engine;
|
|
|
|
IR::Module module;
|
2012-12-11 09:03:40 +00:00
|
|
|
VM::Function *globalCode = 0;
|
2012-11-16 21:21:34 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
QQmlJS::Engine ee, *engine = ⅇ
|
|
|
|
Lexer lexer(engine);
|
|
|
|
lexer.setCode(source, 1, false);
|
|
|
|
Parser parser(engine);
|
|
|
|
|
|
|
|
const bool parsed = parser.parseProgram();
|
|
|
|
|
2012-11-28 10:00:23 +00:00
|
|
|
VM::DiagnosticMessage *error = 0, **errIt = &error;
|
|
|
|
foreach (const QQmlJS::DiagnosticMessage &m, parser.diagnosticMessages()) {
|
|
|
|
if (m.isError()) {
|
2012-12-04 11:00:23 +00:00
|
|
|
*errIt = new VM::DiagnosticMessage;
|
|
|
|
(*errIt)->fileName = fileName;
|
2012-11-28 10:00:23 +00:00
|
|
|
(*errIt)->offset = m.loc.offset;
|
|
|
|
(*errIt)->length = m.loc.length;
|
|
|
|
(*errIt)->startLine = m.loc.startLine;
|
|
|
|
(*errIt)->startColumn = m.loc.startColumn;
|
|
|
|
(*errIt)->type = VM::DiagnosticMessage::Error;
|
2012-12-04 11:00:23 +00:00
|
|
|
(*errIt)->message = m.message;
|
2012-11-28 10:00:23 +00:00
|
|
|
errIt = &(*errIt)->next;
|
|
|
|
} else {
|
|
|
|
std::cerr << qPrintable(fileName) << ':' << m.loc.startLine << ':' << m.loc.startColumn
|
|
|
|
<< ": warning: " << qPrintable(m.message) << std::endl;
|
|
|
|
}
|
2012-11-16 21:21:34 +00:00
|
|
|
}
|
2012-11-28 10:00:23 +00:00
|
|
|
if (error)
|
|
|
|
ctx->throwSyntaxError(error);
|
2012-11-16 21:21:34 +00:00
|
|
|
|
|
|
|
if (parsed) {
|
|
|
|
using namespace AST;
|
|
|
|
Program *program = AST::cast<Program *>(parser.rootNode());
|
2012-11-26 13:38:14 +00:00
|
|
|
if (!program) {
|
|
|
|
// if parsing was successful, and we have no program, then
|
|
|
|
// we're done...:
|
2012-11-27 21:42:20 +00:00
|
|
|
return 0;
|
2012-11-26 13:38:14 +00:00
|
|
|
}
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-12-02 18:58:35 +00:00
|
|
|
Codegen cg(ctx);
|
2012-12-11 09:03:40 +00:00
|
|
|
IR::Function *globalIRCode = cg(fileName, program, &module, mode);
|
|
|
|
QScopedPointer<EvalInstructionSelection> isel(ctx->engine->iselFactory->create(vm, &module));
|
2012-12-13 11:09:04 +00:00
|
|
|
if (globalIRCode)
|
|
|
|
globalCode = isel->vmFunction(globalIRCode);
|
2012-11-16 21:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! globalCode)
|
2012-11-18 23:00:50 +00:00
|
|
|
// ### should be a syntax error
|
|
|
|
__qmljs_throw_type_error(ctx);
|
2012-11-16 21:21:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 21:42:20 +00:00
|
|
|
return globalCode;
|
2012-11-16 21:21:34 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 13:37:55 +00:00
|
|
|
// parseInt [15.1.2.2]
|
|
|
|
ParseIntFunction::ParseIntFunction(ExecutionContext *scope)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
{
|
|
|
|
name = scope->engine->newString(QLatin1String("parseInt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int toInt(const QChar &qc, int R)
|
|
|
|
{
|
|
|
|
ushort c = qc.unicode();
|
|
|
|
int v = -1;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
v = c - '0';
|
|
|
|
else if (c >= 'A' && c <= 'Z')
|
|
|
|
v = c - 'A' + 10;
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
v = c - 'a' + 10;
|
|
|
|
if (v >= 0 && v < R)
|
|
|
|
return v;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ParseIntFunction::call(ExecutionContext *context, Value thisObject, Value *args, int argc)
|
|
|
|
{
|
|
|
|
Q_UNUSED(thisObject);
|
|
|
|
|
|
|
|
Value string = (argc > 0) ? args[0] : Value::undefinedValue();
|
|
|
|
Value radix = (argc > 1) ? args[1] : Value::undefinedValue();
|
|
|
|
int R = radix.isUndefined() ? 0 : radix.toInt32(context);
|
|
|
|
|
|
|
|
// [15.1.2.2] step by step:
|
|
|
|
String *inputString = string.toString(context); // 1
|
|
|
|
QString trimmed = inputString->toQString().trimmed(); // 2
|
|
|
|
const QChar *pos = trimmed.constData();
|
|
|
|
const QChar *end = pos + trimmed.length();
|
|
|
|
|
|
|
|
int sign = 1; // 3
|
|
|
|
if (pos != end) {
|
|
|
|
if (*pos == QLatin1Char('-'))
|
|
|
|
sign = -1; // 4
|
|
|
|
if (*pos == QLatin1Char('-') || *pos == QLatin1Char('+'))
|
|
|
|
++pos; // 5
|
|
|
|
}
|
|
|
|
bool stripPrefix = true; // 7
|
|
|
|
if (R) { // 8
|
|
|
|
if (R < 2 || R > 36)
|
|
|
|
return Value::fromDouble(nan("")); // 8a
|
|
|
|
if (R != 16)
|
|
|
|
stripPrefix = false; // 8b
|
|
|
|
} else { // 9
|
|
|
|
R = 10; // 9a
|
|
|
|
}
|
|
|
|
if (stripPrefix) { // 10
|
|
|
|
if ((end - pos >= 2)
|
|
|
|
&& (pos[0] == QLatin1Char('0'))
|
|
|
|
&& (pos[1] == QLatin1Char('x') || pos[1] == QLatin1Char('X'))) { // 10a
|
|
|
|
pos += 2;
|
|
|
|
R = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 11: Z is progressively built below
|
|
|
|
// 13: this is handled by the toInt function
|
|
|
|
if (pos == end) // 12
|
|
|
|
return Value::fromDouble(nan(""));
|
2013-01-15 14:01:09 +00:00
|
|
|
bool overflow = false;
|
|
|
|
qint64 v_overflow;
|
|
|
|
unsigned overflow_digit_count = 0;
|
2013-01-09 13:37:55 +00:00
|
|
|
int d = toInt(*pos++, R);
|
|
|
|
if (d == -1)
|
|
|
|
return Value::fromDouble(nan(""));
|
|
|
|
qint64 v = d;
|
|
|
|
while (pos != end) {
|
|
|
|
d = toInt(*pos++, R);
|
|
|
|
if (d == -1)
|
|
|
|
break;
|
2013-01-15 14:01:09 +00:00
|
|
|
if (overflow) {
|
|
|
|
if (overflow_digit_count == 0) {
|
|
|
|
v_overflow = v;
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
++overflow_digit_count;
|
|
|
|
v = v * R + d;
|
|
|
|
} else {
|
|
|
|
qint64 vNew = v * R + d;
|
|
|
|
if (vNew < v) {
|
|
|
|
overflow = true;
|
|
|
|
--pos;
|
|
|
|
} else {
|
|
|
|
v = vNew;
|
|
|
|
}
|
|
|
|
}
|
2013-01-09 13:37:55 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 14:01:09 +00:00
|
|
|
if (overflow) {
|
|
|
|
double result = (double) v_overflow * pow(R, overflow_digit_count);
|
|
|
|
result += v;
|
|
|
|
return Value::fromDouble(sign * result);
|
|
|
|
} else {
|
|
|
|
return Value::fromDouble(sign * (double) v); // 15
|
|
|
|
}
|
2013-01-09 13:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseFloat [15.1.2.3]
|
|
|
|
ParseFloatFunction::ParseFloatFunction(ExecutionContext *scope)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
{
|
|
|
|
name = scope->engine->newString(QLatin1String("parseFloat"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ParseFloatFunction::call(ExecutionContext *context, Value thisObject, Value *args, int argc)
|
|
|
|
{
|
|
|
|
Q_UNUSED(context);
|
|
|
|
Q_UNUSED(thisObject);
|
|
|
|
|
|
|
|
Value string = (argc > 0) ? args[0] : Value::undefinedValue();
|
|
|
|
|
|
|
|
// [15.1.2.3] step by step:
|
|
|
|
String *inputString = string.toString(context); // 1
|
|
|
|
QString trimmed = inputString->toQString().trimmed(); // 2
|
|
|
|
|
|
|
|
// 4:
|
|
|
|
if (trimmed.startsWith(QLatin1String("Infinity"))
|
|
|
|
|| trimmed.startsWith(QLatin1String("+Infinity")))
|
|
|
|
return Value::fromDouble(INFINITY);
|
|
|
|
if (trimmed.startsWith("-Infinity"))
|
|
|
|
return Value::fromDouble(-INFINITY);
|
|
|
|
QByteArray ba = trimmed.toLatin1();
|
|
|
|
bool ok;
|
|
|
|
const char *begin = ba.constData();
|
|
|
|
const char *end = 0;
|
|
|
|
double d = qstrtod(begin, &end, &ok);
|
|
|
|
if (end - begin == 0)
|
|
|
|
return Value::fromDouble(nan("")); // 3
|
|
|
|
else
|
|
|
|
return Value::fromDouble(d);
|
|
|
|
}
|
2012-11-16 21:21:34 +00:00
|
|
|
|
2012-11-25 19:53:57 +00:00
|
|
|
/// isNaN [15.1.2.4]
|
2012-11-29 13:39:19 +00:00
|
|
|
IsNaNFunction::IsNaNFunction(ExecutionContext *scope)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
{
|
|
|
|
name = scope->engine->newString(QLatin1String("isNaN"));
|
|
|
|
}
|
|
|
|
|
2013-01-09 13:37:55 +00:00
|
|
|
Value IsNaNFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc)
|
2012-11-25 19:53:57 +00:00
|
|
|
{
|
2013-01-09 13:37:55 +00:00
|
|
|
const Value &v = (argc > 0) ? args[0] : Value::undefinedValue();
|
2012-12-12 22:43:53 +00:00
|
|
|
if (v.integerCompatible())
|
|
|
|
return Value::fromBoolean(false);
|
|
|
|
|
|
|
|
double d = v.toNumber(context);
|
|
|
|
return Value::fromBoolean(std::isnan(d));
|
2012-11-25 19:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isFinite [15.1.2.5]
|
2012-11-29 13:39:19 +00:00
|
|
|
IsFiniteFunction::IsFiniteFunction(ExecutionContext *scope)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
{
|
|
|
|
name = scope->engine->newString(QLatin1String("isFinite"));
|
|
|
|
}
|
|
|
|
|
2013-01-09 13:37:55 +00:00
|
|
|
Value IsFiniteFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc)
|
2012-11-25 19:53:57 +00:00
|
|
|
{
|
2013-01-09 13:37:55 +00:00
|
|
|
const Value &v = (argc > 0) ? args[0] : Value::undefinedValue();
|
2012-12-12 22:43:53 +00:00
|
|
|
if (v.integerCompatible())
|
|
|
|
return Value::fromBoolean(true);
|
|
|
|
|
|
|
|
double d = v.toNumber(context);
|
|
|
|
return Value::fromBoolean(std::isfinite(d));
|
2012-11-25 19:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 08:57:02 +00:00
|
|
|
Value RegExpObject::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
|
2012-10-19 21:10:42 +00:00
|
|
|
{
|
|
|
|
QString n = name->toQString();
|
2012-12-14 08:57:02 +00:00
|
|
|
Value v = Value::undefinedValue();
|
2012-10-19 21:10:42 +00:00
|
|
|
if (n == QLatin1String("source"))
|
2012-12-14 08:57:02 +00:00
|
|
|
v = Value::fromString(ctx, value.pattern());
|
2012-10-19 21:10:42 +00:00
|
|
|
else if (n == QLatin1String("global"))
|
2012-12-14 08:57:02 +00:00
|
|
|
v = Value::fromBoolean(global);
|
2012-10-19 21:10:42 +00:00
|
|
|
else if (n == QLatin1String("ignoreCase"))
|
2012-12-14 08:57:02 +00:00
|
|
|
v = Value::fromBoolean(value.patternOptions() & QRegularExpression::CaseInsensitiveOption);
|
2012-10-19 21:10:42 +00:00
|
|
|
else if (n == QLatin1String("multiline"))
|
2012-12-14 08:57:02 +00:00
|
|
|
v = Value::fromBoolean(value.patternOptions() & QRegularExpression::MultilineOption);
|
2012-10-19 21:10:42 +00:00
|
|
|
else if (n == QLatin1String("lastIndex"))
|
2012-12-14 08:57:02 +00:00
|
|
|
v = lastIndex;
|
|
|
|
if (v.type() != Value::Undefined_Type) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object::__get__(ctx, name, hasProperty);
|
2012-10-19 21:10:42 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 08:57:02 +00:00
|
|
|
Value ErrorObject::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
|
2012-11-14 13:12:16 +00:00
|
|
|
{
|
|
|
|
QString n = name->toQString();
|
2012-12-14 08:57:02 +00:00
|
|
|
if (n == QLatin1String("message")) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2012-11-14 13:12:16 +00:00
|
|
|
return value;
|
2012-12-14 08:57:02 +00:00
|
|
|
}
|
|
|
|
return Object::__get__(ctx, name, hasProperty);
|
2012-11-14 13:12:16 +00:00
|
|
|
}
|
2012-10-19 21:10:42 +00:00
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
void ErrorObject::setNameProperty(ExecutionContext *ctx)
|
2012-11-14 15:44:08 +00:00
|
|
|
{
|
2012-12-12 23:53:04 +00:00
|
|
|
defineDefaultProperty(ctx, QLatin1String("name"), Value::fromString(ctx, className()));
|
2012-11-14 15:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
void ErrorObject::getCollectables(QVector<Object *> &objects)
|
|
|
|
{
|
|
|
|
Object::getCollectables(objects);
|
|
|
|
if (Object *o = value.asObject())
|
|
|
|
objects.append(o);
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:00:23 +00:00
|
|
|
SyntaxErrorObject::SyntaxErrorObject(ExecutionContext *ctx, DiagnosticMessage *message)
|
|
|
|
: ErrorObject(ctx->argument(0))
|
|
|
|
, msg(message)
|
|
|
|
{
|
2013-01-14 16:13:25 +00:00
|
|
|
prototype = ctx->engine->syntaxErrorPrototype;
|
2012-11-28 10:00:23 +00:00
|
|
|
if (message)
|
2012-11-28 13:16:21 +00:00
|
|
|
value = Value::fromString(message->buildFullMessage(ctx));
|
2012-11-28 10:00:23 +00:00
|
|
|
setNameProperty(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-10 07:55:31 +00:00
|
|
|
ArgumentsObject::ArgumentsObject(ExecutionContext *context, int formalParameterCount, int actualParameterCount)
|
2012-12-14 12:27:26 +00:00
|
|
|
: context(context)
|
|
|
|
, currentIndex(-1)
|
2012-12-14 09:25:27 +00:00
|
|
|
{
|
2013-01-10 07:55:31 +00:00
|
|
|
defineDefaultProperty(context->engine->id_length, Value::fromInt32(actualParameterCount));
|
2012-12-14 10:32:52 +00:00
|
|
|
if (context->strictMode) {
|
2012-12-14 12:27:26 +00:00
|
|
|
for (uint i = 0; i < context->argumentCount; ++i)
|
|
|
|
Object::__put__(context, QString::number(i), context->arguments[i]);
|
2013-01-13 22:57:35 +00:00
|
|
|
FunctionObject *thrower = context->engine->newNativeFunction(context, 0, __qmljs_throw_type_error);
|
|
|
|
PropertyDescriptor pd = PropertyDescriptor::fromAccessor(thrower, thrower);
|
2012-12-14 10:32:52 +00:00
|
|
|
pd.configurable = PropertyDescriptor::Disabled;
|
|
|
|
pd.enumberable = PropertyDescriptor::Disabled;
|
|
|
|
__defineOwnProperty__(context, QStringLiteral("callee"), &pd);
|
|
|
|
__defineOwnProperty__(context, QStringLiteral("caller"), &pd);
|
|
|
|
} else {
|
2012-12-14 12:27:26 +00:00
|
|
|
FunctionObject *get = context->engine->newNativeFunction(context, 0, method_getArg);
|
|
|
|
FunctionObject *set = context->engine->newNativeFunction(context, 0, method_setArg);
|
|
|
|
PropertyDescriptor pd = PropertyDescriptor::fromAccessor(get, set);
|
|
|
|
pd.configurable = PropertyDescriptor::Enabled;
|
|
|
|
pd.enumberable = PropertyDescriptor::Enabled;
|
2013-01-14 12:52:16 +00:00
|
|
|
uint enumerableParams = qMin(formalParameterCount, actualParameterCount);
|
|
|
|
for (uint i = 0; i < (uint)enumerableParams; ++i)
|
2013-01-10 21:42:58 +00:00
|
|
|
__defineOwnProperty__(context, i, &pd);
|
2013-01-14 12:52:16 +00:00
|
|
|
pd.type = PropertyDescriptor::Data;
|
|
|
|
pd.writable = PropertyDescriptor::Enabled;
|
|
|
|
for (uint i = enumerableParams; i < qMin((uint)actualParameterCount, context->argumentCount); ++i) {
|
|
|
|
pd.value = context->argument(i);
|
|
|
|
__defineOwnProperty__(context, i, &pd);
|
|
|
|
}
|
2012-12-14 10:32:52 +00:00
|
|
|
defineDefaultProperty(context, QStringLiteral("callee"), Value::fromObject(context->function));
|
2012-06-04 14:36:00 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-29 13:39:19 +00:00
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
Value ArgumentsObject::__get__(ExecutionContext *ctx, uint index, bool *hasProperty)
|
2012-12-14 12:27:26 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
if (!ctx->strictMode)
|
|
|
|
currentIndex = index;
|
|
|
|
Value result = Object::__get__(ctx, index, hasProperty);
|
2012-12-14 12:27:26 +00:00
|
|
|
currentIndex = -1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:11:32 +00:00
|
|
|
void ArgumentsObject::__put__(ExecutionContext *ctx, uint index, Value value)
|
2012-12-14 12:27:26 +00:00
|
|
|
{
|
2013-01-10 14:11:32 +00:00
|
|
|
if (!ctx->strictMode)
|
|
|
|
currentIndex = index;
|
|
|
|
Object::__put__(ctx, index, value);
|
2012-12-14 12:27:26 +00:00
|
|
|
currentIndex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArgumentsObject::method_getArg(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
Object *that = ctx->thisObject.asObject();
|
|
|
|
if (!that)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
ArgumentsObject *args = that->asArgumentsObject();
|
|
|
|
if (!args)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
|
|
|
|
assert(ctx != args->context);
|
2012-12-14 12:53:22 +00:00
|
|
|
assert(args->currentIndex >= 0 && args->currentIndex < (int)args->context->argumentCount);
|
2012-12-14 12:27:26 +00:00
|
|
|
return args->context->argument(args->currentIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArgumentsObject::method_setArg(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
Object *that = ctx->thisObject.asObject();
|
|
|
|
if (!that)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
ArgumentsObject *args = that->asArgumentsObject();
|
|
|
|
if (!args)
|
|
|
|
__qmljs_throw_type_error(ctx);
|
|
|
|
|
|
|
|
assert(ctx != args->context);
|
2012-12-14 12:53:22 +00:00
|
|
|
assert(args->currentIndex >= 0 && args->currentIndex < (int)args->context->argumentCount);
|
2012-12-14 12:27:26 +00:00
|
|
|
args->context->arguments[args->currentIndex] = ctx->arguments[0];
|
2012-12-14 12:53:22 +00:00
|
|
|
return Value::undefinedValue();
|
2012-12-14 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 13:39:19 +00:00
|
|
|
NativeFunction::NativeFunction(ExecutionContext *scope, String *name, Value (*code)(ExecutionContext *))
|
|
|
|
: FunctionObject(scope)
|
|
|
|
, code(code)
|
|
|
|
{
|
|
|
|
this->name = name;
|
|
|
|
}
|
2013-01-11 14:01:48 +00:00
|
|
|
|
|
|
|
Value NativeFunction::construct(ExecutionContext *ctx)
|
|
|
|
{
|
|
|
|
ctx->throwTypeError();
|
|
|
|
return Value::undefinedValue();
|
|
|
|
}
|
2013-01-11 15:51:34 +00:00
|
|
|
|
|
|
|
void NativeFunction::maybeAdjustThisObjectForDirectCall(ExecutionContext *context, Value thisArg)
|
|
|
|
{
|
|
|
|
// Built-in functions allow for the this object to be null or undefined. This overrides
|
|
|
|
// the behaviour of changing thisObject to the global object if null/undefined and allows
|
|
|
|
// the built-in functions for example to throw a type error if null is passed.
|
|
|
|
if (thisArg.isNull() || thisArg.isUndefined())
|
|
|
|
context->thisObject = thisArg;
|
|
|
|
}
|
2013-01-13 22:57:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
BoundFunction::BoundFunction(ExecutionContext *scope, FunctionObject *target, Value boundThis, const QVector<Value> &boundArgs)
|
|
|
|
: FunctionObject(scope)
|
|
|
|
, target(target)
|
|
|
|
, boundThis(boundThis)
|
|
|
|
, boundArgs(boundArgs)
|
|
|
|
{
|
|
|
|
prototype = scope->engine->functionPrototype;
|
|
|
|
|
|
|
|
int len = target->__get__(scope, scope->engine->id_length).toUInt32(scope);
|
|
|
|
len -= boundArgs.size();
|
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
|
|
|
defineReadonlyProperty(scope->engine->id_length, Value::fromInt32(len));
|
|
|
|
|
|
|
|
FunctionObject *thrower = scope->engine->newNativeFunction(scope, 0, __qmljs_throw_type_error);
|
|
|
|
PropertyDescriptor pd = PropertyDescriptor::fromAccessor(thrower, thrower);
|
|
|
|
pd.configurable = PropertyDescriptor::Disabled;
|
|
|
|
pd.enumberable = PropertyDescriptor::Disabled;
|
|
|
|
*members->insert(scope->engine->id_arguments) = pd;
|
|
|
|
*members->insert(scope->engine->id_caller) = pd;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value BoundFunction::call(ExecutionContext *context, Value, Value *args, int argc)
|
|
|
|
{
|
|
|
|
Value *newArgs = static_cast<Value *>(alloca(sizeof(Value)*(boundArgs.size() + argc)));
|
|
|
|
memcpy(newArgs, boundArgs.constData(), boundArgs.size()*sizeof(Value));
|
|
|
|
memcpy(newArgs + boundArgs.size(), args, argc*sizeof(Value));
|
|
|
|
|
|
|
|
return target->call(context, boundThis, newArgs, boundArgs.size() + argc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value BoundFunction::construct(ExecutionContext *context, Value *args, int argc)
|
|
|
|
{
|
|
|
|
Value *newArgs = static_cast<Value *>(alloca(sizeof(Value)*(boundArgs.size() + argc)));
|
|
|
|
memcpy(newArgs, boundArgs.constData(), boundArgs.size()*sizeof(Value));
|
|
|
|
memcpy(newArgs + boundArgs.size(), args, argc*sizeof(Value));
|
|
|
|
|
|
|
|
return target->construct(context, newArgs, boundArgs.size() + argc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoundFunction::hasInstance(ExecutionContext *ctx, const Value &value)
|
|
|
|
{
|
|
|
|
return target->hasInstance(ctx, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundFunction::getCollectables(QVector<Object *> &objects)
|
|
|
|
{
|
|
|
|
FunctionObject::getCollectables(objects);
|
|
|
|
objects.append(target);
|
|
|
|
if (Object *o = boundThis.asObject())
|
|
|
|
objects.append(o);
|
|
|
|
for (int i = 0; i < boundArgs.size(); ++i)
|
|
|
|
if (Object *o = boundArgs.at(i).asObject())
|
|
|
|
objects.append(o);
|
|
|
|
}
|
2013-01-14 13:51:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
StringObject::StringObject(ExecutionContext *ctx, const Value &value)
|
|
|
|
: value(value)
|
|
|
|
{
|
2013-01-16 12:34:46 +00:00
|
|
|
isString = true;
|
|
|
|
|
2013-01-14 13:51:53 +00:00
|
|
|
tmpProperty.type = PropertyDescriptor::Data;
|
|
|
|
tmpProperty.enumberable = PropertyDescriptor::Enabled;
|
|
|
|
tmpProperty.writable = PropertyDescriptor::Disabled;
|
|
|
|
tmpProperty.configurable = PropertyDescriptor::Disabled;
|
|
|
|
tmpProperty.value = Value::undefinedValue();
|
|
|
|
|
|
|
|
assert(value.isString());
|
|
|
|
defineReadonlyProperty(ctx->engine->id_length, Value::fromUInt32(value.stringValue()->toQString().length()));
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:34:46 +00:00
|
|
|
PropertyDescriptor *StringObject::getIndex(ExecutionContext *ctx, uint index)
|
2013-01-14 13:51:53 +00:00
|
|
|
{
|
|
|
|
QString str = value.stringValue()->toQString();
|
|
|
|
if (index >= (uint)str.length())
|
|
|
|
return 0;
|
|
|
|
String *result = ctx->engine->newString(str.mid(index, 1));
|
|
|
|
tmpProperty.value = Value::fromString(result);
|
|
|
|
return &tmpProperty;
|
|
|
|
}
|
2013-01-14 16:13:25 +00:00
|
|
|
|
|
|
|
EvalErrorObject::EvalErrorObject(ExecutionContext *ctx)
|
|
|
|
: ErrorObject(ctx->argument(0))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->evalErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeErrorObject::RangeErrorObject(ExecutionContext *ctx)
|
|
|
|
: ErrorObject(ctx->argument(0))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->rangeErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeErrorObject::RangeErrorObject(ExecutionContext *ctx, const QString &msg)
|
|
|
|
: ErrorObject(Value::fromString(ctx,msg))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->rangeErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferenceErrorObject::ReferenceErrorObject(ExecutionContext *ctx)
|
|
|
|
: ErrorObject(ctx->argument(0))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->referenceErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferenceErrorObject::ReferenceErrorObject(ExecutionContext *ctx, const QString &msg)
|
|
|
|
: ErrorObject(Value::fromString(ctx,msg))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->referenceErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeErrorObject::TypeErrorObject(ExecutionContext *ctx)
|
|
|
|
: ErrorObject(ctx->argument(0))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->typeErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeErrorObject::TypeErrorObject(ExecutionContext *ctx, const QString &msg)
|
|
|
|
: ErrorObject(Value::fromString(ctx,msg))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->typeErrorPrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
URIErrorObject::URIErrorObject(ExecutionContext *ctx)
|
|
|
|
: ErrorObject(ctx->argument(0))
|
|
|
|
{
|
|
|
|
setNameProperty(ctx);
|
|
|
|
prototype = ctx->engine->uRIErrorPrototype;
|
|
|
|
}
|