Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4symbol.cpp
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#include <qv4symbol_p.h>
6#include <qv4functionobject_p.h>
7#include <qv4identifiertable_p.h>
8
9using namespace QV4;
10
14
15void Heap::Symbol::init(const QString &s)
16{
17 Q_ASSERT(s.at(0) == QLatin1Char('@'));
18 QString desc(s);
19 StringOrSymbol::init(desc.data_ptr());
20 identifier = PropertyKey::fromStringOrSymbol(internalClass->engine, this);
21}
22
23void Heap::SymbolCtor::init(QV4::ExecutionEngine *engine)
24{
25 Heap::FunctionObject::init(engine, QStringLiteral("Symbol"));
26}
27
28void Heap::SymbolObject::init(const QV4::Symbol *s)
29{
30 Object::init();
31 symbol.set(internalClass->engine, s->d());
32}
33
34ReturnedValue QV4::SymbolCtor::virtualCall(const QV4::FunctionObject *f, const QV4::Value *, const QV4::Value *argv, int argc)
35{
36 Scope scope(f);
37 QString desc = QChar::fromLatin1('@');
38 if (argc && !argv[0].isUndefined()) {
39 ScopedString s(scope, argv[0].toString(scope.engine));
40 if (scope.hasException())
41 return Encode::undefined();
42 desc += s->toQString();
43 }
44 return Symbol::create(scope.engine, desc)->asReturnedValue();
45}
46
47ReturnedValue SymbolCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *, int, const Value *)
48{
49 return f->engine()->throwTypeError(QStringLiteral("Symbol can't be used together with |new|."));
50}
51
52ReturnedValue SymbolCtor::method_for(const FunctionObject *f, const Value *, const Value *argv, int argc)
53{
54 Scope scope(f);
55 ScopedValue k(scope, argc ? argv[0] : Value::undefinedValue());
56 ScopedString key(scope, k->toString(scope.engine));
57 if (scope.hasException())
58 return Encode::undefined();
59 QString desc = QLatin1Char('@') + key->toQString();
60 return scope.engine->identifierTable->insertSymbol(desc)->asReturnedValue();
61}
62
63ReturnedValue SymbolCtor::method_keyFor(const FunctionObject *f, const Value *, const Value *argv, int argc)
64{
65 ExecutionEngine *e = f->engine();
66 if (!argc || !argv[0].isSymbol())
67 return e->throwTypeError(QLatin1String("Symbol.keyFor: Argument is not a symbol."));
68 const Symbol &arg = static_cast<const Symbol &>(argv[0]);
69 Heap::Symbol *s = e->identifierTable->symbolForId(arg.propertyKey());
70 Q_ASSERT(!s || s == arg.d());
71 if (s)
72 return e->newString(arg.toQString().mid((1)))->asReturnedValue();
73 return Encode::undefined();
74}
75
76void SymbolPrototype::init(ExecutionEngine *engine, Object *ctor)
77{
78 Scope scope(engine);
79 ScopedValue v(scope);
80 ctor->defineReadonlyProperty(engine->id_prototype(), (v = this));
81 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(0));
82
83 ctor->defineDefaultProperty(QStringLiteral("for"), SymbolCtor::method_for, 1);
84 ctor->defineDefaultProperty(QStringLiteral("keyFor"), SymbolCtor::method_keyFor, 1);
85 ctor->defineReadonlyProperty(QStringLiteral("hasInstance"), *engine->symbol_hasInstance());
86 ctor->defineReadonlyProperty(QStringLiteral("isConcatSpreadable"), *engine->symbol_isConcatSpreadable());
87 ctor->defineReadonlyProperty(QStringLiteral("iterator"), *engine->symbol_iterator());
88 ctor->defineReadonlyProperty(QStringLiteral("match"), *engine->symbol_match());
89 ctor->defineReadonlyProperty(QStringLiteral("replace"), *engine->symbol_replace());
90 ctor->defineReadonlyProperty(QStringLiteral("search"), *engine->symbol_search());
91 ctor->defineReadonlyProperty(QStringLiteral("species"), *engine->symbol_species());
92 ctor->defineReadonlyProperty(QStringLiteral("split"), *engine->symbol_split());
93 ctor->defineReadonlyProperty(QStringLiteral("toPrimitive"), *engine->symbol_toPrimitive());
94 ctor->defineReadonlyProperty(QStringLiteral("toStringTag"), *engine->symbol_toStringTag());
95 ctor->defineReadonlyProperty(QStringLiteral("unscopables"), *engine->symbol_unscopables());
96
97 defineDefaultProperty(QStringLiteral("constructor"), (v = ctor));
98 defineDefaultProperty(QStringLiteral("toString"), method_toString);
99 defineDefaultProperty(QStringLiteral("valueOf"), method_valueOf);
100 defineDefaultProperty(engine->symbol_toPrimitive(), method_symbolToPrimitive, 1, Attr_ReadOnly_ButConfigurable);
101
102 v = engine->newString(QStringLiteral("Symbol"));
103 defineReadonlyConfigurableProperty(engine->symbol_toStringTag(), v);
104
105}
106
107ReturnedValue SymbolPrototype::method_toString(const FunctionObject *f, const Value *thisObject, const Value *, int)
108{
109 Scope scope(f);
110 Scoped<Symbol> s(scope, thisObject->as<Symbol>());
111 if (!s) {
112 if (const SymbolObject *o = thisObject->as<SymbolObject>())
113 s = o->d()->symbol;
114 else
115 return scope.engine->throwTypeError();
116 }
117 return scope.engine->newString(s->descriptiveString())->asReturnedValue();
118}
119
120ReturnedValue SymbolPrototype::method_valueOf(const FunctionObject *f, const Value *thisObject, const Value *, int)
121{
122 Scope scope(f);
123 Scoped<Symbol> s(scope, thisObject->as<Symbol>());
124 if (!s) {
125 if (const SymbolObject *o = thisObject->as<SymbolObject>())
126 s = o->d()->symbol;
127 else
128 return scope.engine->throwTypeError();
129 }
130 return s->asReturnedValue();
131}
132
133ReturnedValue SymbolPrototype::method_symbolToPrimitive(const FunctionObject *f, const Value *thisObject, const Value *, int)
134{
135 if (thisObject->isSymbol())
136 return thisObject->asReturnedValue();
137 if (const SymbolObject *o = thisObject->as<SymbolObject>())
138 return o->d()->symbol->asReturnedValue();
139 return f->engine()->throwTypeError();
140}
141
142Heap::Symbol *Symbol::create(ExecutionEngine *e, const QString &s)
143{
144 Q_ASSERT(s.at(0) == QLatin1Char('@'));
145 return e->memoryManager->alloc<Symbol>(s);
146}
147
148QString Symbol::descriptiveString() const
149{
150 return QLatin1String("Symbol(") + QStringView{toQString()}.mid(1) + QLatin1String(")");
151}
Definition qjsvalue.h:23
Scoped< String > ScopedString
#define QStringLiteral(str)
Definition qstring.h:1826
DEFINE_OBJECT_VTABLE(SymbolObject)
DEFINE_MANAGED_VTABLE(Symbol)
DEFINE_OBJECT_VTABLE(SymbolCtor)
static V4_NEEDS_DESTROY Heap::Symbol * create(ExecutionEngine *e, const QString &s)