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 | #include <private/qv4mm_p.h> |
11 | |
12 | using namespace Qt::Literals::StringLiterals; |
13 | |
14 | QV4::Heap::StringOrSymbol *QV4::PropertyKey::toStringOrSymbol(QV4::ExecutionEngine *e) |
15 | { |
16 | if (isArrayIndex()) |
17 | return Value::fromUInt32(i: asArrayIndex()).toString(e); |
18 | return asStringOrSymbol(); |
19 | } |
20 | |
21 | bool QV4::PropertyKey::isString() const { |
22 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
23 | return s && s->internalClass->vtable->isString; |
24 | } |
25 | |
26 | bool QV4::PropertyKey::isSymbol() const { |
27 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
28 | return s && !s->internalClass->vtable->isString && s->internalClass->vtable->isStringOrSymbol; |
29 | } |
30 | |
31 | bool QV4::PropertyKey::isCanonicalNumericIndexString() const |
32 | { |
33 | if (isArrayIndex()) |
34 | return true; |
35 | if (isSymbol()) |
36 | return false; |
37 | Heap::String *s = static_cast<Heap::String *>(asStringOrSymbol()); |
38 | Scope scope(s->internalClass->engine); |
39 | ScopedString str(scope, s); |
40 | double d = str->toNumber(); |
41 | if (d == 0. && std::signbit(x: d)) |
42 | return true; |
43 | ScopedString converted(scope, Value::fromDouble(d).toString(e: scope.engine)); |
44 | if (converted->equals(other: str)) |
45 | return true; |
46 | return false; |
47 | } |
48 | |
49 | QString QV4::PropertyKey::toQString() const |
50 | { |
51 | if (isArrayIndex()) |
52 | return QString::number(asArrayIndex()); |
53 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
54 | Q_ASSERT(s->internalClass->vtable->isStringOrSymbol); |
55 | return s->toQString(); |
56 | } |
57 | |
58 | QV4::Heap::String *QV4::PropertyKey::asFunctionName(ExecutionEngine *engine, FunctionNamePrefix prefix) const |
59 | { |
60 | QString n; |
61 | if (prefix == Getter) |
62 | n += "get "_L1; |
63 | else if (prefix == Setter) |
64 | n += "set "_L1; |
65 | if (isArrayIndex()) |
66 | n += QString::number(asArrayIndex()); |
67 | else { |
68 | Heap::StringOrSymbol *s = asStringOrSymbol(); |
69 | QString str = s->toQString(); |
70 | if (s->internalClass->vtable->isString) |
71 | n += s->toQString(); |
72 | else if (str.size() > 1) |
73 | n += QChar::fromLatin1(c: '[') + QStringView{str}.mid(pos: 1) + QChar::fromLatin1(c: ']'); |
74 | } |
75 | return engine->newString(s: n); |
76 | } |
77 |