aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4value.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/jsruntime/qv4value.cpp')
-rw-r--r--src/qml/jsruntime/qv4value.cpp221
1 files changed, 98 insertions, 123 deletions
diff --git a/src/qml/jsruntime/qv4value.cpp b/src/qml/jsruntime/qv4value.cpp
index cbc153bb86..223a004602 100644
--- a/src/qml/jsruntime/qv4value.cpp
+++ b/src/qml/jsruntime/qv4value.cpp
@@ -1,51 +1,13 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtQml 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 The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-#include <qv4engine_p.h>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
#include <qv4runtime_p.h>
-#include <qv4string_p.h>
#include <qv4propertykey_p.h>
-#ifndef V4_BOOTSTRAP
+#include <qv4string_p.h>
#include <qv4symbol_p.h>
#include <qv4object_p.h>
#include <qv4objectproto_p.h>
#include <private/qv4mm_p.h>
-#endif
#include <wtf/MathExtras.h>
@@ -83,12 +45,8 @@ bool Value::toBooleanImpl(Value val)
Heap::Base *b = val.m();
if (!b)
return false;
-#ifdef V4_BOOTSTRAP
- Q_UNIMPLEMENTED();
-#else
if (b->internalClass->vtable->isString)
return static_cast<Heap::String *>(b)->length() > 0;
-#endif
return true;
}
@@ -103,10 +61,6 @@ double Value::toNumberImpl(Value val)
case QV4::Value::Undefined_Type:
return std::numeric_limits<double>::quiet_NaN();
case QV4::Value::Managed_Type:
-#ifdef V4_BOOTSTRAP
- Q_UNIMPLEMENTED();
- Q_FALLTHROUGH();
-#else
if (String *s = val.stringValue())
return RuntimeHelpers::stringToNumber(s->toQString());
if (val.isSymbol()) {
@@ -114,16 +68,15 @@ double Value::toNumberImpl(Value val)
m.engine()->throwTypeError();
return 0;
}
- {
- Q_ASSERT(val.isObject());
- Scope scope(val.objectValue()->engine());
- ScopedValue protectThis(scope, val);
- ScopedValue prim(scope, RuntimeHelpers::toPrimitive(val, NUMBER_HINT));
- if (scope.engine->hasException)
+ {
+ Q_ASSERT(val.isObject());
+ Scope scope(val.objectValue()->engine());
+ ScopedValue protectThis(scope, val);
+ ScopedValue prim(scope, RuntimeHelpers::toPrimitive(val, NUMBER_HINT));
+ if (scope.hasException())
return 0;
return prim->toNumber();
}
-#endif
case QV4::Value::Null_Type:
case QV4::Value::Boolean_Type:
case QV4::Value::Integer_Type:
@@ -133,101 +86,122 @@ double Value::toNumberImpl(Value val)
}
}
-#ifndef V4_BOOTSTRAP
-QString Value::toQStringNoThrow() const
+static QString primitiveToQString(const Value *value)
{
- switch (type()) {
+ switch (value->type()) {
case Value::Empty_Type:
Q_ASSERT(!"empty Value encountered");
- Q_UNREACHABLE();
+ Q_UNREACHABLE_RETURN(QString());
case Value::Undefined_Type:
return QStringLiteral("undefined");
case Value::Null_Type:
return QStringLiteral("null");
case Value::Boolean_Type:
- if (booleanValue())
+ if (value->booleanValue())
return QStringLiteral("true");
else
return QStringLiteral("false");
case Value::Managed_Type:
+ Q_UNREACHABLE_RETURN(QString());
+ case Value::Integer_Type: {
+ QString str;
+ RuntimeHelpers::numberToString(&str, (double)value->int_32(), 10);
+ return str;
+ }
+ case Value::Double_Type: {
+ QString str;
+ RuntimeHelpers::numberToString(&str, value->doubleValue(), 10);
+ return str;
+ }
+ } // switch
+
+ Q_UNREACHABLE_RETURN(QString());
+}
+
+
+QString Value::toQStringNoThrow() const
+{
+ if (isManaged()) {
if (String *s = stringValue())
return s->toQString();
if (Symbol *s = symbolValue())
return s->descriptiveString();
- {
- Q_ASSERT(isObject());
- Scope scope(objectValue()->engine());
- ScopedValue ex(scope);
- bool caughtException = false;
- ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
+
+ Q_ASSERT(isObject());
+ Scope scope(objectValue()->engine());
+ ScopedValue ex(scope);
+ bool caughtException = false;
+ ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
+ if (scope.hasException()) {
+ ex = scope.engine->catchException();
+ caughtException = true;
+ } else if (prim->isPrimitive()) {
+ return prim->toQStringNoThrow();
+ }
+
+ // Can't nest try/catch due to CXX ABI limitations for foreign exception nesting.
+ if (caughtException) {
+ ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ex, STRING_HINT));
if (scope.hasException()) {
ex = scope.engine->catchException();
- caughtException = true;
} else if (prim->isPrimitive()) {
- return prim->toQStringNoThrow();
- }
- // Can't nest try/catch due to CXX ABI limitations for foreign exception nesting.
- if (caughtException) {
- ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ex, STRING_HINT));
- if (scope.hasException()) {
- ex = scope.engine->catchException();
- } else if (prim->isPrimitive()) {
- return prim->toQStringNoThrow();
- }
+ return prim->toQStringNoThrow();
}
- return QString();
}
- case Value::Integer_Type: {
- QString str;
- RuntimeHelpers::numberToString(&str, (double)int_32(), 10);
- return str;
- }
- default: { // double
- QString str;
- RuntimeHelpers::numberToString(&str, doubleValue(), 10);
- return str;
+
+ return QString();
}
- } // switch
+
+ return primitiveToQString(this);
}
QString Value::toQString() const
{
- switch (type()) {
- case Value::Empty_Type:
- Q_ASSERT(!"empty Value encountered");
- Q_UNREACHABLE();
- case Value::Undefined_Type:
- return QStringLiteral("undefined");
- case Value::Null_Type:
- return QStringLiteral("null");
- case Value::Boolean_Type:
- if (booleanValue())
- return QStringLiteral("true");
- else
- return QStringLiteral("false");
- case Value::Managed_Type:
- if (String *s = stringValue()) {
+ if (isManaged()) {
+ if (String *s = stringValue())
return s->toQString();
- } else if (isSymbol()) {
+
+ if (isSymbol()) {
static_cast<const Managed *>(this)->engine()->throwTypeError();
return QString();
- } else {
- Q_ASSERT(isObject());
- Scope scope(objectValue()->engine());
- ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
- return prim->toQString();
}
- case Value::Integer_Type: {
- QString str;
- RuntimeHelpers::numberToString(&str, (double)int_32(), 10);
- return str;
+
+ Q_ASSERT(isObject());
+ Scope scope(objectValue()->engine());
+ ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
+ return prim->toQString();
}
- default: { // double
- QString str;
- RuntimeHelpers::numberToString(&str, doubleValue(), 10);
- return str;
+
+ return primitiveToQString(this);
+}
+
+QString Value::toQString(bool *ok) const
+{
+ if (isManaged()) {
+ if (String *s = stringValue()) {
+ *ok = true;
+ return s->toQString();
+ }
+
+ if (isSymbol()) {
+ static_cast<const Managed *>(this)->engine()->throwTypeError();
+ *ok = false;
+ return QString();
+ }
+
+ Q_ASSERT(isObject());
+ Scope scope(objectValue()->engine());
+ ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
+
+ if (scope.hasException()) {
+ *ok = false;
+ return QString();
+ }
+
+ return prim->toQString(ok);
}
- } // switch
+
+ return primitiveToQString(this);
}
QV4::PropertyKey Value::toPropertyKey(ExecutionEngine *e) const
@@ -248,7 +222,6 @@ QV4::PropertyKey Value::toPropertyKey(ExecutionEngine *e) const
ScopedStringOrSymbol s(scope, v);
return s->toPropertyKey();
}
-#endif // V4_BOOTSTRAP
bool Value::sameValue(Value other) const {
if (_val == other._val)
@@ -263,6 +236,8 @@ bool Value::sameValue(Value other) const {
if (isDouble() && other.isInteger())
return other.int_32() ? (doubleValue() == double(other.int_32()))
: (doubleValue() == 0 && !std::signbit(doubleValue()));
+ if (isManaged())
+ return other.isManaged() && cast<Managed>()->isEqualTo(other.cast<Managed>());
return false;
}
@@ -282,10 +257,11 @@ bool Value::sameValueZero(Value other) const {
return true;
}
}
+ if (isManaged())
+ return other.isManaged() && cast<Managed>()->isEqualTo(other.cast<Managed>());
return false;
}
-#ifndef V4_BOOTSTRAP
Heap::String *Value::toString(ExecutionEngine *e, Value val)
{
return RuntimeHelpers::convertToString(e, val);
@@ -327,4 +303,3 @@ uint Value::asArrayLength(bool *ok) const
}
return idx;
}
-#endif // V4_BOOTSTRAP