1 | // Copyright (C) 2018 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qv4propertykey_p.h" |
5 | |
6 | #include <QtCore/qstring.h> |
7 | #include <qv4string_p.h> |
8 | #include <qv4engine_p.h> |
9 | #include <qv4scopedvalue_p.h> |
10 | |
11 | QV4::Heap::StringOrSymbol *QV4::PropertyKey::toStringOrSymbol(QV4::ExecutionEngine *e) |
12 | { |
13 | if (isArrayIndex()) |
14 | return Value::fromUInt32(i: asArrayIndex()).toString(e); |
15 | return asStringOrSymbol(); |
16 | } |
17 | |
18 | bool QV4::PropertyKey::isString() const { |
19 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
20 | return s && s->internalClass->vtable->isString; |
21 | } |
22 | |
23 | bool QV4::PropertyKey::isSymbol() const { |
24 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
25 | return s && !s->internalClass->vtable->isString && s->internalClass->vtable->isStringOrSymbol; |
26 | } |
27 | |
28 | bool QV4::PropertyKey::isCanonicalNumericIndexString() const |
29 | { |
30 | if (isArrayIndex()) |
31 | return true; |
32 | if (isSymbol()) |
33 | return false; |
34 | Heap::String *s = static_cast<Heap::String *>(asStringOrSymbol()); |
35 | Scope scope(s->internalClass->engine); |
36 | ScopedString str(scope, s); |
37 | double d = str->toNumber(); |
38 | if (d == 0. && std::signbit(x: d)) |
39 | return true; |
40 | ScopedString converted(scope, Value::fromDouble(d).toString(e: scope.engine)); |
41 | if (converted->equals(other: str)) |
42 | return true; |
43 | return false; |
44 | } |
45 | |
46 | QString QV4::PropertyKey::toQString() const |
47 | { |
48 | if (isArrayIndex()) |
49 | return QString::number(asArrayIndex()); |
50 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
51 | Q_ASSERT(s->internalClass->vtable->isStringOrSymbol); |
52 | return s->toQString(); |
53 | } |
54 | |
55 | QV4::Heap::String *QV4::PropertyKey::asFunctionName(ExecutionEngine *engine, FunctionNamePrefix prefix) const |
56 | { |
57 | QString n; |
58 | if (prefix == Getter) |
59 | n = QStringLiteral("get "); |
60 | else if (prefix == Setter) |
61 | n = QStringLiteral("set "); |
62 | if (isArrayIndex()) |
63 | n += QString::number(asArrayIndex()); |
64 | else { |
65 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
66 | QString str = s->toQString(); |
67 | if (s->internalClass->vtable->isString) |
68 | n += s->toQString(); |
69 | else if (str.size() > 1) |
70 | n += QChar::fromLatin1(c: '[') + QStringView{str}.mid(pos: 1) + QChar::fromLatin1(c: ']'); |
71 | } |
72 | return engine->newString(s: n); |
73 | } |
74 |