2012-10-12 08:12:24 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-12 08:12:24 +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.
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-10-12 08:12:24 +00:00
|
|
|
** 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
|
2014-08-22 06:13:59 +00:00
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
2012-10-12 08:12:24 +00:00
|
|
|
** 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
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
2014-08-22 06:13:59 +00:00
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2012-10-12 08:12:24 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4global_p.h"
|
|
|
|
#include "qv4runtime_p.h"
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4object_p.h"
|
|
|
|
#include "qv4objectproto_p.h"
|
|
|
|
#include "qv4globalobject_p.h"
|
|
|
|
#include "qv4stringobject_p.h"
|
2013-08-16 10:54:30 +00:00
|
|
|
#include "qv4argumentsobject_p.h"
|
2014-07-25 15:44:14 +00:00
|
|
|
#include "qv4objectiterator_p.h"
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4lookup_p.h"
|
2013-05-08 12:24:30 +00:00
|
|
|
#include "qv4function_p.h"
|
2012-12-10 23:41:35 +00:00
|
|
|
#include "private/qlocale_tools_p.h"
|
2013-09-04 14:05:34 +00:00
|
|
|
#include "qv4scopedvalue_p.h"
|
2013-10-23 14:24:58 +00:00
|
|
|
#include <private/qqmlcontextwrapper_p.h>
|
2014-07-23 17:43:38 +00:00
|
|
|
#include <private/qqmltypewrapper_p.h>
|
2013-10-23 14:24:58 +00:00
|
|
|
#include "qv4qobjectwrapper_p.h"
|
2013-11-14 23:06:18 +00:00
|
|
|
#include <private/qv8engine_p.h>
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2012-06-05 08:47:04 +00:00
|
|
|
|
2012-05-18 13:28:59 +00:00
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
#include <QtCore/qnumeric.h>
|
2012-05-07 14:05:05 +00:00
|
|
|
#include <QtCore/QDebug>
|
2012-04-16 19:23:25 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
2012-05-18 13:28:59 +00:00
|
|
|
#include <typeinfo>
|
2012-11-15 15:56:52 +00:00
|
|
|
#include <stdlib.h>
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#include "../../3rdparty/double-conversion/double-conversion.h"
|
2013-01-03 15:16:03 +00:00
|
|
|
|
2013-06-24 13:28:00 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
2013-04-19 11:03:42 +00:00
|
|
|
|
2013-06-24 13:28:00 +00:00
|
|
|
namespace QV4 {
|
2012-05-04 13:28:04 +00:00
|
|
|
|
2013-08-29 17:42:50 +00:00
|
|
|
#ifdef QV4_COUNT_RUNTIME_FUNCTIONS
|
|
|
|
struct RuntimeCounters::Data {
|
|
|
|
enum Type {
|
|
|
|
None = 0,
|
|
|
|
Undefined = 1,
|
|
|
|
Null = 2,
|
|
|
|
Boolean = 3,
|
|
|
|
Integer = 4,
|
2013-09-15 13:46:36 +00:00
|
|
|
Managed = 5,
|
2013-08-29 17:42:50 +00:00
|
|
|
Double = 7
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *pretty(Type t) {
|
|
|
|
switch (t) {
|
|
|
|
case None: return "";
|
|
|
|
case Undefined: return "Undefined";
|
|
|
|
case Null: return "Null";
|
|
|
|
case Boolean: return "Boolean";
|
|
|
|
case Integer: return "Integer";
|
2013-09-15 13:46:36 +00:00
|
|
|
case Managed: return "Managed";
|
2013-08-29 17:42:50 +00:00
|
|
|
case Double: return "Double";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned mangle(unsigned tag) {
|
|
|
|
switch (tag) {
|
|
|
|
case Value::Undefined_Type: return Undefined;
|
|
|
|
case Value::Null_Type: return Null;
|
|
|
|
case Value::Boolean_Type: return Boolean;
|
|
|
|
case Value::Integer_Type: return Integer;
|
2013-09-15 13:46:36 +00:00
|
|
|
case Value::Managed_Type: return Managed;
|
2013-08-29 17:42:50 +00:00
|
|
|
default: return Double;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned mangle(unsigned tag1, unsigned tag2) {
|
|
|
|
return (mangle(tag1) << 3) | mangle(tag2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unmangle(unsigned signature, Type &tag1, Type &tag2) {
|
|
|
|
tag1 = Type((signature >> 3) & 7);
|
|
|
|
tag2 = Type(signature & 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef QVector<quint64> Counters;
|
|
|
|
QHash<const char *, Counters> counters;
|
|
|
|
|
2013-10-09 08:47:11 +00:00
|
|
|
inline void count(const char *func) {
|
|
|
|
QVector<quint64> &cnt = counters[func];
|
|
|
|
if (cnt.isEmpty())
|
|
|
|
cnt.resize(64);
|
|
|
|
cnt[0] += 1;
|
|
|
|
}
|
|
|
|
|
2013-08-29 17:42:50 +00:00
|
|
|
inline void count(const char *func, unsigned tag) {
|
|
|
|
QVector<quint64> &cnt = counters[func];
|
|
|
|
if (cnt.isEmpty())
|
|
|
|
cnt.resize(64);
|
|
|
|
cnt[mangle(tag)] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void count(const char *func, unsigned tag1, unsigned tag2) {
|
|
|
|
QVector<quint64> &cnt = counters[func];
|
|
|
|
if (cnt.isEmpty())
|
|
|
|
cnt.resize(64);
|
|
|
|
cnt[mangle(tag1, tag2)] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Line {
|
|
|
|
const char *func;
|
|
|
|
Type tag1, tag2;
|
|
|
|
quint64 count;
|
|
|
|
|
|
|
|
static bool less(const Line &line1, const Line &line2) {
|
|
|
|
return line1.count > line2.count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void dump() const {
|
2013-10-04 14:06:16 +00:00
|
|
|
QTextStream outs(stderr, QIODevice::WriteOnly);
|
2013-08-29 17:42:50 +00:00
|
|
|
QList<Line> lines;
|
|
|
|
foreach (const char *func, counters.keys()) {
|
|
|
|
const Counters &fCount = counters[func];
|
|
|
|
for (int i = 0, ei = fCount.size(); i != ei; ++i) {
|
|
|
|
quint64 count = fCount[i];
|
|
|
|
if (!count)
|
|
|
|
continue;
|
|
|
|
Line line;
|
|
|
|
line.func = func;
|
|
|
|
unmangle(i, line.tag1, line.tag2);
|
|
|
|
line.count = count;
|
|
|
|
lines.append(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qSort(lines.begin(), lines.end(), Line::less);
|
2013-10-04 14:06:16 +00:00
|
|
|
outs << lines.size() << " counters:" << endl;
|
2013-08-29 17:42:50 +00:00
|
|
|
foreach (const Line &line, lines)
|
2013-10-04 14:06:16 +00:00
|
|
|
outs << qSetFieldWidth(10) << line.count << qSetFieldWidth(0)
|
|
|
|
<< " | " << line.func
|
|
|
|
<< " | " << pretty(line.tag1)
|
|
|
|
<< " | " << pretty(line.tag2)
|
|
|
|
<< endl;
|
2013-08-29 17:42:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RuntimeCounters *RuntimeCounters::instance = 0;
|
|
|
|
static RuntimeCounters runtimeCountersInstance;
|
|
|
|
RuntimeCounters::RuntimeCounters()
|
|
|
|
: d(new Data)
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeCounters::~RuntimeCounters()
|
|
|
|
{
|
|
|
|
d->dump();
|
2013-10-04 14:06:16 +00:00
|
|
|
delete d;
|
2013-08-29 17:42:50 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 08:47:11 +00:00
|
|
|
void RuntimeCounters::count(const char *func)
|
|
|
|
{
|
|
|
|
d->count(func);
|
|
|
|
}
|
|
|
|
|
2013-08-29 17:42:50 +00:00
|
|
|
void RuntimeCounters::count(const char *func, uint tag)
|
|
|
|
{
|
|
|
|
d->count(func, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeCounters::count(const char *func, uint tag1, uint tag2)
|
|
|
|
{
|
|
|
|
d->count(func, tag1, tag2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // QV4_COUNT_RUNTIME_FUNCTIONS
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
2014-03-10 18:58:05 +00:00
|
|
|
void RuntimeHelpers::numberToString(QString *result, double num, int radix)
|
2012-05-18 13:28:59 +00:00
|
|
|
{
|
2013-04-24 12:28:52 +00:00
|
|
|
Q_ASSERT(result);
|
|
|
|
|
2013-04-16 15:03:00 +00:00
|
|
|
if (std::isnan(num)) {
|
2013-04-24 12:28:52 +00:00
|
|
|
*result = QStringLiteral("NaN");
|
|
|
|
return;
|
2012-05-18 13:28:59 +00:00
|
|
|
} else if (qIsInf(num)) {
|
2014-03-14 09:15:56 +00:00
|
|
|
*result = num < 0 ? QStringLiteral("-Infinity") : QStringLiteral("Infinity");
|
2013-04-24 12:28:52 +00:00
|
|
|
return;
|
2012-05-18 13:28:59 +00:00
|
|
|
}
|
|
|
|
|
2013-01-03 15:16:03 +00:00
|
|
|
if (radix == 10) {
|
|
|
|
char str[100];
|
|
|
|
double_conversion::StringBuilder builder(str, sizeof(str));
|
|
|
|
double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToShortest(num, &builder);
|
2013-04-24 12:28:52 +00:00
|
|
|
*result = QString::fromLatin1(builder.Finalize());
|
|
|
|
return;
|
2013-01-03 15:16:03 +00:00
|
|
|
}
|
2012-05-18 13:28:59 +00:00
|
|
|
|
2013-04-24 12:28:52 +00:00
|
|
|
result->clear();
|
2012-05-18 13:28:59 +00:00
|
|
|
bool negative = false;
|
|
|
|
|
|
|
|
if (num < 0) {
|
|
|
|
negative = true;
|
|
|
|
num = -num;
|
|
|
|
}
|
|
|
|
|
|
|
|
double frac = num - ::floor(num);
|
2013-09-25 11:52:15 +00:00
|
|
|
num = Primitive::toInteger(num);
|
2012-05-18 13:28:59 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
char c = (char)::fmod(num, radix);
|
|
|
|
c = (c < 10) ? (c + '0') : (c - 10 + 'a');
|
2013-04-24 12:28:52 +00:00
|
|
|
result->prepend(QLatin1Char(c));
|
2012-05-18 13:28:59 +00:00
|
|
|
num = ::floor(num / radix);
|
|
|
|
} while (num != 0);
|
|
|
|
|
|
|
|
if (frac != 0) {
|
2013-04-24 12:28:52 +00:00
|
|
|
result->append(QLatin1Char('.'));
|
2012-05-18 13:28:59 +00:00
|
|
|
do {
|
|
|
|
frac = frac * radix;
|
|
|
|
char c = (char)::floor(frac);
|
|
|
|
c = (c < 10) ? (c + '0') : (c - 10 + 'a');
|
2013-04-24 12:28:52 +00:00
|
|
|
result->append(QLatin1Char(c));
|
2012-05-18 13:28:59 +00:00
|
|
|
frac = frac - ::floor(frac);
|
|
|
|
} while (frac != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (negative)
|
2013-04-24 12:28:52 +00:00
|
|
|
result->prepend(QLatin1Char('-'));
|
2012-05-18 13:28:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::closure(ExecutionEngine *engine, int functionId)
|
2012-05-09 11:51:44 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::Function *clos = engine->currentContext()->d()->compilationUnit->runtimeFunctions[functionId];
|
2013-09-09 20:34:47 +00:00
|
|
|
Q_ASSERT(clos);
|
2014-11-11 15:07:54 +00:00
|
|
|
return FunctionObject::createScriptFunction(engine->currentContext(), clos)->asReturnedValue();
|
2012-05-09 11:51:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::deleteElement(ExecutionEngine *engine, const ValueRef base, const ValueRef index)
|
2012-06-07 12:28:42 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-19 10:05:18 +00:00
|
|
|
ScopedObject o(scope, base);
|
|
|
|
if (o) {
|
2013-09-09 11:38:10 +00:00
|
|
|
uint n = index->asArrayIndex();
|
2013-02-14 15:02:40 +00:00
|
|
|
if (n < UINT_MAX) {
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode((bool)o->deleteIndexedProperty(n));
|
2013-02-14 15:02:40 +00:00
|
|
|
}
|
2012-06-07 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ScopedString name(scope, index->toString(engine));
|
|
|
|
return Runtime::deleteMember(engine, base, name.getPointer());
|
2012-06-07 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::deleteMember(ExecutionEngine *engine, const ValueRef base, String *name)
|
2012-06-07 12:28:42 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject obj(scope, base->toObject(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2013-09-19 10:05:18 +00:00
|
|
|
return Encode(obj->deleteProperty(name));
|
2012-06-07 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::deleteName(ExecutionEngine *engine, String *name)
|
2012-06-07 12:28:42 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
return Encode(engine->currentContext()->deleteProperty(name));
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::instanceof(ExecutionEngine *engine, const ValueRef left, const ValueRef right)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2014-03-26 07:55:28 +00:00
|
|
|
// As nothing in this method can call into the memory manager, avoid using a Scope
|
|
|
|
// for performance reasons
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2014-11-07 17:10:30 +00:00
|
|
|
ScopedFunctionObject f(scope, right->asFunctionObject());
|
2013-11-14 21:08:00 +00:00
|
|
|
if (!f)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2014-11-03 02:17:57 +00:00
|
|
|
if (f->subtype() == Heap::FunctionObject::BoundFunction)
|
2014-11-07 17:10:30 +00:00
|
|
|
f = static_cast<BoundFunction *>(f.getPointer())->target();
|
2013-11-14 21:08:00 +00:00
|
|
|
|
2014-03-26 07:55:28 +00:00
|
|
|
Object *v = left->asObject();
|
2013-11-14 21:08:00 +00:00
|
|
|
if (!v)
|
|
|
|
return Encode(false);
|
|
|
|
|
2014-03-26 07:55:28 +00:00
|
|
|
Object *o = QV4::Value::fromReturnedValue(f->protoProperty()).asObject();
|
|
|
|
if (!o)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-11-14 21:08:00 +00:00
|
|
|
|
|
|
|
while (v) {
|
|
|
|
v = v->prototype();
|
|
|
|
|
2014-03-26 07:55:28 +00:00
|
|
|
if (!v)
|
2013-11-14 21:08:00 +00:00
|
|
|
break;
|
2014-03-26 07:55:28 +00:00
|
|
|
else if (o == v)
|
2013-11-14 21:08:00 +00:00
|
|
|
return Encode(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Encode(false);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::in(ExecutionEngine *engine, const ValueRef left, const ValueRef right)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-09-09 11:38:10 +00:00
|
|
|
if (!right->isObject())
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
|
|
|
Scope scope(engine);
|
|
|
|
ScopedString s(scope, left->toString(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.hasException())
|
|
|
|
return Encode::undefined();
|
2014-05-07 11:33:24 +00:00
|
|
|
bool r = right->objectValue()->hasProperty(s.getPointer());
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(r);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
double RuntimeHelpers::stringToNumber(const QString &string)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-04-17 16:17:30 +00:00
|
|
|
QString s = string.trimmed();
|
2012-06-11 07:38:52 +00:00
|
|
|
if (s.startsWith(QLatin1String("0x")) || s.startsWith(QLatin1String("0X")))
|
|
|
|
return s.toLong(0, 16);
|
2012-12-10 23:41:35 +00:00
|
|
|
bool ok;
|
|
|
|
QByteArray ba = s.toLatin1();
|
|
|
|
const char *begin = ba.constData();
|
|
|
|
const char *end = 0;
|
|
|
|
double d = qstrtod(begin, &end, &ok);
|
2012-12-12 18:18:40 +00:00
|
|
|
if (end - begin != ba.size()) {
|
2012-12-13 00:53:13 +00:00
|
|
|
if (ba == "Infinity" || ba == "+Infinity")
|
2013-02-07 15:52:18 +00:00
|
|
|
d = Q_INFINITY;
|
2012-12-13 00:53:13 +00:00
|
|
|
else if (ba == "-Infinity")
|
2013-02-07 15:52:18 +00:00
|
|
|
d = -Q_INFINITY;
|
2012-12-10 23:41:35 +00:00
|
|
|
else
|
2013-02-08 08:39:25 +00:00
|
|
|
d = std::numeric_limits<double>::quiet_NaN();
|
2012-12-10 23:41:35 +00:00
|
|
|
}
|
2012-11-15 15:56:52 +00:00
|
|
|
return d;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
Returned<String> *RuntimeHelpers::stringFromNumber(ExecutionEngine *engine, double number)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-04-24 12:28:52 +00:00
|
|
|
QString qstr;
|
2014-03-10 18:58:05 +00:00
|
|
|
RuntimeHelpers::numberToString(&qstr, number, 10);
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->newString(qstr);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
ReturnedValue RuntimeHelpers::objectDefaultValue(Object *object, int typeHint)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2012-05-18 13:28:59 +00:00
|
|
|
if (typeHint == PREFERREDTYPE_HINT) {
|
2013-04-14 20:01:20 +00:00
|
|
|
if (object->asDateObject())
|
2012-05-18 13:28:59 +00:00
|
|
|
typeHint = STRING_HINT;
|
|
|
|
else
|
|
|
|
typeHint = NUMBER_HINT;
|
|
|
|
}
|
|
|
|
|
2014-04-05 18:23:20 +00:00
|
|
|
ExecutionEngine *engine = object->internalClass()->engine;
|
2013-10-21 07:57:58 +00:00
|
|
|
if (engine->hasException)
|
|
|
|
return Encode::undefined();
|
|
|
|
|
2014-01-24 21:55:39 +00:00
|
|
|
StringValue *meth1 = &engine->id_toString;
|
|
|
|
StringValue *meth2 = &engine->id_valueOf;
|
2012-05-18 13:28:59 +00:00
|
|
|
|
|
|
|
if (typeHint == NUMBER_HINT)
|
|
|
|
qSwap(meth1, meth2);
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 0);
|
2013-09-26 20:07:27 +00:00
|
|
|
callData->thisObject = object;
|
2012-05-25 15:45:15 +00:00
|
|
|
|
2013-09-18 13:34:13 +00:00
|
|
|
ScopedValue conv(scope, object->get(*meth1));
|
2013-09-11 12:36:01 +00:00
|
|
|
if (FunctionObject *o = conv->asFunctionObject()) {
|
2013-09-26 20:07:27 +00:00
|
|
|
ScopedValue r(scope, o->call(callData));
|
|
|
|
if (r->isPrimitive())
|
|
|
|
return r->asReturnedValue();
|
2012-05-18 13:28:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-21 07:57:58 +00:00
|
|
|
if (engine->hasException)
|
|
|
|
return Encode::undefined();
|
|
|
|
|
2013-09-18 13:34:13 +00:00
|
|
|
conv = object->get(*meth2);
|
2013-09-11 12:36:01 +00:00
|
|
|
if (FunctionObject *o = conv->asFunctionObject()) {
|
2013-09-26 20:07:27 +00:00
|
|
|
ScopedValue r(scope, o->call(callData));
|
|
|
|
if (r->isPrimitive())
|
|
|
|
return r->asReturnedValue();
|
2012-05-18 13:28:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
|
2013-02-14 22:16:50 +00:00
|
|
|
|
2014-11-11 12:34:18 +00:00
|
|
|
Heap::Object *RuntimeHelpers::convertToObject(ExecutionEngine *engine, const ValueRef value)
|
2012-06-07 13:28:45 +00:00
|
|
|
{
|
2014-11-07 17:51:19 +00:00
|
|
|
Q_ASSERT(!value->isObject());
|
2013-09-09 11:38:10 +00:00
|
|
|
switch (value->type()) {
|
2013-02-14 20:41:49 +00:00
|
|
|
case Value::Undefined_Type:
|
|
|
|
case Value::Null_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
engine->throwTypeError();
|
2013-10-21 07:57:58 +00:00
|
|
|
return 0;
|
2013-02-14 20:41:49 +00:00
|
|
|
case Value::Boolean_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->newBooleanObject(value);
|
2013-09-15 13:46:36 +00:00
|
|
|
case Value::Managed_Type:
|
|
|
|
Q_ASSERT(value->isString());
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->newStringObject(value);
|
2013-02-14 20:41:49 +00:00
|
|
|
case Value::Integer_Type:
|
|
|
|
default: // double
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->newNumberObject(value);
|
2013-02-14 20:41:49 +00:00
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
Returned<String> *RuntimeHelpers::convertToString(ExecutionEngine *engine, const ValueRef value)
|
2013-02-14 22:00:11 +00:00
|
|
|
{
|
2013-09-09 11:38:10 +00:00
|
|
|
switch (value->type()) {
|
2013-09-16 13:27:16 +00:00
|
|
|
case Value::Empty_Type:
|
2013-09-17 16:16:35 +00:00
|
|
|
Q_ASSERT(!"empty Value encountered");
|
|
|
|
case Value::Undefined_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_undefined.ret();
|
2013-02-14 22:00:11 +00:00
|
|
|
case Value::Null_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_null.ret();
|
2013-02-14 22:00:11 +00:00
|
|
|
case Value::Boolean_Type:
|
2013-09-09 11:38:10 +00:00
|
|
|
if (value->booleanValue())
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_true.ret();
|
2013-02-14 22:00:11 +00:00
|
|
|
else
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_false.ret();
|
2013-09-15 13:46:36 +00:00
|
|
|
case Value::Managed_Type:
|
|
|
|
if (value->isString())
|
|
|
|
return value->stringValue()->asReturned<String>();
|
|
|
|
{
|
2014-11-07 17:51:19 +00:00
|
|
|
Scope scope(engine);
|
2014-03-10 18:58:05 +00:00
|
|
|
ScopedValue prim(scope, RuntimeHelpers::toPrimitive(value, STRING_HINT));
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::convertToString(engine, prim);
|
2013-09-15 13:46:36 +00:00
|
|
|
}
|
2013-09-13 14:19:22 +00:00
|
|
|
case Value::Integer_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::stringFromNumber(engine, value->int_32);
|
2013-09-13 14:19:22 +00:00
|
|
|
default: // double
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::stringFromNumber(engine, value->doubleValue());
|
2013-02-14 22:00:11 +00:00
|
|
|
} // switch
|
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2013-11-02 16:11:06 +00:00
|
|
|
// This is slightly different from the method above, as
|
|
|
|
// the + operator requires a slightly different conversion
|
2014-11-07 17:51:19 +00:00
|
|
|
static Returned<String> *convert_to_string_add(ExecutionEngine *engine, const ValueRef value)
|
2013-11-02 16:11:06 +00:00
|
|
|
{
|
|
|
|
switch (value->type()) {
|
|
|
|
case Value::Empty_Type:
|
|
|
|
Q_ASSERT(!"empty Value encountered");
|
|
|
|
case Value::Undefined_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_undefined.ret();
|
2013-11-02 16:11:06 +00:00
|
|
|
case Value::Null_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_null.ret();
|
2013-11-02 16:11:06 +00:00
|
|
|
case Value::Boolean_Type:
|
|
|
|
if (value->booleanValue())
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_true.ret();
|
2013-11-02 16:11:06 +00:00
|
|
|
else
|
2014-11-07 17:51:19 +00:00
|
|
|
return engine->id_false.ret();
|
2013-11-02 16:11:06 +00:00
|
|
|
case Value::Managed_Type:
|
|
|
|
if (value->isString())
|
|
|
|
return value->stringValue()->asReturned<String>();
|
|
|
|
{
|
2014-11-07 17:51:19 +00:00
|
|
|
Scope scope(engine);
|
2014-03-10 18:58:05 +00:00
|
|
|
ScopedValue prim(scope, RuntimeHelpers::toPrimitive(value, PREFERREDTYPE_HINT));
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::convertToString(engine, prim);
|
2013-11-02 16:11:06 +00:00
|
|
|
}
|
|
|
|
case Value::Integer_Type:
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::stringFromNumber(engine, value->int_32);
|
2013-11-02 16:11:06 +00:00
|
|
|
default: // double
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::stringFromNumber(engine, value->doubleValue());
|
2013-11-02 16:11:06 +00:00
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
QV4::ReturnedValue RuntimeHelpers::addHelper(ExecutionEngine *engine, const ValueRef left, const ValueRef right)
|
2013-11-02 16:11:06 +00:00
|
|
|
{
|
2014-11-07 17:51:19 +00:00
|
|
|
Scope scope(engine);
|
2013-11-02 16:11:06 +00:00
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
ScopedValue pleft(scope, RuntimeHelpers::toPrimitive(left, PREFERREDTYPE_HINT));
|
|
|
|
ScopedValue pright(scope, RuntimeHelpers::toPrimitive(right, PREFERREDTYPE_HINT));
|
2013-11-02 16:11:06 +00:00
|
|
|
if (pleft->isString() || pright->isString()) {
|
|
|
|
if (!pleft->isString())
|
2014-11-07 17:51:19 +00:00
|
|
|
pleft = convert_to_string_add(engine, pleft);
|
2013-11-02 16:11:06 +00:00
|
|
|
if (!pright->isString())
|
2014-11-07 17:51:19 +00:00
|
|
|
pright = convert_to_string_add(engine, pright);
|
2013-11-02 16:11:06 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2014-05-09 13:06:29 +00:00
|
|
|
if (!pleft->stringValue()->d()->length())
|
2013-11-02 16:11:06 +00:00
|
|
|
return pright->asReturnedValue();
|
2014-05-09 13:06:29 +00:00
|
|
|
if (!pright->stringValue()->d()->length())
|
2013-11-02 16:11:06 +00:00
|
|
|
return pleft->asReturnedValue();
|
2014-11-07 17:51:19 +00:00
|
|
|
return (engine->memoryManager->alloc<String>(engine, pleft->stringValue()->d(), pright->stringValue()->d()))->asReturnedValue();
|
2013-11-02 16:11:06 +00:00
|
|
|
}
|
2014-03-10 18:58:05 +00:00
|
|
|
double x = RuntimeHelpers::toNumber(pleft);
|
|
|
|
double y = RuntimeHelpers::toNumber(pright);
|
2013-11-02 16:11:06 +00:00
|
|
|
return Encode(x + y);
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::addString(ExecutionEngine *engine, const QV4::ValueRef left, const QV4::ValueRef right)
|
2013-11-02 16:11:06 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(left->isString() || right->isString());
|
|
|
|
|
|
|
|
if (left->isString() && right->isString()) {
|
2014-05-09 13:06:29 +00:00
|
|
|
if (!left->stringValue()->d()->length())
|
2013-11-02 16:11:06 +00:00
|
|
|
return right->asReturnedValue();
|
2014-05-09 13:06:29 +00:00
|
|
|
if (!right->stringValue()->d()->length())
|
2013-11-02 16:11:06 +00:00
|
|
|
return left->asReturnedValue();
|
2014-11-11 15:07:54 +00:00
|
|
|
return (engine->memoryManager->alloc<String>(engine, left->stringValue()->d(), right->stringValue()->d()))->asReturnedValue();
|
2013-11-02 16:11:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-11-02 16:11:06 +00:00
|
|
|
ScopedValue pleft(scope, *left);
|
|
|
|
ScopedValue pright(scope, *right);
|
|
|
|
|
|
|
|
if (!pleft->isString())
|
2014-11-11 15:07:54 +00:00
|
|
|
pleft = convert_to_string_add(engine, left);
|
2013-11-02 16:11:06 +00:00
|
|
|
if (!pright->isString())
|
2014-11-11 15:07:54 +00:00
|
|
|
pright = convert_to_string_add(engine, right);
|
2013-11-02 16:11:06 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2014-05-09 13:06:29 +00:00
|
|
|
if (!pleft->stringValue()->d()->length())
|
2013-11-02 16:11:06 +00:00
|
|
|
return pright->asReturnedValue();
|
2014-05-09 13:06:29 +00:00
|
|
|
if (!pright->stringValue()->d()->length())
|
2013-11-02 16:11:06 +00:00
|
|
|
return pleft->asReturnedValue();
|
2014-11-11 15:07:54 +00:00
|
|
|
return (engine->memoryManager->alloc<String>(engine, pleft->stringValue()->d(), pright->stringValue()->d()))->asReturnedValue();
|
2013-11-02 16:11:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::setProperty(ExecutionEngine *engine, const ValueRef object, String *name, const ValueRef value)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject o(scope, object->toObject(engine));
|
2013-11-12 10:01:06 +00:00
|
|
|
if (!o)
|
|
|
|
return;
|
2013-09-19 10:05:18 +00:00
|
|
|
o->put(name, value);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getElement(ExecutionEngine *engine, const ValueRef object, const ValueRef index)
|
2012-05-20 17:59:47 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-09 11:38:10 +00:00
|
|
|
uint idx = index->asArrayIndex();
|
2013-01-16 23:45:11 +00:00
|
|
|
|
2013-09-13 12:11:55 +00:00
|
|
|
Scoped<Object> o(scope, object);
|
2013-02-14 19:58:10 +00:00
|
|
|
if (!o) {
|
2013-02-16 22:27:07 +00:00
|
|
|
if (idx < UINT_MAX) {
|
2013-09-09 11:38:10 +00:00
|
|
|
if (String *str = object->asString()) {
|
2013-02-16 22:27:07 +00:00
|
|
|
if (idx >= (uint)str->toQString().length()) {
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode::undefined();
|
2013-02-16 22:27:07 +00:00
|
|
|
}
|
|
|
|
const QString s = str->toQString().mid(idx, 1);
|
2013-09-26 10:04:52 +00:00
|
|
|
return scope.engine->newString(s)->asReturnedValue();
|
2013-02-14 19:58:10 +00:00
|
|
|
}
|
2013-01-18 12:48:03 +00:00
|
|
|
}
|
2012-10-18 12:39:59 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
if (object->isNullOrUndefined()) {
|
2013-09-09 12:33:28 +00:00
|
|
|
QString message = QStringLiteral("Cannot read property '%1' of %2").arg(index->toQStringNoThrow()).arg(object->toQStringNoThrow());
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError(message);
|
2013-06-05 07:59:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
o = RuntimeHelpers::convertToObject(scope.engine, object);
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!o) // type error
|
|
|
|
return Encode::undefined();
|
2013-01-18 12:48:03 +00:00
|
|
|
}
|
2012-10-18 12:39:59 +00:00
|
|
|
|
2013-01-18 12:48:03 +00:00
|
|
|
if (idx < UINT_MAX) {
|
2014-10-27 07:54:26 +00:00
|
|
|
if (o->arrayData() && !o->arrayData()->hasAttributes()) {
|
2014-04-05 18:47:36 +00:00
|
|
|
ScopedValue v(scope, o->arrayData()->get(idx));
|
2013-12-16 08:16:57 +00:00
|
|
|
if (!v->isEmpty())
|
|
|
|
return v->asReturnedValue();
|
2013-02-14 19:58:10 +00:00
|
|
|
}
|
2013-01-18 12:48:03 +00:00
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
return o->getIndexed(idx);
|
2013-01-18 12:48:03 +00:00
|
|
|
}
|
2013-01-11 09:13:02 +00:00
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ScopedString name(scope, index->toString(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.hasException())
|
|
|
|
return Encode::undefined();
|
2014-05-07 11:33:24 +00:00
|
|
|
return o->get(name.getPointer());
|
2012-05-20 17:59:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::setElement(ExecutionEngine *engine, const ValueRef object, const ValueRef index, const ValueRef value)
|
2012-05-20 17:59:47 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject o(scope, object->toObject(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return;
|
2013-01-11 09:13:02 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
uint idx = index->asArrayIndex();
|
2013-01-11 09:13:02 +00:00
|
|
|
if (idx < UINT_MAX) {
|
2014-11-01 22:04:20 +00:00
|
|
|
if (o->arrayType() == Heap::ArrayData::Simple) {
|
2014-04-05 18:47:36 +00:00
|
|
|
SimpleArrayData *s = static_cast<SimpleArrayData *>(o->arrayData());
|
2014-10-21 12:54:45 +00:00
|
|
|
if (s && idx < s->len() && !s->data(idx).isEmpty()) {
|
|
|
|
s->data(idx) = value;
|
2014-01-24 11:46:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o->putIndexed(idx, value);
|
2013-01-11 09:13:02 +00:00
|
|
|
return;
|
2012-10-18 12:39:59 +00:00
|
|
|
}
|
2012-05-20 17:59:47 +00:00
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ScopedString name(scope, index->toString(engine));
|
2014-05-07 11:33:24 +00:00
|
|
|
o->put(name.getPointer(), value);
|
2012-05-20 17:59:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::foreachIterator(ExecutionEngine *engine, const ValueRef in)
|
2012-10-28 15:24:06 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-14 09:25:02 +00:00
|
|
|
Scoped<Object> o(scope, (Object *)0);
|
2013-09-09 11:38:10 +00:00
|
|
|
if (!in->isNullOrUndefined())
|
2014-11-11 15:07:54 +00:00
|
|
|
o = in->toObject(engine);
|
|
|
|
return engine->newForEachIteratorObject(o)->asReturnedValue();
|
2012-10-28 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
ReturnedValue Runtime::foreachNextPropertyName(const ValueRef foreach_iterator)
|
2012-10-28 15:24:06 +00:00
|
|
|
{
|
2013-09-09 19:17:01 +00:00
|
|
|
Q_ASSERT(foreach_iterator->isObject());
|
2012-10-28 15:24:06 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
ForEachIteratorObject *it = static_cast<ForEachIteratorObject *>(foreach_iterator->objectValue());
|
2013-09-09 19:17:01 +00:00
|
|
|
Q_ASSERT(it->as<ForEachIteratorObject>());
|
2012-10-28 15:24:06 +00:00
|
|
|
|
2013-09-12 20:37:41 +00:00
|
|
|
return it->nextPropertyName();
|
2012-10-28 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::setActivationProperty(ExecutionEngine *engine, String *name, const ValueRef value)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
engine->currentContext()->setProperty(name, value);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getProperty(ExecutionEngine *engine, const ValueRef object, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-13 12:11:55 +00:00
|
|
|
|
|
|
|
Scoped<Object> o(scope, object);
|
|
|
|
if (o)
|
|
|
|
return o->get(name);
|
2013-06-05 07:59:18 +00:00
|
|
|
|
2013-09-09 19:17:01 +00:00
|
|
|
if (object->isNullOrUndefined()) {
|
|
|
|
QString message = QStringLiteral("Cannot read property '%1' of %2").arg(name->toQString()).arg(object->toQStringNoThrow());
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError(message);
|
2012-05-09 09:04:57 +00:00
|
|
|
}
|
2013-09-09 19:17:01 +00:00
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
o = RuntimeHelpers::convertToObject(scope.engine, object);
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!o) // type error
|
|
|
|
return Encode::undefined();
|
2013-09-13 12:11:55 +00:00
|
|
|
return o->get(name);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getActivationProperty(ExecutionEngine *engine, String *name)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->currentContext()->getProperty(name);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif // V4_BOOTSTRAP
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
uint RuntimeHelpers::equalHelper(const ValueRef x, const ValueRef y)
|
2013-08-06 20:55:09 +00:00
|
|
|
{
|
2013-09-15 13:46:36 +00:00
|
|
|
Q_ASSERT(x->type() != y->type() || (x->isManaged() && (x->isString() != y->isString())));
|
2013-08-06 20:55:09 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
if (x->isNumber() && y->isNumber())
|
|
|
|
return x->asDouble() == y->asDouble();
|
|
|
|
if (x->isNull() && y->isUndefined()) {
|
2013-08-06 20:55:09 +00:00
|
|
|
return true;
|
2013-09-09 11:38:10 +00:00
|
|
|
} else if (x->isUndefined() && y->isNull()) {
|
2013-08-06 20:55:09 +00:00
|
|
|
return true;
|
2013-09-09 11:38:10 +00:00
|
|
|
} else if (x->isNumber() && y->isString()) {
|
2014-03-10 18:58:05 +00:00
|
|
|
double dy = RuntimeHelpers::toNumber(y);
|
2013-09-09 11:38:10 +00:00
|
|
|
return x->asDouble() == dy;
|
|
|
|
} else if (x->isString() && y->isNumber()) {
|
2014-03-10 18:58:05 +00:00
|
|
|
double dx = RuntimeHelpers::toNumber(x);
|
2013-09-09 11:38:10 +00:00
|
|
|
return dx == y->asDouble();
|
|
|
|
} else if (x->isBoolean()) {
|
2014-03-10 18:58:05 +00:00
|
|
|
return Runtime::compareEqual(Primitive::fromDouble((double) x->booleanValue()), y);
|
2013-09-09 11:38:10 +00:00
|
|
|
} else if (y->isBoolean()) {
|
2014-03-10 18:58:05 +00:00
|
|
|
return Runtime::compareEqual(x, Primitive::fromDouble((double) y->booleanValue()));
|
2014-03-12 15:55:06 +00:00
|
|
|
} else {
|
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
#else
|
|
|
|
if ((x->isNumber() || x->isString()) && y->isObject()) {
|
|
|
|
Scope scope(y->objectValue()->engine());
|
|
|
|
ScopedValue py(scope, RuntimeHelpers::toPrimitive(y, PREFERREDTYPE_HINT));
|
|
|
|
return Runtime::compareEqual(x, py);
|
|
|
|
} else if (x->isObject() && (y->isNumber() || y->isString())) {
|
|
|
|
Scope scope(x->objectValue()->engine());
|
|
|
|
ScopedValue px(scope, RuntimeHelpers::toPrimitive(x, PREFERREDTYPE_HINT));
|
|
|
|
return Runtime::compareEqual(px, y);
|
|
|
|
}
|
|
|
|
#endif
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
Bool RuntimeHelpers::strictEqual(const ValueRef x, const ValueRef y)
|
2013-03-06 13:01:07 +00:00
|
|
|
{
|
|
|
|
TRACE2(x, y);
|
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
if (x->rawValue() == y->rawValue())
|
2013-08-05 20:48:35 +00:00
|
|
|
// NaN != NaN
|
2013-09-15 13:46:36 +00:00
|
|
|
return !x->isNaN();
|
2013-08-05 20:48:35 +00:00
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
if (x->isNumber())
|
|
|
|
return y->isNumber() && x->asDouble() == y->asDouble();
|
2013-11-12 08:50:29 +00:00
|
|
|
if (x->isManaged())
|
2014-07-24 09:53:59 +00:00
|
|
|
return y->isManaged() && x->cast<Managed>()->isEqualTo(y->cast<Managed>());
|
2013-03-06 13:01:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::Bool Runtime::compareGreaterThan(const QV4::ValueRef l, const QV4::ValueRef r)
|
2013-09-09 12:33:28 +00:00
|
|
|
{
|
|
|
|
TRACE2(l, r);
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isInteger() && r->isInteger())
|
2013-09-09 12:33:28 +00:00
|
|
|
return l->integerValue() > r->integerValue();
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isNumber() && r->isNumber())
|
|
|
|
return l->asDouble() > r->asDouble();
|
2014-03-12 15:55:06 +00:00
|
|
|
if (l->isString() && r->isString()) {
|
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
return false;
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
return r->stringValue()->compare(l->stringValue());
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-09-09 12:33:28 +00:00
|
|
|
|
|
|
|
if (l->isObject() || r->isObject()) {
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
QV4::ExecutionEngine *e = (l->isObject() ? l->objectValue() : r->objectValue())->engine();
|
2013-09-11 12:47:34 +00:00
|
|
|
QV4::Scope scope(e);
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT));
|
|
|
|
QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT));
|
|
|
|
return Runtime::compareGreaterThan(pl, pr);
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2013-09-09 12:33:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
double dl = RuntimeHelpers::toNumber(l);
|
|
|
|
double dr = RuntimeHelpers::toNumber(r);
|
2013-09-09 12:33:28 +00:00
|
|
|
return dl > dr;
|
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::Bool Runtime::compareLessThan(const QV4::ValueRef l, const QV4::ValueRef r)
|
2013-09-09 12:33:28 +00:00
|
|
|
{
|
|
|
|
TRACE2(l, r);
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isInteger() && r->isInteger())
|
2013-09-09 12:33:28 +00:00
|
|
|
return l->integerValue() < r->integerValue();
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isNumber() && r->isNumber())
|
|
|
|
return l->asDouble() < r->asDouble();
|
2014-03-12 15:55:06 +00:00
|
|
|
if (l->isString() && r->isString()) {
|
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
return false;
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
return l->stringValue()->compare(r->stringValue());
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-09-09 12:33:28 +00:00
|
|
|
|
|
|
|
if (l->isObject() || r->isObject()) {
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
QV4::ExecutionEngine *e = (l->isObject() ? l->objectValue() : r->objectValue())->engine();
|
2013-09-11 12:47:34 +00:00
|
|
|
QV4::Scope scope(e);
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT));
|
|
|
|
QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT));
|
|
|
|
return Runtime::compareLessThan(pl, pr);
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2013-09-09 12:33:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
double dl = RuntimeHelpers::toNumber(l);
|
|
|
|
double dr = RuntimeHelpers::toNumber(r);
|
2013-09-09 12:33:28 +00:00
|
|
|
return dl < dr;
|
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::Bool Runtime::compareGreaterEqual(const QV4::ValueRef l, const QV4::ValueRef r)
|
2013-09-09 12:33:28 +00:00
|
|
|
{
|
|
|
|
TRACE2(l, r);
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isInteger() && r->isInteger())
|
2013-09-09 12:33:28 +00:00
|
|
|
return l->integerValue() >= r->integerValue();
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isNumber() && r->isNumber())
|
|
|
|
return l->asDouble() >= r->asDouble();
|
2014-03-12 15:55:06 +00:00
|
|
|
if (l->isString() && r->isString()) {
|
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
return false;
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
return !l->stringValue()->compare(r->stringValue());
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-09-09 12:33:28 +00:00
|
|
|
|
|
|
|
if (l->isObject() || r->isObject()) {
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
QV4::ExecutionEngine *e = (l->isObject() ? l->objectValue() : r->objectValue())->engine();
|
2013-09-11 12:47:34 +00:00
|
|
|
QV4::Scope scope(e);
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT));
|
|
|
|
QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT));
|
|
|
|
return Runtime::compareGreaterEqual(pl, pr);
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2013-09-09 12:33:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
double dl = RuntimeHelpers::toNumber(l);
|
|
|
|
double dr = RuntimeHelpers::toNumber(r);
|
2013-09-09 12:33:28 +00:00
|
|
|
return dl >= dr;
|
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::Bool Runtime::compareLessEqual(const QV4::ValueRef l, const QV4::ValueRef r)
|
2013-09-09 12:33:28 +00:00
|
|
|
{
|
|
|
|
TRACE2(l, r);
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isInteger() && r->isInteger())
|
2013-09-09 12:33:28 +00:00
|
|
|
return l->integerValue() <= r->integerValue();
|
2014-02-07 08:46:02 +00:00
|
|
|
if (l->isNumber() && r->isNumber())
|
|
|
|
return l->asDouble() <= r->asDouble();
|
2014-03-12 15:55:06 +00:00
|
|
|
if (l->isString() && r->isString()) {
|
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
return false;
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
return !r->stringValue()->compare(l->stringValue());
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-09-09 12:33:28 +00:00
|
|
|
|
|
|
|
if (l->isObject() || r->isObject()) {
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
#else
|
2013-09-09 12:33:28 +00:00
|
|
|
QV4::ExecutionEngine *e = (l->isObject() ? l->objectValue() : r->objectValue())->engine();
|
2013-09-11 12:47:34 +00:00
|
|
|
QV4::Scope scope(e);
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT));
|
|
|
|
QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT));
|
|
|
|
return Runtime::compareLessEqual(pl, pr);
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2013-09-09 12:33:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
double dl = RuntimeHelpers::toNumber(l);
|
|
|
|
double dr = RuntimeHelpers::toNumber(r);
|
2013-09-09 12:33:28 +00:00
|
|
|
return dl <= dr;
|
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
2014-11-11 15:07:54 +00:00
|
|
|
Bool Runtime::compareInstanceof(ExecutionEngine *engine, const ValueRef left, const ValueRef right)
|
2014-07-25 15:44:14 +00:00
|
|
|
{
|
|
|
|
TRACE2(left, right);
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedValue v(scope, Runtime::instanceof(engine, left, right));
|
2014-07-25 15:44:14 +00:00
|
|
|
return v->booleanValue();
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
uint Runtime::compareIn(ExecutionEngine *engine, const ValueRef left, const ValueRef right)
|
2014-07-25 15:44:14 +00:00
|
|
|
{
|
|
|
|
TRACE2(left, right);
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedValue v(scope, Runtime::in(engine, left, right));
|
2014-07-25 15:44:14 +00:00
|
|
|
return v->booleanValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::callGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData)
|
2013-04-14 10:17:11 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-05 12:49:55 +00:00
|
|
|
Q_ASSERT(callData->thisObject.isUndefined());
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Lookup *l = engine->currentContext()->d()->lookups + index;
|
|
|
|
Scoped<FunctionObject> o(scope, l->globalGetter(l, engine));
|
2013-04-14 10:17:11 +00:00
|
|
|
if (!o)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-04-14 10:17:11 +00:00
|
|
|
|
2014-05-06 07:23:59 +00:00
|
|
|
if (o.getPointer() == scope.engine->evalFunction && l->name->equals(scope.engine->id_eval))
|
2013-09-26 10:08:03 +00:00
|
|
|
return static_cast<EvalFunction *>(o.getPointer())->evalCall(callData, true);
|
2013-04-14 10:17:11 +00:00
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
return o->call(callData);
|
2013-04-14 10:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::callActivationProperty(ExecutionEngine *engine, String *name, CallData *callData)
|
2012-04-16 19:23:25 +00:00
|
|
|
{
|
2013-09-05 12:49:55 +00:00
|
|
|
Q_ASSERT(callData->thisObject.isUndefined());
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-05 12:49:55 +00:00
|
|
|
|
2013-09-27 15:04:42 +00:00
|
|
|
ScopedObject base(scope);
|
2014-07-23 11:56:43 +00:00
|
|
|
Object *baseObj = 0;
|
2014-11-11 15:07:54 +00:00
|
|
|
ScopedValue func(scope, engine->currentContext()->getPropertyAndBase(name, baseObj));
|
2014-07-23 11:56:43 +00:00
|
|
|
base.ptr->m = baseObj ? &baseObj->data : 0;
|
2014-05-06 07:23:59 +00:00
|
|
|
if (scope.engine->hasException)
|
2013-10-18 13:42:17 +00:00
|
|
|
return Encode::undefined();
|
|
|
|
|
2013-09-05 12:49:55 +00:00
|
|
|
if (base)
|
2013-09-26 20:07:27 +00:00
|
|
|
callData->thisObject = base;
|
2013-09-05 12:49:55 +00:00
|
|
|
|
2013-09-11 11:10:42 +00:00
|
|
|
FunctionObject *o = func->asFunctionObject();
|
2013-06-03 11:27:38 +00:00
|
|
|
if (!o) {
|
|
|
|
QString objectAsString = QStringLiteral("[null]");
|
2013-09-27 15:04:42 +00:00
|
|
|
if (base)
|
|
|
|
objectAsString = ScopedValue(scope, base.asReturnedValue())->toQStringNoThrow();
|
2013-06-03 11:27:38 +00:00
|
|
|
QString msg = QStringLiteral("Property '%1' of object %2 is not a function").arg(name->toQString()).arg(objectAsString);
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError(msg);
|
2013-06-03 11:27:38 +00:00
|
|
|
}
|
2012-12-01 19:08:26 +00:00
|
|
|
|
2014-05-06 07:23:59 +00:00
|
|
|
if (o == scope.engine->evalFunction && name->equals(scope.engine->id_eval)) {
|
2013-09-26 10:08:03 +00:00
|
|
|
return static_cast<EvalFunction *>(o)->evalCall(callData, true);
|
2013-02-13 13:15:08 +00:00
|
|
|
}
|
2013-01-23 09:07:18 +00:00
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
return o->call(callData);
|
2012-05-09 10:00:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::callProperty(ExecutionEngine *engine, String *name, CallData *callData)
|
2012-05-07 14:05:05 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-13 12:11:55 +00:00
|
|
|
Scoped<Object> baseObject(scope, callData->thisObject);
|
2013-03-07 12:41:21 +00:00
|
|
|
if (!baseObject) {
|
2013-09-17 16:16:35 +00:00
|
|
|
Q_ASSERT(!callData->thisObject.isEmpty());
|
|
|
|
if (callData->thisObject.isNullOrUndefined()) {
|
2013-09-09 12:33:28 +00:00
|
|
|
QString message = QStringLiteral("Cannot call method '%1' of %2").arg(name->toQString()).arg(callData->thisObject.toQStringNoThrow());
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError(message);
|
2013-06-23 13:08:13 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
baseObject = RuntimeHelpers::convertToObject(scope.engine, ValueRef(&callData->thisObject));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!baseObject) // type error
|
|
|
|
return Encode::undefined();
|
2013-09-30 20:41:12 +00:00
|
|
|
callData->thisObject = baseObject.asReturnedValue();
|
2013-01-29 21:07:48 +00:00
|
|
|
}
|
2012-05-09 13:47:55 +00:00
|
|
|
|
2013-09-11 14:28:17 +00:00
|
|
|
Scoped<FunctionObject> o(scope, baseObject->get(name));
|
2013-06-05 23:38:16 +00:00
|
|
|
if (!o) {
|
2013-11-01 11:38:32 +00:00
|
|
|
QString error = QStringLiteral("Property '%1' of object %2 is not a function").arg(name->toQString(), callData->thisObject.toQStringNoThrow());
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError(error);
|
2013-06-05 23:38:16 +00:00
|
|
|
}
|
2012-12-01 19:08:26 +00:00
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
return o->call(callData);
|
2012-10-18 12:39:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::callPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData)
|
2013-02-12 15:23:52 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Lookup *l = engine->currentContext()->d()->lookups + index;
|
2014-01-24 21:55:39 +00:00
|
|
|
Value v;
|
2013-11-02 20:41:58 +00:00
|
|
|
v = l->getter(l, callData->thisObject);
|
2014-01-20 12:51:00 +00:00
|
|
|
if (!v.isObject())
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-02-12 15:23:52 +00:00
|
|
|
|
2014-01-20 12:51:00 +00:00
|
|
|
return v.objectValue()->call(callData);
|
2013-02-12 15:23:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::callElement(ExecutionEngine *engine, const ValueRef index, CallData *callData)
|
2013-01-27 21:39:01 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject baseObject(scope, callData->thisObject.toObject(engine));
|
|
|
|
ScopedString s(scope, index->toString(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
|
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2013-09-26 20:07:27 +00:00
|
|
|
callData->thisObject = baseObject;
|
2013-01-27 21:39:01 +00:00
|
|
|
|
2014-05-07 11:33:24 +00:00
|
|
|
ScopedObject o(scope, baseObject->get(s.getPointer()));
|
2013-01-27 21:39:01 +00:00
|
|
|
if (!o)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-01-27 21:39:01 +00:00
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
return o->call(callData);
|
2013-01-27 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::callValue(ExecutionEngine *engine, const ValueRef func, CallData *callData)
|
2012-05-09 13:47:55 +00:00
|
|
|
{
|
2014-01-20 12:51:00 +00:00
|
|
|
if (!func->isObject())
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-09-05 11:22:23 +00:00
|
|
|
|
2014-01-20 12:51:00 +00:00
|
|
|
return func->objectValue()->call(callData);
|
2012-05-09 09:04:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 21:08:01 +00:00
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::constructGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData)
|
2013-04-14 21:08:01 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-05 12:49:55 +00:00
|
|
|
Q_ASSERT(callData->thisObject.isUndefined());
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
Lookup *l = engine->currentContext()->d()->lookups + index;
|
|
|
|
Scoped<Object> f(scope, l->globalGetter(l, engine));
|
2013-08-30 12:39:40 +00:00
|
|
|
if (!f)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-04-14 21:08:01 +00:00
|
|
|
|
2013-09-11 13:09:25 +00:00
|
|
|
return f->construct(callData);
|
2013-04-14 21:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::constructActivationProperty(ExecutionEngine *engine, String *name, CallData *callData)
|
2012-05-09 13:06:30 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedValue func(scope, engine->currentContext()->getProperty(name));
|
2014-05-06 07:23:59 +00:00
|
|
|
if (scope.engine->hasException)
|
2013-10-18 13:42:17 +00:00
|
|
|
return Encode::undefined();
|
|
|
|
|
2013-09-11 11:10:42 +00:00
|
|
|
Object *f = func->asObject();
|
2013-09-05 12:49:55 +00:00
|
|
|
if (!f)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-09-05 12:49:55 +00:00
|
|
|
|
2013-09-11 13:09:25 +00:00
|
|
|
return f->construct(callData);
|
2012-05-13 11:50:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::constructValue(ExecutionEngine *engine, const ValueRef func, CallData *callData)
|
2012-05-13 11:50:55 +00:00
|
|
|
{
|
2013-09-09 09:07:21 +00:00
|
|
|
Object *f = func->asObject();
|
2013-09-05 12:49:55 +00:00
|
|
|
if (!f)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2012-11-10 11:45:00 +00:00
|
|
|
|
2013-09-11 13:09:25 +00:00
|
|
|
return f->construct(callData);
|
2012-05-09 13:06:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::constructProperty(ExecutionEngine *engine, String *name, CallData *callData)
|
2012-05-09 09:04:57 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject thisObject(scope, callData->thisObject.toObject(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2012-05-09 13:06:30 +00:00
|
|
|
|
2013-09-11 14:28:17 +00:00
|
|
|
Scoped<Object> f(scope, thisObject->get(name));
|
2013-09-05 12:49:55 +00:00
|
|
|
if (!f)
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2012-11-10 11:45:00 +00:00
|
|
|
|
2013-09-11 13:09:25 +00:00
|
|
|
return f->construct(callData);
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::constructPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData)
|
2013-11-13 10:45:51 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Lookup *l = engine->currentContext()->d()->lookups + index;
|
2014-01-24 21:55:39 +00:00
|
|
|
Value v;
|
2013-11-13 10:45:51 +00:00
|
|
|
v = l->getter(l, callData->thisObject);
|
2014-01-20 12:51:00 +00:00
|
|
|
if (!v.isObject())
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->throwTypeError();
|
2013-11-13 10:45:51 +00:00
|
|
|
|
2014-01-20 12:51:00 +00:00
|
|
|
return v.objectValue()->construct(callData);
|
2013-11-13 10:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::throwException(ExecutionEngine *engine, const ValueRef value)
|
2012-06-06 13:30:50 +00:00
|
|
|
{
|
2013-10-21 15:07:45 +00:00
|
|
|
if (!value->isEmpty())
|
2014-11-11 15:07:54 +00:00
|
|
|
engine->throwError(value);
|
2012-06-06 13:30:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::typeofValue(ExecutionEngine *engine, const ValueRef value)
|
2012-12-08 04:31:19 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-09-25 13:24:50 +00:00
|
|
|
ScopedString res(scope);
|
2013-09-09 09:07:21 +00:00
|
|
|
switch (value->type()) {
|
2012-12-08 04:31:19 +00:00
|
|
|
case Value::Undefined_Type:
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_undefined;
|
2012-12-08 04:31:19 +00:00
|
|
|
break;
|
|
|
|
case Value::Null_Type:
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_object;
|
2012-12-08 04:31:19 +00:00
|
|
|
break;
|
|
|
|
case Value::Boolean_Type:
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_boolean;
|
2012-12-08 04:31:19 +00:00
|
|
|
break;
|
2013-09-15 13:46:36 +00:00
|
|
|
case Value::Managed_Type:
|
|
|
|
if (value->isString())
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_string;
|
2013-09-15 13:46:36 +00:00
|
|
|
else if (value->objectValue()->asFunctionObject())
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_function;
|
2012-12-08 04:31:19 +00:00
|
|
|
else
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_object; // ### implementation-defined
|
2012-12-08 04:31:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-11 15:07:54 +00:00
|
|
|
res = engine->id_number;
|
2012-12-08 04:31:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-09-25 13:24:50 +00:00
|
|
|
return res.asReturnedValue();
|
2012-12-08 04:31:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::typeofName(ExecutionEngine *engine, String *name)
|
2012-12-08 04:31:19 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedValue prop(scope, engine->currentContext()->getProperty(name));
|
2013-10-18 13:42:17 +00:00
|
|
|
// typeof doesn't throw. clear any possible exception
|
2014-05-06 07:23:59 +00:00
|
|
|
scope.engine->hasException = false;
|
2014-11-11 15:07:54 +00:00
|
|
|
return Runtime::typeofValue(engine, prop);
|
2012-12-08 04:31:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::typeofMember(ExecutionEngine *engine, const ValueRef base, String *name)
|
2012-12-08 04:31:19 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject obj(scope, base->toObject(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2013-09-09 09:07:21 +00:00
|
|
|
ScopedValue prop(scope, obj->get(name));
|
2014-11-11 15:07:54 +00:00
|
|
|
return Runtime::typeofValue(engine, prop);
|
2012-12-08 04:31:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::typeofElement(ExecutionEngine *engine, const ValueRef base, const ValueRef index)
|
2012-05-23 16:48:48 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedString name(scope, index->toString(engine));
|
|
|
|
ScopedObject obj(scope, base->toObject(engine));
|
2013-10-21 07:57:58 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return Encode::undefined();
|
2014-05-07 11:33:24 +00:00
|
|
|
ScopedValue prop(scope, obj->get(name.getPointer()));
|
2014-11-11 15:07:54 +00:00
|
|
|
return Runtime::typeofValue(engine, prop);
|
2012-05-23 16:48:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::pushWithScope(const ValueRef o, NoThrowEngine *engine)
|
2012-11-24 21:07:02 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedObject obj(scope, o->toObject(engine));
|
|
|
|
engine->currentContext()->newWithContext(obj);
|
2012-11-24 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::unwindException(ExecutionEngine *engine)
|
2013-10-21 15:07:45 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
if (!engine->hasException)
|
2013-10-21 15:07:45 +00:00
|
|
|
return Primitive::emptyValue().asReturnedValue();
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->catchException(engine->currentContext(), 0);
|
2013-10-21 15:07:45 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::pushCatchScope(NoThrowEngine *engine, String *exceptionVarName)
|
2013-02-08 12:33:50 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
ScopedValue v(scope, engine->catchException(engine->currentContext(), 0));
|
|
|
|
engine->currentContext()->newCatchContext(exceptionVarName, v)->getPointer();
|
2013-02-08 12:33:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::popScope(ExecutionEngine *engine)
|
2012-11-24 21:07:02 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
engine->popContext();
|
2012-11-24 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::declareVar(ExecutionEngine *engine, bool deletable, String *name)
|
2012-11-26 22:26:39 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
engine->currentContext()->createMutableBinding(name, deletable);
|
2012-11-26 22:26:39 +00:00
|
|
|
}
|
2012-10-30 06:29:14 +00:00
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::arrayLiteral(ExecutionEngine *engine, Value *values, uint length)
|
2013-03-01 15:47:07 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
Scoped<ArrayObject> a(scope, engine->newArrayObject());
|
2013-03-01 15:47:07 +00:00
|
|
|
|
|
|
|
if (length) {
|
2013-12-16 08:16:57 +00:00
|
|
|
a->arrayReserve(length);
|
2014-01-22 14:25:50 +00:00
|
|
|
a->arrayPut(0, values, length);
|
2013-03-01 15:47:07 +00:00
|
|
|
a->setArrayLengthUnchecked(length);
|
|
|
|
}
|
2013-09-14 09:25:02 +00:00
|
|
|
return a.asReturnedValue();
|
2013-01-24 11:53:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::objectLiteral(ExecutionEngine *engine, const QV4::Value *args, int classId, int arrayValueCount, int arrayGetterSetterCountAndFlags)
|
2013-04-23 05:31:02 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
QV4::InternalClass *klass = engine->currentContext()->d()->compilationUnit->runtimeClasses[classId];
|
|
|
|
Scoped<Object> o(scope, engine->newObject(klass));
|
2013-04-23 05:31:02 +00:00
|
|
|
|
2014-02-15 03:39:33 +00:00
|
|
|
{
|
|
|
|
bool needSparseArray = arrayGetterSetterCountAndFlags >> 30;
|
|
|
|
if (needSparseArray)
|
|
|
|
o->initSparseArray();
|
|
|
|
}
|
|
|
|
|
2014-03-06 11:06:36 +00:00
|
|
|
for (uint i = 0; i < klass->size; ++i)
|
2014-11-01 20:44:57 +00:00
|
|
|
o->memberData()->data()[i] = *args++;
|
2013-04-23 05:31:02 +00:00
|
|
|
|
2014-06-15 06:12:17 +00:00
|
|
|
if (arrayValueCount > 0) {
|
|
|
|
ScopedValue entry(scope);
|
|
|
|
for (int i = 0; i < arrayValueCount; ++i) {
|
|
|
|
uint idx = args->toUInt32();
|
|
|
|
++args;
|
|
|
|
entry = *args++;
|
|
|
|
o->arraySet(idx, entry);
|
|
|
|
}
|
2014-02-15 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 03:39:33 +00:00
|
|
|
uint arrayGetterSetterCount = arrayGetterSetterCountAndFlags & ((1 << 30) - 1);
|
2014-06-15 06:12:17 +00:00
|
|
|
if (arrayGetterSetterCount > 0) {
|
|
|
|
ScopedProperty pd(scope);
|
|
|
|
for (uint i = 0; i < arrayGetterSetterCount; ++i) {
|
|
|
|
uint idx = args->toUInt32();
|
|
|
|
++args;
|
|
|
|
pd->value = *args;
|
|
|
|
++args;
|
|
|
|
pd->set = *args;
|
|
|
|
++args;
|
|
|
|
o->arraySet(idx, pd, Attr_Accessor);
|
|
|
|
}
|
2014-02-15 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 20:02:27 +00:00
|
|
|
return o.asReturnedValue();
|
2013-04-23 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::setupArgumentsObject(ExecutionEngine *engine)
|
2013-08-16 10:54:30 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Q_ASSERT(engine->currentContext()->d()->type >= Heap::ExecutionContext::Type_CallContext);
|
|
|
|
CallContext *c = static_cast<CallContext *>(engine->currentContext());
|
|
|
|
return (engine->memoryManager->alloc<ArgumentsObject>(c))->asReturnedValue();
|
2013-08-16 10:54:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif // V4_BOOTSTRAP
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::ReturnedValue Runtime::increment(const QV4::ValueRef value)
|
2012-12-12 21:46:57 +00:00
|
|
|
{
|
|
|
|
TRACE1(value);
|
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
if (value->isInteger() && value->integerValue() < INT_MAX)
|
|
|
|
return Encode(value->integerValue() + 1);
|
2013-02-14 14:55:43 +00:00
|
|
|
else {
|
2013-09-09 11:38:10 +00:00
|
|
|
double d = value->toNumber();
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(d + 1.);
|
2013-02-14 14:55:43 +00:00
|
|
|
}
|
2012-12-12 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
QV4::ReturnedValue Runtime::decrement(const QV4::ValueRef value)
|
2012-12-12 21:46:57 +00:00
|
|
|
{
|
|
|
|
TRACE1(value);
|
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
if (value->isInteger() && value->integerValue() > INT_MIN)
|
|
|
|
return Encode(value->integerValue() - 1);
|
2013-02-14 14:55:43 +00:00
|
|
|
else {
|
2013-09-09 11:38:10 +00:00
|
|
|
double d = value->toNumber();
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode(d - 1.);
|
2013-02-14 14:55:43 +00:00
|
|
|
}
|
2012-12-12 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
QV4::ReturnedValue RuntimeHelpers::toString(ExecutionEngine *engine, const QV4::ValueRef value)
|
2013-09-13 12:11:55 +00:00
|
|
|
{
|
|
|
|
if (value->isString())
|
|
|
|
return value.asReturnedValue();
|
2014-11-07 17:51:19 +00:00
|
|
|
return RuntimeHelpers::convertToString(engine, value)->asReturnedValue();
|
2013-09-13 12:11:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 17:51:19 +00:00
|
|
|
QV4::ReturnedValue RuntimeHelpers::toObject(ExecutionEngine *engine, const QV4::ValueRef value)
|
2013-09-13 12:11:55 +00:00
|
|
|
{
|
|
|
|
if (value->isObject())
|
|
|
|
return value.asReturnedValue();
|
2013-10-21 07:57:58 +00:00
|
|
|
|
2014-11-11 12:34:18 +00:00
|
|
|
Heap::Object *o = RuntimeHelpers::convertToObject(engine, value);
|
2013-10-21 07:57:58 +00:00
|
|
|
if (!o) // type error
|
|
|
|
return Encode::undefined();
|
|
|
|
|
|
|
|
return Encode(o);
|
2013-09-13 12:11:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif // V4_BOOTSTRAP
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
ReturnedValue Runtime::toDouble(const ValueRef value)
|
2013-08-21 11:45:24 +00:00
|
|
|
{
|
2013-10-09 08:47:11 +00:00
|
|
|
TRACE1(value);
|
2013-09-15 13:46:36 +00:00
|
|
|
return Encode(value->toNumber());
|
2013-08-21 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
int Runtime::toInt(const ValueRef value)
|
2013-07-30 14:53:03 +00:00
|
|
|
{
|
2013-10-09 08:47:11 +00:00
|
|
|
TRACE1(value);
|
2013-09-09 11:38:10 +00:00
|
|
|
return value->toInt32();
|
2013-07-30 14:53:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
int Runtime::doubleToInt(const double &d)
|
2013-07-30 14:53:03 +00:00
|
|
|
{
|
2013-10-09 08:47:11 +00:00
|
|
|
TRACE0();
|
2013-09-25 11:52:15 +00:00
|
|
|
return Primitive::toInt32(d);
|
2013-07-30 14:53:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
unsigned Runtime::toUInt(const ValueRef value)
|
2013-07-30 14:53:03 +00:00
|
|
|
{
|
2013-10-09 08:47:11 +00:00
|
|
|
TRACE1(value);
|
2013-09-09 11:38:10 +00:00
|
|
|
return value->toUInt32();
|
2013-07-30 14:53:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:58:05 +00:00
|
|
|
unsigned Runtime::doubleToUInt(const double &d)
|
2013-07-30 14:53:03 +00:00
|
|
|
{
|
2013-10-09 08:47:11 +00:00
|
|
|
TRACE0();
|
2013-09-25 11:52:15 +00:00
|
|
|
return Primitive::toUInt32(d);
|
2013-07-30 14:53:03 +00:00
|
|
|
}
|
2013-08-16 10:54:30 +00:00
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::regexpLiteral(ExecutionEngine *engine, int id)
|
2013-08-15 13:54:36 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
return engine->currentContext()->d()->compilationUnit->runtimeRegularExpressions[id].asReturnedValue();
|
2013-08-15 13:54:36 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getQmlIdArray(NoThrowEngine *engine)
|
2013-10-23 14:24:58 +00:00
|
|
|
{
|
2014-11-11 12:34:18 +00:00
|
|
|
Q_ASSERT(engine->qmlContextObject());
|
|
|
|
Scope scope(engine);
|
|
|
|
Scoped<QmlContextWrapper> wrapper(scope, engine->qmlContextObject());
|
|
|
|
return wrapper->idObjectsArray();
|
2013-10-23 14:24:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getQmlContextObject(NoThrowEngine *engine)
|
2013-10-24 12:51:02 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
QQmlContextData *context = QmlContextWrapper::callingContext(engine);
|
2014-03-20 15:28:15 +00:00
|
|
|
if (!context)
|
|
|
|
return Encode::undefined();
|
2014-11-11 15:07:54 +00:00
|
|
|
return QObjectWrapper::wrap(engine, context->contextObject);
|
2013-10-24 12:51:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getQmlScopeObject(NoThrowEngine *engine)
|
2013-10-24 12:51:02 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2014-11-11 12:34:18 +00:00
|
|
|
QV4::Scoped<QmlContextWrapper> c(scope, engine->qmlContextObject());
|
2014-11-11 15:07:54 +00:00
|
|
|
return QObjectWrapper::wrap(engine, c->getScopeObject());
|
2013-10-24 12:51:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getQmlQObjectProperty(ExecutionEngine *engine, const ValueRef object, int propertyIndex, bool captureRequired)
|
2013-10-24 12:51:02 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-10-24 12:51:02 +00:00
|
|
|
QV4::Scoped<QObjectWrapper> wrapper(scope, object);
|
|
|
|
if (!wrapper) {
|
2014-11-11 15:07:54 +00:00
|
|
|
engine->throwTypeError(QStringLiteral("Cannot read property of null"));
|
2013-10-24 12:51:02 +00:00
|
|
|
return Encode::undefined();
|
|
|
|
}
|
2014-11-11 15:07:54 +00:00
|
|
|
return QV4::QObjectWrapper::getProperty(wrapper->object(), engine->currentContext(), propertyIndex, captureRequired);
|
2013-11-14 23:06:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::getQmlAttachedProperty(ExecutionEngine *engine, int attachedPropertiesId, int propertyIndex)
|
2013-11-14 23:06:18 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2014-11-11 12:34:18 +00:00
|
|
|
QV4::Scoped<QmlContextWrapper> c(scope, engine->qmlContextObject());
|
2013-11-14 23:06:18 +00:00
|
|
|
QObject *scopeObject = c->getScopeObject();
|
|
|
|
QObject *attachedObject = qmlAttachedPropertiesObjectById(attachedPropertiesId, scopeObject);
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QQmlEngine *qmlEngine = engine->v8Engine->engine();
|
2013-11-14 23:06:18 +00:00
|
|
|
QQmlData::ensurePropertyCache(qmlEngine, attachedObject);
|
2014-11-11 15:07:54 +00:00
|
|
|
return QV4::QObjectWrapper::getProperty(attachedObject, engine->currentContext(), propertyIndex, /*captureRequired*/true);
|
2013-10-24 12:51:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getQmlSingletonQObjectProperty(ExecutionEngine *engine, const ValueRef object, int propertyIndex, bool captureRequired)
|
2014-07-23 17:43:38 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2014-07-23 17:43:38 +00:00
|
|
|
QV4::Scoped<QmlTypeWrapper> wrapper(scope, object);
|
|
|
|
if (!wrapper) {
|
2014-07-28 08:07:57 +00:00
|
|
|
scope.engine->throwTypeError(QStringLiteral("Cannot read property of null"));
|
2014-07-23 17:43:38 +00:00
|
|
|
return Encode::undefined();
|
|
|
|
}
|
2014-11-11 15:07:54 +00:00
|
|
|
return QV4::QObjectWrapper::getProperty(wrapper->singletonObject(), engine->currentContext(), propertyIndex, captureRequired);
|
2014-07-23 17:43:38 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::setQmlQObjectProperty(ExecutionEngine *engine, const ValueRef object, int propertyIndex, const ValueRef value)
|
2013-10-28 14:18:31 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Scope scope(engine);
|
2013-10-28 14:18:31 +00:00
|
|
|
QV4::Scoped<QObjectWrapper> wrapper(scope, object);
|
|
|
|
if (!wrapper) {
|
2014-11-11 15:07:54 +00:00
|
|
|
engine->throwTypeError(QStringLiteral("Cannot write property of null"));
|
2013-10-28 14:18:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-11 15:07:54 +00:00
|
|
|
wrapper->setProperty(engine->currentContext(), propertyIndex, value);
|
2013-10-28 14:18:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
ReturnedValue Runtime::getQmlImportedScripts(NoThrowEngine *engine)
|
2013-10-28 16:02:54 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
QQmlContextData *context = QmlContextWrapper::callingContext(engine);
|
2014-03-20 15:28:15 +00:00
|
|
|
if (!context)
|
|
|
|
return Encode::undefined();
|
2013-10-29 10:59:54 +00:00
|
|
|
return context->importedScripts.value();
|
2013-10-28 16:02:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
QV4::ReturnedValue Runtime::getQmlSingleton(QV4::NoThrowEngine *engine, String *name)
|
2013-11-02 21:46:25 +00:00
|
|
|
{
|
2014-11-11 12:34:18 +00:00
|
|
|
Scope scope(engine);
|
|
|
|
Scoped<QmlContextWrapper> wrapper(scope, engine->qmlContextObject());
|
|
|
|
return wrapper->qmlSingletonWrapper(engine->v8Engine, name);
|
2013-11-02 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:07:54 +00:00
|
|
|
void Runtime::convertThisToObject(ExecutionEngine *engine)
|
2013-11-02 20:10:13 +00:00
|
|
|
{
|
2014-11-11 15:07:54 +00:00
|
|
|
Value *t = &engine->currentContext()->d()->callData->thisObject;
|
2013-11-02 20:10:13 +00:00
|
|
|
if (t->isObject())
|
|
|
|
return;
|
|
|
|
if (t->isNullOrUndefined()) {
|
2014-11-11 15:07:54 +00:00
|
|
|
*t = engine->globalObject->asReturnedValue();
|
2013-11-02 20:10:13 +00:00
|
|
|
} else {
|
2014-11-11 15:07:54 +00:00
|
|
|
*t = t->toObject(engine)->asReturnedValue();
|
2013-11-02 20:10:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif // V4_BOOTSTRAP
|
|
|
|
|
2013-06-24 13:28:00 +00:00
|
|
|
} // namespace QV4
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|