Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include <qv4symbol_p.h>
7
8using namespace QV4;
9
13
15{
16 Q_ASSERT(s.at(0) == QLatin1Char('@'));
17 QString desc(s);
18 StringOrSymbol::init(desc.data_ptr());
20}
21
23{
24 Heap::FunctionObject::init(engine, QStringLiteral("Symbol"));
25}
26
27void Heap::SymbolObject::init(const QV4::Symbol *s)
28{
29 Object::init();
30 symbol.set(internalClass->engine, s->d());
31}
32
34{
35 Scope scope(f);
36 QString desc = QChar::fromLatin1('@');
37 if (argc && !argv[0].isUndefined()) {
38 ScopedString s(scope, argv[0].toString(scope.engine));
39 if (scope.hasException())
40 return Encode::undefined();
41 desc += s->toQString();
42 }
43 return Symbol::create(scope.engine, desc)->asReturnedValue();
44}
45
47{
48 return f->engine()->throwTypeError(QStringLiteral("Symbol can't be used together with |new|."));
49}
50
51ReturnedValue SymbolCtor::method_for(const FunctionObject *f, const Value *, const Value *argv, int argc)
52{
53 Scope scope(f);
54 ScopedValue k(scope, argc ? argv[0] : Value::undefinedValue());
55 ScopedString key(scope, k->toString(scope.engine));
56 if (scope.hasException())
57 return Encode::undefined();
58 QString desc = QLatin1Char('@') + key->toQString();
59 return scope.engine->identifierTable->insertSymbol(desc)->asReturnedValue();
60}
61
62ReturnedValue SymbolCtor::method_keyFor(const FunctionObject *f, const Value *, const Value *argv, int argc)
63{
64 ExecutionEngine *e = f->engine();
65 if (!argc || !argv[0].isSymbol())
66 return e->throwTypeError(QLatin1String("Symbol.keyFor: Argument is not a symbol."));
67 const Symbol &arg = static_cast<const Symbol &>(argv[0]);
68 Heap::Symbol *s = e->identifierTable->symbolForId(arg.propertyKey());
69 Q_ASSERT(!s || s == arg.d());
70 if (s)
71 return e->newString(arg.toQString().mid((1)))->asReturnedValue();
72 return Encode::undefined();
73}
74
76{
77 Scope scope(engine);
78 ScopedValue v(scope);
79 ctor->defineReadonlyProperty(engine->id_prototype(), (v = this));
80 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(0));
81
82 ctor->defineDefaultProperty(QStringLiteral("for"), SymbolCtor::method_for, 1);
83 ctor->defineDefaultProperty(QStringLiteral("keyFor"), SymbolCtor::method_keyFor, 1);
84 ctor->defineReadonlyProperty(QStringLiteral("hasInstance"), *engine->symbol_hasInstance());
85 ctor->defineReadonlyProperty(QStringLiteral("isConcatSpreadable"), *engine->symbol_isConcatSpreadable());
86 ctor->defineReadonlyProperty(QStringLiteral("iterator"), *engine->symbol_iterator());
87 ctor->defineReadonlyProperty(QStringLiteral("match"), *engine->symbol_match());
88 ctor->defineReadonlyProperty(QStringLiteral("replace"), *engine->symbol_replace());
89 ctor->defineReadonlyProperty(QStringLiteral("search"), *engine->symbol_search());
90 ctor->defineReadonlyProperty(QStringLiteral("species"), *engine->symbol_species());
91 ctor->defineReadonlyProperty(QStringLiteral("split"), *engine->symbol_split());
92 ctor->defineReadonlyProperty(QStringLiteral("toPrimitive"), *engine->symbol_toPrimitive());
93 ctor->defineReadonlyProperty(QStringLiteral("toStringTag"), *engine->symbol_toStringTag());
94 ctor->defineReadonlyProperty(QStringLiteral("unscopables"), *engine->symbol_unscopables());
95
96 defineDefaultProperty(QStringLiteral("constructor"), (v = ctor));
97 defineDefaultProperty(QStringLiteral("toString"), method_toString);
98 defineDefaultProperty(QStringLiteral("valueOf"), method_valueOf);
99 defineDefaultProperty(engine->symbol_toPrimitive(), method_symbolToPrimitive, 1, Attr_ReadOnly_ButConfigurable);
100
101 v = engine->newString(QStringLiteral("Symbol"));
102 defineReadonlyConfigurableProperty(engine->symbol_toStringTag(), v);
103
104}
105
107{
108 Scope scope(f);
109 Scoped<Symbol> s(scope, thisObject->as<Symbol>());
110 if (!s) {
111 if (const SymbolObject *o = thisObject->as<SymbolObject>())
112 s = o->d()->symbol;
113 else
114 return scope.engine->throwTypeError();
115 }
116 return scope.engine->newString(s->descriptiveString())->asReturnedValue();
117}
118
120{
121 Scope scope(f);
122 Scoped<Symbol> s(scope, thisObject->as<Symbol>());
123 if (!s) {
124 if (const SymbolObject *o = thisObject->as<SymbolObject>())
125 s = o->d()->symbol;
126 else
127 return scope.engine->throwTypeError();
128 }
129 return s->asReturnedValue();
130}
131
133{
134 if (thisObject->isSymbol())
135 return thisObject->asReturnedValue();
136 if (const SymbolObject *o = thisObject->as<SymbolObject>())
137 return o->d()->symbol->asReturnedValue();
138 return f->engine()->throwTypeError();
139}
140
142{
143 Q_ASSERT(s.at(0) == QLatin1Char('@'));
144 return e->memoryManager->alloc<Symbol>(s);
145}
146
148{
149 return QLatin1String("Symbol(") + QStringView{toQString()}.mid(1) + QLatin1String(")");
150}
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
ManagedType::Data * alloc(Args &&... args)
Definition qv4mm_p.h:307
quint64 ReturnedValue
Scoped< String > ScopedString
@ Attr_ReadOnly_ButConfigurable
static const void * symbol()
static QString toString(QV4::ReturnedValue v)
GLsizei const GLfloat * v
[13]
GLuint64 key
GLfloat GLfloat f
GLdouble s
[6]
Definition qopenglext.h:235
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
SSL_CTX int void * arg
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
#define DEFINE_OBJECT_VTABLE(classname)
#define DEFINE_MANAGED_VTABLE(classname)
QJSEngine engine
[0]
\inmodule QtCore \reentrant
Definition qchar.h:18
static constexpr ReturnedValue undefined()
IdentifierTable * identifierTable
MemoryManager * memoryManager
Heap::String * newString(char16_t c)
ReturnedValue throwTypeError()
ReturnedValue asReturnedValue() const
Definition qv4value_p.h:342
Pointer< InternalClass *, 0 > internalClass
Definition qv4heap_p.h:63
QString toQString() const
Definition qv4string_p.h:65
void init(ExecutionEngine *engine)
Definition qv4symbol.cpp:22
void init(const QString &s)
Definition qv4symbol.cpp:14
Heap::Symbol * symbolForId(PropertyKey i) const
Heap::Symbol * insertSymbol(const QString &s)
static PropertyKey fromStringOrSymbol(Engine *engine, StringOrSymbol *b)
bool hasException() const
ExecutionEngine * engine
constexpr ReturnedValue asReturnedValue() const
static ReturnedValue method_keyFor(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
Definition qv4symbol.cpp:62
static ReturnedValue method_for(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
Definition qv4symbol.cpp:51
static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
void init(ExecutionEngine *engine, Object *ctor)
Definition qv4symbol.cpp:75
static ReturnedValue method_symbolToPrimitive(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_valueOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static V4_NEEDS_DESTROY Heap::Symbol * create(ExecutionEngine *e, const QString &s)
QString descriptiveString() const
static constexpr VTable::CallAsConstructor virtualCallAsConstructor
static constexpr VTable::Call virtualCall
static constexpr Value fromInt32(int i)
Definition qv4value_p.h:187
Heap::String * toString(ExecutionEngine *e) const
Definition qv4value_p.h:114
static constexpr Value undefinedValue()
Definition qv4value_p.h:191
const T * as() const
Definition qv4value_p.h:132
bool isSymbol() const
Definition qv4value_p.h:296