diff options
Diffstat (limited to 'src/qml/jsruntime/qv4value.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4value.cpp | 192 |
1 files changed, 91 insertions, 101 deletions
diff --git a/src/qml/jsruntime/qv4value.cpp b/src/qml/jsruntime/qv4value.cpp index d29b060b9e..223a004602 100644 --- a/src/qml/jsruntime/qv4value.cpp +++ b/src/qml/jsruntime/qv4value.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// 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 <qv4propertykey_p.h> @@ -109,7 +73,7 @@ double Value::toNumberImpl(Value val) Scope scope(val.objectValue()->engine()); ScopedValue protectThis(scope, val); ScopedValue prim(scope, RuntimeHelpers::toPrimitive(val, NUMBER_HINT)); - if (scope.engine->hasException) + if (scope.hasException()) return 0; return prim->toNumber(); } @@ -122,100 +86,122 @@ double Value::toNumberImpl(Value val) } } -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 @@ -250,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; } @@ -269,6 +257,8 @@ bool Value::sameValueZero(Value other) const { return true; } } + if (isManaged()) + return other.isManaged() && cast<Managed>()->isEqualTo(other.cast<Managed>()); return false; } |