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
qv4engine.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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 "qv4engine_p.h"
6
7#include <wtf/BumpPointerAllocator.h>
8#include <wtf/OSAllocator.h>
9#include <wtf/PageAllocation.h>
10
11#include <private/qjsvalue_p.h>
12#include <private/qqmlbuiltinfunctions_p.h>
13#include <private/qqmlengine_p.h>
14#include <private/qqmljsdiagnosticmessage_p.h>
15#include <private/qqmllist_p.h>
16#include <private/qqmllistwrapper_p.h>
17#include <private/qqmlscriptblob_p.h>
18#include <private/qqmlscriptdata_p.h>
19#include <private/qqmltypeloader_p.h>
20#include <private/qqmltypewrapper_p.h>
21#include <private/qqmlvaluetype_p.h>
22#include <private/qqmlvaluetypewrapper_p.h>
23#include <private/qv4argumentsobject_p.h>
24#include <private/qv4arraybuffer_p.h>
25#include <private/qv4arrayiterator_p.h>
26#include <private/qv4arrayobject_p.h>
27#include <private/qv4atomics_p.h>
28#include <private/qv4booleanobject_p.h>
29#include <private/qv4codegen_p.h>
30#include <private/qv4compileddata_p.h>
31#include <private/qv4dataview_p.h>
32#include <private/qv4dateobject_p.h>
33#include <private/qv4debugging_p.h>
34#include <private/qv4errorobject_p.h>
35#include <private/qv4executableallocator_p.h>
36#include <private/qv4function_p.h>
37#include <private/qv4functionobject_p.h>
38#include <private/qv4generatorobject_p.h>
39#include <private/qv4globalobject_p.h>
40#include <private/qv4identifiertable_p.h>
41#include <private/qv4iterator_p.h>
42#include <private/qv4jsonobject_p.h>
43#include <private/qv4mapiterator_p.h>
44#include <private/qv4mapobject_p.h>
45#include <private/qv4mathobject_p.h>
46#include <private/qv4memberdata_p.h>
47#include <private/qv4mm_p.h>
48#include <private/qv4module_p.h>
49#include <private/qv4numberobject_p.h>
50#include <private/qv4object_p.h>
51#include <private/qv4objectiterator_p.h>
52#include <private/qv4objectproto_p.h>
53#include <private/qv4profiling_p.h>
54#include <private/qv4promiseobject_p.h>
55#include <private/qv4proxy_p.h>
56#include <private/qv4qmetaobjectwrapper_p.h>
57#include <private/qv4qmlcontext_p.h>
58#include <private/qv4qobjectwrapper_p.h>
59#include <private/qv4reflect_p.h>
60#include <private/qv4regexp_p.h>
61#include <private/qv4regexpobject_p.h>
62#include <private/qv4runtime_p.h>
63#include <private/qv4sequenceobject_p.h>
64#include <private/qv4setiterator_p.h>
65#include <private/qv4setobject_p.h>
66#include <private/qv4sqlerrors_p.h>
67#include <private/qv4stackframe_p.h>
68#include <private/qv4stacklimits_p.h>
69#include <private/qv4stringiterator_p.h>
70#include <private/qv4stringobject_p.h>
71#include <private/qv4symbol_p.h>
72#include <private/qv4typedarray_p.h>
73#include <private/qv4urlobject_p.h>
74#include <private/qv4value_p.h>
75#include <private/qv4variantassociationobject_p.h>
76#include <private/qv4variantobject_p.h>
77
78#include <QtQml/qqmlfile.h>
79
80#include <QtCore/qdatetime.h>
81#include <QtCore/qdir.h>
82#include <QtCore/qfileinfo.h>
83#include <QtCore/qiterable.h>
84#include <QtCore/qloggingcategory.h>
85#include <QtCore/qmetatype.h>
86#include <QtCore/qmetasequence.h>
87#include <QtCore/qtextstream.h>
88#include <QtCore/qtimezone.h>
89
90#if QT_CONFIG(regularexpression)
91#include <QtCore/qregularexpression.h>
92#endif
93#if QT_CONFIG(qml_locale)
94#include <private/qqmllocale_p.h>
95#endif
96#if QT_CONFIG(qml_xml_http_request)
97#include <private/qv4domerrors_p.h>
98#include <private/qqmlxmlhttprequest_p.h>
99#endif
100#ifdef V4_USE_VALGRIND
101#include <valgrind/memcheck.h>
102#endif
103
105
106DEFINE_BOOL_CONFIG_OPTION(disableDiskCache, QML_DISABLE_DISK_CACHE);
108
109using namespace QV4;
110
111// While engineSerial is odd the statics haven't been initialized. The engine that receives ID 1
112// initializes the statics and sets engineSerial to 2 afterwards.
113// Each engine does engineSerial.fetchAndAddOrdered(2) on creation. Therefore engineSerial stays
114// odd while the statics are being initialized, and stays even afterwards.
115// Any further engines created while the statics are being initialized busy-wait until engineSerial
116// is even.
117Q_CONSTINIT static QBasicAtomicInt engineSerial = Q_BASIC_ATOMIC_INITIALIZER(1);
118Q_CONSTINIT static QBasicAtomicInt hasPreview = Q_BASIC_ATOMIC_INITIALIZER(0);
119int ExecutionEngine::s_maxCallDepth = -1;
120int ExecutionEngine::s_jitCallCountThreshold = 3;
121int ExecutionEngine::s_maxJSStackSize = 4 * 1024 * 1024;
122int ExecutionEngine::s_maxGCStackSize = 2 * 1024 * 1024;
123int ExecutionEngine::s_stackSizeSoftLimit = -1;
124
125ReturnedValue throwTypeError(const FunctionObject *b, const QV4::Value *, const QV4::Value *, int)
126{
127 return b->engine()->throwTypeError();
128}
129
130
131template <typename ReturnType>
132ReturnType convertJSValueToVariantType(const QJSValue &value)
133{
134 const QVariant variant = value.toVariant();
135 return variant.metaType() == QMetaType::fromType<QJSValue>()
136 ? ReturnType()
137 : variant.value<ReturnType>();
138}
139
144
145namespace {
146void createNewIteratorIfNonExisting(void **iterator) {
147 if (*iterator == nullptr)
148 *iterator = new JSArrayIterator;
149}
150}
151
153{
154 // set up some functions so that non-array QMetaSequence::Iterables do not crash
155 // but instead appear as an empty sequence
156
157 using namespace QtMetaContainerPrivate;
158 QMetaSequenceInterface iface;
159 iface.sizeFn = [](const void *) { return qsizetype(0); };
160 iface.valueAtIndexFn = [](const void *, qsizetype, void *) {};
161 iface.createIteratorFn = [](void *, QMetaSequenceInterface::Position) -> void * {
162 return nullptr;
163 };
164 iface.advanceIteratorFn = [](void *, qsizetype) {};
165 iface.compareIteratorFn = [](const void *, const void *) {
166 return true; /*all iterators are nullptr*/
167 };
168 iface.destroyIteratorFn = [](const void *) {};
169 iface.copyIteratorFn = [](void *, const void *) {};
170 iface.diffIteratorFn = [](const void *, const void *) { return qsizetype(0); };
171
172 iface.createConstIteratorFn = [](const void *, QMetaSequenceInterface::Position) -> void * {
173 return nullptr;
174 };
175 iface.advanceConstIteratorFn = [](void *, qsizetype) {};
176 iface.compareConstIteratorFn = [](const void *, const void *) {
177 return true; /*all iterators are nullptr*/
178 };
179 iface.destroyConstIteratorFn = [](const void *) {};
180 iface.copyConstIteratorFn = [](void *, const void *) {};
181 iface.diffConstIteratorFn = [](const void *, const void *) { return qsizetype(0); };
182 return iface;
183}
184
186{
187 using namespace QtMetaContainerPrivate;
188 QMetaSequenceInterface iface;
189 iface.valueMetaType = QtPrivate::qMetaTypeInterfaceForType<QVariant>();
190 iface.iteratorCapabilities = RandomAccessCapability | BiDirectionalCapability | ForwardCapability;
191 iface.addRemoveCapabilities = CanAddAtEnd;
192 iface.sizeFn = [](const void *p) -> qsizetype {
193 return static_cast<QJSValue const *>(p)->property(QString::fromLatin1("length")).toInt();
194 };
195
196 /* Lifetime management notes:
197 * valueAtIndexFn and valueAtIteratorFn return a pointer to a JSValue allocated via
198 * QMetaType::create Because we set QVariantConstructionFlags::ShouldDeleteVariantData,
199 * QMetaSequence::Iterable::at and QMetaSequence::Iterable::operator*() will free that memory
200 */
201
202 iface.valueAtIndexFn = [](const void *iterable, qsizetype index, void *dataPtr) -> void {
203 auto *data = static_cast<QVariant *>(dataPtr);
204 *data = static_cast<QJSValue const *>(iterable)->property(quint32(index)).toVariant();
205 };
206 iface.createIteratorFn = [](void *iterable, QMetaSequenceInterface::Position pos) {
207 void *iterator = nullptr;
208 createNewIteratorIfNonExisting(&iterator);
209 auto jsArrayIterator = static_cast<JSArrayIterator *>(iterator);
210 jsArrayIterator->index = 0;
211 jsArrayIterator->data = reinterpret_cast<QJSValue const*>(iterable);
212 if (pos == QMetaSequenceInterface::AtEnd) {
213 auto length = static_cast<QJSValue const *>(iterable)->property(
214 QString::fromLatin1("length")).toInt();
215 jsArrayIterator->index = quint32(length);
216 }
217 return iterator;
218 };
219 iface.createConstIteratorFn = [](const void *iterable, QMetaSequenceInterface::Position pos) {
220 void *iterator = nullptr;
221 createNewIteratorIfNonExisting(&iterator);
222 auto jsArrayIterator = static_cast<JSArrayIterator *>(iterator);
223 jsArrayIterator->index = 0;
224 jsArrayIterator->data = reinterpret_cast<QJSValue const*>(iterable);
225 if (pos == QMetaSequenceInterface::AtEnd) {
226 auto length = static_cast<QJSValue const *>(iterable)->property(
227 QString::fromLatin1("length")).toInt();
228 jsArrayIterator->index = quint32(length);
229 }
230 return iterator;
231 };
232 iface.advanceIteratorFn = [](void *iterator, qsizetype advanceBy) {
233 static_cast<JSArrayIterator *>(iterator)->index += quint32(advanceBy);
234 };
235 iface.advanceConstIteratorFn = [](void *iterator, qsizetype advanceBy) {
236 static_cast<JSArrayIterator *>(iterator)->index += quint32(advanceBy);
237 };
238 iface.valueAtIteratorFn = [](const void *iterator, void *dataPtr) -> void {
239 const auto *arrayIterator = static_cast<const JSArrayIterator *>(iterator);
240 const QJSValue *jsArray = arrayIterator->data;
241 auto *data = static_cast<QVariant *>(dataPtr);
242 *data = jsArray->property(arrayIterator->index).toVariant();
243 };
244 iface.valueAtConstIteratorFn = [](const void *iterator, void *dataPtr) -> void {
245 const auto *arrayIterator = static_cast<const JSArrayIterator *>(iterator);
246 const QJSValue *jsArray = arrayIterator->data;
247 auto *data = static_cast<QVariant *>(dataPtr);
248 *data = jsArray->property(arrayIterator->index).toVariant();
249 };
250 iface.destroyIteratorFn = [](const void *iterator) {
251 delete static_cast<const JSArrayIterator *>(iterator);
252 };
253 iface.destroyConstIteratorFn = [](const void *iterator) {
254 delete static_cast<const JSArrayIterator *>(iterator);
255 };
256 iface.compareIteratorFn = [](const void *p, const void *other) {
257 auto this_ = static_cast<const JSArrayIterator *>(p);
258 auto that_ = static_cast<const JSArrayIterator *>(other);
259 return this_->index == that_->index && this_->data == that_->data;
260 };
261 iface.compareConstIteratorFn = [](const void *p, const void *other) {
262 auto this_ = static_cast<const JSArrayIterator *>(p);
263 auto that_ = static_cast<const JSArrayIterator *>(other);
264 return this_->index == that_->index && this_->data == that_->data;
265 };
266 iface.copyIteratorFn = [](void *iterator, const void *otherIterator) {
267 auto *otherIter = (static_cast<JSArrayIterator const *>(otherIterator));
268 static_cast<JSArrayIterator *>(iterator)->index = otherIter->index;
269 static_cast<JSArrayIterator *>(iterator)->data = otherIter->data;
270 };
271 iface.copyConstIteratorFn = [](void *iterator, const void *otherIterator) {
272 auto *otherIter = (static_cast<JSArrayIterator const *>(otherIterator));
273 static_cast<JSArrayIterator *>(iterator)->index = otherIter->index;
274 static_cast<JSArrayIterator *>(iterator)->data = otherIter->data;
275 };
276 iface.diffIteratorFn = [](const void *iterator, const void *otherIterator) -> qsizetype {
277 const auto *self = static_cast<const JSArrayIterator *>(iterator);
278 const auto *other = static_cast<const JSArrayIterator *>(otherIterator);
279 return self->index - other->index;
280 };
281 iface.diffConstIteratorFn = [](const void *iterator, const void *otherIterator) -> qsizetype {
282 const auto *self = static_cast<const JSArrayIterator *>(iterator);
283 const auto *other = static_cast<const JSArrayIterator *>(otherIterator);
284 return self->index - other->index;
285 };
286 iface.addValueFn = [](void *iterable, const void *data, QMetaSequenceInterface::Position) {
287 auto *jsvalue = static_cast<QJSValue *>(iterable);
288 QV4::Scope scope(QJSValuePrivate::engine(jsvalue));
289 QV4::ScopedArrayObject a(scope, QJSValuePrivate::asManagedType<QV4::ArrayObject>(jsvalue));
290 QV4::ScopedValue v(scope, scope.engine->fromVariant(*static_cast<const QVariant *>(data)));
291 if (!a)
292 return;
293 int len = a->getLength();
294 a->setIndexed(len, v, QV4::Object::DoNotThrow);
295 };
296 return iface;
297}
298
299static QMetaSequence::Iterable jsvalueToSequence (const QJSValue& value) {
300 using namespace QtMetaTypePrivate;
301 using namespace QtMetaContainerPrivate;
302
303
304 if (!value.isArray()) {
305 static QMetaSequenceInterface emptySequence = emptySequenceInterface();
306 return QMetaSequence::Iterable(QMetaSequence(&emptySequence), nullptr);
307 }
308
309 static QMetaSequenceInterface sequence = sequenceInterface();
310 return QMetaSequence::Iterable(QMetaSequence(&sequence), &value);
311}
312
313void ExecutionEngine::initializeStaticMembers()
314{
315 bool ok = false;
316
317 const int envMaxJSStackSize = qEnvironmentVariableIntValue("QV4_JS_MAX_STACK_SIZE", &ok);
318 if (ok && envMaxJSStackSize > 0)
319 s_maxJSStackSize = envMaxJSStackSize;
320
321 const int envMaxGCStackSize = qEnvironmentVariableIntValue("QV4_GC_MAX_STACK_SIZE", &ok);
322 if (ok && envMaxGCStackSize > 0)
323 s_maxGCStackSize = envMaxGCStackSize;
324
325 const int envStackSizeSoftLimit = qEnvironmentVariableIntValue("QV4_STACK_SOFT_LIMIT", &ok);
326 if (ok && envStackSizeSoftLimit > -1)
327 s_stackSizeSoftLimit = envStackSizeSoftLimit;
328 else
329 s_stackSizeSoftLimit = -1;
330
331 if (qEnvironmentVariableIsSet("QV4_CRASH_ON_STACKOVERFLOW")) {
332 s_maxCallDepth = std::numeric_limits<qint32>::max();
333 } else {
334 ok = false;
335 s_maxCallDepth = qEnvironmentVariableIntValue("QV4_MAX_CALL_DEPTH", &ok);
336 if (!ok || s_maxCallDepth <= 0)
337 s_maxCallDepth = -1;
338 }
339
340 ok = false;
341 s_jitCallCountThreshold = qEnvironmentVariableIntValue("QV4_JIT_CALL_THRESHOLD", &ok);
342 if (!ok)
343 s_jitCallCountThreshold = 3;
344 if (qEnvironmentVariableIsSet("QV4_FORCE_INTERPRETER"))
345 s_jitCallCountThreshold = std::numeric_limits<int>::max();
346
347 qMetaTypeId<QJSValue>();
348 qMetaTypeId<QList<int> >();
349
350 if (!QMetaType::hasRegisteredConverterFunction<QJSValue, QVariantMap>())
351 QMetaType::registerConverter<QJSValue, QVariantMap>(convertJSValueToVariantType<QVariantMap>);
352 if (!QMetaType::hasRegisteredConverterFunction<QJSValue, QVariantHash>())
353 QMetaType::registerConverter<QJSValue, QVariantHash>(convertJSValueToVariantType<QVariantHash>);
354 if (!QMetaType::hasRegisteredConverterFunction<QJSValue, QVariantList>())
355 QMetaType::registerConverter<QJSValue, QVariantList>(convertJSValueToVariantType<QVariantList>);
356 if (!QMetaType::hasRegisteredConverterFunction<QJSValue, QStringList>())
357 QMetaType::registerConverter<QJSValue, QStringList>(convertJSValueToVariantType<QStringList>);
358 if (!QMetaType::hasRegisteredConverterFunction<QJSValue, QMetaSequence::Iterable>())
359 QMetaType::registerConverter<QJSValue, QMetaSequence::Iterable>(jsvalueToSequence);
360}
361
362/*!
363 \class QV4::ExecutionEngine
364 \inmodule QtQml
365 \internal
366*/
367ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
368 : executableAllocator(new QV4::ExecutableAllocator)
369 , regExpAllocator(new QV4::ExecutableAllocator)
370 , bumperPointerAllocator(new WTF::BumpPointerAllocator)
371 , jsStack(new WTF::PageAllocation)
372 , gcStack(new WTF::PageAllocation)
373 , publicEngine(jsEngine)
374 , m_engineId(engineSerial.fetchAndAddOrdered(2))
375#if QT_CONFIG(qml_jit)
376 , m_canAllocateExecutableMemory(OSAllocator::canAllocateExecutableMemory())
377#endif
378{
379 if (m_engineId == 1) {
380 initializeStaticMembers();
381 engineSerial.storeRelease(2); // make it even
382 } else if (Q_UNLIKELY(m_engineId & 1)) {
383 // This should be rare. You usually don't create lots of engines at the same time.
384 while (engineSerial.loadAcquire() & 1) {
385 QThread::yieldCurrentThread();
386 }
387 }
388
389 if (s_maxCallDepth < 0)
390 setCppStackProperties();
391 else
392 callDepth = 0;
393
394 // We allocate guard pages around our stacks.
395 const size_t guardPages = 2 * WTF::pageSize();
396
397 memoryManager = new QV4::MemoryManager(this);
398 // we don't want to run the gc while the initial setup is not done; not even in aggressive mode
399 GCCriticalSection gcCriticalSection(this);
400 // reserve space for the JS stack
401 // we allow it to grow to a bit more than m_maxJSStackSize, as we can overshoot due to ScopedValues
402 // allocated outside of JIT'ed methods.
403 *jsStack = WTF::PageAllocation::allocate(
404 s_maxJSStackSize + 256*1024 + guardPages, WTF::OSAllocator::JSVMStackPages,
405 /* writable */ true, /* executable */ false, /* includesGuardPages */ true);
406 jsStackBase = (Value *)jsStack->base();
407#ifdef V4_USE_VALGRIND
408 VALGRIND_MAKE_MEM_UNDEFINED(jsStackBase, s_maxJSStackSize + 256*1024);
409#endif
410
411 jsStackTop = jsStackBase;
412
413 *gcStack = WTF::PageAllocation::allocate(
414 s_maxGCStackSize + guardPages, WTF::OSAllocator::JSVMStackPages,
415 /* writable */ true, /* executable */ false, /* includesGuardPages */ true);
416
417 exceptionValue = jsAlloca(1);
418 *exceptionValue = Encode::undefined();
419 globalObject = static_cast<Object *>(jsAlloca(1));
420 jsObjects = jsAlloca(NJSObjects);
421 typedArrayPrototype = static_cast<Object *>(jsAlloca(NTypedArrayTypes));
422 typedArrayCtors = static_cast<FunctionObject *>(jsAlloca(NTypedArrayTypes));
423 jsStrings = jsAlloca(NJSStrings);
424 jsSymbols = jsAlloca(NJSSymbols);
425
426 // set up stack limits
427 jsStackLimit = jsStackBase + s_maxJSStackSize/sizeof(Value);
428
429 identifierTable = new IdentifierTable(this);
430
431 memset(classes, 0, sizeof(classes));
432 classes[Class_Empty] = memoryManager->allocIC<InternalClass>();
433 classes[Class_Empty]->init(this);
434
435 classes[Class_MemberData] = classes[Class_Empty]->changeVTable(QV4::MemberData::staticVTable());
436 classes[Class_SimpleArrayData] = classes[Class_Empty]->changeVTable(QV4::SimpleArrayData::staticVTable());
437 classes[Class_SparseArrayData] = classes[Class_Empty]->changeVTable(QV4::SparseArrayData::staticVTable());
438 classes[Class_ExecutionContext] = classes[Class_Empty]->changeVTable(QV4::ExecutionContext::staticVTable());
439 classes[Class_CallContext] = classes[Class_Empty]->changeVTable(QV4::CallContext::staticVTable());
440 classes[Class_QmlContext] = classes[Class_Empty]->changeVTable(QV4::QmlContext::staticVTable());
441
442 Scope scope(this);
443 Scoped<InternalClass> ic(scope);
444 ic = classes[Class_Empty]->changeVTable(QV4::Object::staticVTable());
445 jsObjects[ObjectProto] = memoryManager->allocObject<ObjectPrototype>(ic->d());
446 classes[Class_Object] = ic->changePrototype(objectPrototype()->d());
447 classes[Class_QmlContextWrapper] = classes[Class_Object]->changeVTable(QV4::QQmlContextWrapper::staticVTable());
448
449 ic = newInternalClass(QV4::StringObject::staticVTable(), objectPrototype());
450 jsObjects[StringProto] = memoryManager->allocObject<StringPrototype>(ic->d(), /*init =*/ false);
451 classes[Class_String] = classes[Class_Empty]->changeVTable(QV4::String::staticVTable())->changePrototype(stringPrototype()->d());
452 Q_ASSERT(stringPrototype()->d() && classes[Class_String]->prototype);
453
454 jsObjects[SymbolProto] = memoryManager->allocate<SymbolPrototype>();
455 classes[Class_Symbol] = classes[EngineBase::Class_Empty]->changeVTable(QV4::Symbol::staticVTable())->changePrototype(symbolPrototype()->d());
456
457 jsStrings[String_Empty] = newIdentifier(QString());
458 jsStrings[String_undefined] = newIdentifier(QStringLiteral("undefined"));
459 jsStrings[String_null] = newIdentifier(QStringLiteral("null"));
460 jsStrings[String_true] = newIdentifier(QStringLiteral("true"));
461 jsStrings[String_false] = newIdentifier(QStringLiteral("false"));
462 jsStrings[String_boolean] = newIdentifier(QStringLiteral("boolean"));
463 jsStrings[String_number] = newIdentifier(QStringLiteral("number"));
464 jsStrings[String_string] = newIdentifier(QStringLiteral("string"));
465 jsStrings[String_default] = newIdentifier(QStringLiteral("default"));
466 jsStrings[String_symbol] = newIdentifier(QStringLiteral("symbol"));
467 jsStrings[String_object] = newIdentifier(QStringLiteral("object"));
468 jsStrings[String_function] = newIdentifier(QStringLiteral("function"));
469 jsStrings[String_length] = newIdentifier(QStringLiteral("length"));
470 jsStrings[String_prototype] = newIdentifier(QStringLiteral("prototype"));
471 jsStrings[String_constructor] = newIdentifier(QStringLiteral("constructor"));
472 jsStrings[String_arguments] = newIdentifier(QStringLiteral("arguments"));
473 jsStrings[String_caller] = newIdentifier(QStringLiteral("caller"));
474 jsStrings[String_callee] = newIdentifier(QStringLiteral("callee"));
475 jsStrings[String_this] = newIdentifier(QStringLiteral("this"));
476 jsStrings[String___proto__] = newIdentifier(QStringLiteral("__proto__"));
477 jsStrings[String_enumerable] = newIdentifier(QStringLiteral("enumerable"));
478 jsStrings[String_configurable] = newIdentifier(QStringLiteral("configurable"));
479 jsStrings[String_writable] = newIdentifier(QStringLiteral("writable"));
480 jsStrings[String_value] = newIdentifier(QStringLiteral("value"));
481 jsStrings[String_get] = newIdentifier(QStringLiteral("get"));
482 jsStrings[String_set] = newIdentifier(QStringLiteral("set"));
483 jsStrings[String_eval] = newIdentifier(QStringLiteral("eval"));
484 jsStrings[String_uintMax] = newIdentifier(QStringLiteral("4294967295"));
485 jsStrings[String_name] = newIdentifier(QStringLiteral("name"));
486 jsStrings[String_index] = newIdentifier(QStringLiteral("index"));
487 jsStrings[String_input] = newIdentifier(QStringLiteral("input"));
488 jsStrings[String_toString] = newIdentifier(QStringLiteral("toString"));
489 jsStrings[String_toLocaleString] = newIdentifier(QStringLiteral("toLocaleString"));
490 jsStrings[String_destroy] = newIdentifier(QStringLiteral("destroy"));
491 jsStrings[String_valueOf] = newIdentifier(QStringLiteral("valueOf"));
492 jsStrings[String_byteLength] = newIdentifier(QStringLiteral("byteLength"));
493 jsStrings[String_byteOffset] = newIdentifier(QStringLiteral("byteOffset"));
494 jsStrings[String_buffer] = newIdentifier(QStringLiteral("buffer"));
495 jsStrings[String_lastIndex] = newIdentifier(QStringLiteral("lastIndex"));
496 jsStrings[String_next] = newIdentifier(QStringLiteral("next"));
497 jsStrings[String_done] = newIdentifier(QStringLiteral("done"));
498 jsStrings[String_return] = newIdentifier(QStringLiteral("return"));
499 jsStrings[String_throw] = newIdentifier(QStringLiteral("throw"));
500 jsStrings[String_global] = newIdentifier(QStringLiteral("global"));
501 jsStrings[String_ignoreCase] = newIdentifier(QStringLiteral("ignoreCase"));
502 jsStrings[String_multiline] = newIdentifier(QStringLiteral("multiline"));
503 jsStrings[String_unicode] = newIdentifier(QStringLiteral("unicode"));
504 jsStrings[String_sticky] = newIdentifier(QStringLiteral("sticky"));
505 jsStrings[String_source] = newIdentifier(QStringLiteral("source"));
506 jsStrings[String_flags] = newIdentifier(QStringLiteral("flags"));
507
508 jsSymbols[Symbol_hasInstance] = Symbol::create(this, QStringLiteral("@Symbol.hasInstance"));
509 jsSymbols[Symbol_isConcatSpreadable] = Symbol::create(this, QStringLiteral("@Symbol.isConcatSpreadable"));
510 jsSymbols[Symbol_iterator] = Symbol::create(this, QStringLiteral("@Symbol.iterator"));
511 jsSymbols[Symbol_match] = Symbol::create(this, QStringLiteral("@Symbol.match"));
512 jsSymbols[Symbol_replace] = Symbol::create(this, QStringLiteral("@Symbol.replace"));
513 jsSymbols[Symbol_search] = Symbol::create(this, QStringLiteral("@Symbol.search"));
514 jsSymbols[Symbol_species] = Symbol::create(this, QStringLiteral("@Symbol.species"));
515 jsSymbols[Symbol_split] = Symbol::create(this, QStringLiteral("@Symbol.split"));
516 jsSymbols[Symbol_toPrimitive] = Symbol::create(this, QStringLiteral("@Symbol.toPrimitive"));
517 jsSymbols[Symbol_toStringTag] = Symbol::create(this, QStringLiteral("@Symbol.toStringTag"));
518 jsSymbols[Symbol_unscopables] = Symbol::create(this, QStringLiteral("@Symbol.unscopables"));
519 jsSymbols[Symbol_revokableProxy] = Symbol::create(this, QStringLiteral("@Proxy.revokableProxy"));
520
521 ic = newInternalClass(ArrayPrototype::staticVTable(), objectPrototype());
522 Q_ASSERT(ic->d()->prototype);
523 ic = ic->addMember(id_length()->propertyKey(), Attr_NotConfigurable|Attr_NotEnumerable);
524 Q_ASSERT(ic->d()->prototype);
525 jsObjects[ArrayProto] = memoryManager->allocObject<ArrayPrototype>(ic->d());
526 classes[Class_ArrayObject] = ic->changePrototype(arrayPrototype()->d());
527 jsObjects[PropertyListProto] = memoryManager->allocate<PropertyListPrototype>();
528
529 Scoped<InternalClass> argsClass(scope);
530 argsClass = newInternalClass(ArgumentsObject::staticVTable(), objectPrototype());
531 argsClass = argsClass->addMember(id_length()->propertyKey(), Attr_NotEnumerable);
532 argsClass = argsClass->addMember(symbol_iterator()->propertyKey(), Attr_Data|Attr_NotEnumerable);
533 classes[Class_ArgumentsObject] = argsClass->addMember(id_callee()->propertyKey(), Attr_Data|Attr_NotEnumerable);
534 argsClass = newInternalClass(StrictArgumentsObject::staticVTable(), objectPrototype());
535 argsClass = argsClass->addMember(id_length()->propertyKey(), Attr_NotEnumerable);
536 argsClass = argsClass->addMember(symbol_iterator()->propertyKey(), Attr_Data|Attr_NotEnumerable);
537 classes[Class_StrictArgumentsObject] = argsClass->addMember(id_callee()->propertyKey(), Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
538
539 *static_cast<Value *>(globalObject) = newObject();
540 Q_ASSERT(globalObject->d()->vtable());
541 initRootContext();
542
543 ic = newInternalClass(QV4::StringObject::staticVTable(), objectPrototype());
544 ic = ic->addMember(id_length()->propertyKey(), Attr_ReadOnly);
545 classes[Class_StringObject] = ic->changePrototype(stringPrototype()->d());
546 Q_ASSERT(classes[Class_StringObject]->verifyIndex(id_length()->propertyKey(), Heap::StringObject::LengthPropertyIndex));
547
548 classes[Class_SymbolObject] = newInternalClass(QV4::SymbolObject::staticVTable(), symbolPrototype());
549
550 jsObjects[NumberProto] = memoryManager->allocate<NumberPrototype>();
551 jsObjects[BooleanProto] = memoryManager->allocate<BooleanPrototype>();
552 jsObjects[DateProto] = memoryManager->allocate<DatePrototype>();
553
554#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
555 InternalClassEntry *index = nullptr;
556#else
557 InternalClassEntry _index;
558 auto *index = &_index;
559#endif
560 ic = newInternalClass(QV4::FunctionPrototype::staticVTable(), objectPrototype());
561 auto addProtoHasInstance = [&] {
562 // Add an invalid prototype slot, so that all function objects have the same layout
563 // This helps speed up instanceof operations and other things where we need to query
564 // prototype property (as we always know it's location)
565 ic = ic->addMember(id_prototype()->propertyKey(), Attr_Invalid, index);
566 Q_ASSERT(index->index == Heap::FunctionObject::Index_Prototype);
567 // add an invalid @hasInstance slot, so that we can quickly track whether the
568 // hasInstance method has been reimplemented. This is required for a fast
569 // instanceof implementation
570 ic = ic->addMember(symbol_hasInstance()->propertyKey(), Attr_Invalid, index);
571 Q_ASSERT(index->index == Heap::FunctionObject::Index_HasInstance);
572 };
573 addProtoHasInstance();
574 jsObjects[FunctionProto] = memoryManager->allocObject<FunctionPrototype>(ic->d());
575 ic = newInternalClass(FunctionObject::staticVTable(), functionPrototype());
576 addProtoHasInstance();
577 classes[Class_FunctionObject] = ic->d();
578 ic = ic->addMember(id_name()->propertyKey(), Attr_ReadOnly, index);
579 Q_ASSERT(index->index == Heap::ArrowFunction::Index_Name);
580 ic = ic->addMember(id_length()->propertyKey(), Attr_ReadOnly_ButConfigurable, index);
581 Q_ASSERT(index->index == Heap::ArrowFunction::Index_Length);
582 classes[Class_ArrowFunction] = ic->changeVTable(ArrowFunction::staticVTable());
583 ic = ic->changeVTable(MemberFunction::staticVTable());
584 classes[Class_MemberFunction] = ic->d();
585 ic = ic->changeVTable(GeneratorFunction::staticVTable());
586 classes[Class_GeneratorFunction] = ic->d();
587 ic = ic->changeVTable(MemberGeneratorFunction::staticVTable());
588 classes[Class_MemberGeneratorFunction] = ic->d();
589
590 ic = ic->changeMember(id_prototype()->propertyKey(), Attr_NotConfigurable|Attr_NotEnumerable);
591 ic = ic->changeVTable(ScriptFunction::staticVTable());
592 classes[Class_ScriptFunction] = ic->d();
593 ic = ic->changeVTable(ConstructorFunction::staticVTable());
594 classes[Class_ConstructorFunction] = ic->d();
595
596 classes[Class_ObjectProto] = classes[Class_Object]->addMember(id_constructor()->propertyKey(), Attr_NotEnumerable, index);
597 Q_ASSERT(index->index == Heap::FunctionObject::Index_ProtoConstructor);
598
599 jsObjects[GeneratorProto] = memoryManager->allocObject<GeneratorPrototype>(classes[Class_Object]);
600 classes[Class_GeneratorObject] = newInternalClass(QV4::GeneratorObject::staticVTable(), generatorPrototype());
601
602 ScopedString str(scope);
603 classes[Class_RegExp] = classes[Class_Empty]->changeVTable(QV4::RegExp::staticVTable());
604 ic = newInternalClass(QV4::RegExpObject::staticVTable(), objectPrototype());
605 ic = ic->addMember(id_lastIndex()->propertyKey(), Attr_NotEnumerable|Attr_NotConfigurable, index);
606 Q_ASSERT(index->index == RegExpObject::Index_LastIndex);
607 jsObjects[RegExpProto] = memoryManager->allocObject<RegExpPrototype>(classes[Class_Object]);
608 classes[Class_RegExpObject] = ic->changePrototype(regExpPrototype()->d());
609
610 ic = classes[Class_ArrayObject]->addMember(id_index()->propertyKey(), Attr_Data, index);
611 Q_ASSERT(index->index == RegExpObject::Index_ArrayIndex);
612 classes[Class_RegExpExecArray] = ic->addMember(id_input()->propertyKey(), Attr_Data, index);
613 Q_ASSERT(index->index == RegExpObject::Index_ArrayInput);
614
615 ic = newInternalClass(ErrorObject::staticVTable(), nullptr);
616 ic = ic->addMember((str = newIdentifier(QStringLiteral("stack")))->propertyKey(), Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable, index);
617 Q_ASSERT(index->index == ErrorObject::Index_Stack);
618 Q_ASSERT(index->setterIndex == ErrorObject::Index_StackSetter);
619 ic = ic->addMember((str = newIdentifier(QStringLiteral("fileName")))->propertyKey(), Attr_Data|Attr_NotEnumerable, index);
620 Q_ASSERT(index->index == ErrorObject::Index_FileName);
621 ic = ic->addMember((str = newIdentifier(QStringLiteral("lineNumber")))->propertyKey(), Attr_Data|Attr_NotEnumerable, index);
622 classes[Class_ErrorObject] = ic->d();
623 Q_ASSERT(index->index == ErrorObject::Index_LineNumber);
624 classes[Class_ErrorObjectWithMessage] = ic->addMember((str = newIdentifier(QStringLiteral("message")))->propertyKey(), Attr_Data|Attr_NotEnumerable, index);
625 Q_ASSERT(index->index == ErrorObject::Index_Message);
626 ic = newInternalClass(Object::staticVTable(), objectPrototype());
627 ic = ic->addMember(id_constructor()->propertyKey(), Attr_Data|Attr_NotEnumerable, index);
628 Q_ASSERT(index->index == ErrorPrototype::Index_Constructor);
629 ic = ic->addMember((str = newIdentifier(QStringLiteral("message")))->propertyKey(), Attr_Data|Attr_NotEnumerable, index);
630 Q_ASSERT(index->index == ErrorPrototype::Index_Message);
631 classes[Class_ErrorProto] = ic->addMember(id_name()->propertyKey(), Attr_Data|Attr_NotEnumerable, index);
632 Q_ASSERT(index->index == ErrorPrototype::Index_Name);
633
634 classes[Class_ProxyObject] = classes[Class_Empty]->changeVTable(ProxyObject::staticVTable());
635 classes[Class_ProxyFunctionObject] = classes[Class_Empty]->changeVTable(ProxyFunctionObject::staticVTable());
636
637 jsObjects[GetStack_Function] = FunctionObject::createBuiltinFunction(this, str = newIdentifier(QStringLiteral("stack")), ErrorObject::method_get_stack, 0);
638
639 jsObjects[ErrorProto] = memoryManager->allocObject<ErrorPrototype>(classes[Class_ErrorProto]);
640 ic = classes[Class_ErrorProto]->changePrototype(errorPrototype()->d());
641 jsObjects[EvalErrorProto] = memoryManager->allocObject<EvalErrorPrototype>(ic->d());
642 jsObjects[RangeErrorProto] = memoryManager->allocObject<RangeErrorPrototype>(ic->d());
643 jsObjects[ReferenceErrorProto] = memoryManager->allocObject<ReferenceErrorPrototype>(ic->d());
644 jsObjects[SyntaxErrorProto] = memoryManager->allocObject<SyntaxErrorPrototype>(ic->d());
645 jsObjects[TypeErrorProto] = memoryManager->allocObject<TypeErrorPrototype>(ic->d());
646 jsObjects[URIErrorProto] = memoryManager->allocObject<URIErrorPrototype>(ic->d());
647
648 jsObjects[VariantProto] = memoryManager->allocate<VariantPrototype>();
649 Q_ASSERT(variantPrototype()->getPrototypeOf() == objectPrototype()->d());
650
651 jsObjects[VariantAssociationProto] = memoryManager->allocate<VariantAssociationPrototype>();
652 Q_ASSERT(variantAssociationPrototype()->getPrototypeOf() == objectPrototype()->d());
653
654 ic = newInternalClass(SequencePrototype::staticVTable(), SequencePrototype::defaultPrototype(this));
655 jsObjects[SequenceProto] = ScopedValue(scope, memoryManager->allocObject<SequencePrototype>(ic->d()));
656
657 jsObjects[Object_Ctor] = memoryManager->allocate<ObjectCtor>(this);
658 jsObjects[String_Ctor] = memoryManager->allocate<StringCtor>(this);
659 jsObjects[Symbol_Ctor] = memoryManager->allocate<SymbolCtor>(this);
660 jsObjects[Number_Ctor] = memoryManager->allocate<NumberCtor>(this);
661 jsObjects[Boolean_Ctor] = memoryManager->allocate<BooleanCtor>(this);
662 jsObjects[Array_Ctor] = memoryManager->allocate<ArrayCtor>(this);
663 jsObjects[Function_Ctor] = memoryManager->allocate<FunctionCtor>(this);
664 jsObjects[GeneratorFunction_Ctor] = memoryManager->allocate<GeneratorFunctionCtor>(this);
665 jsObjects[Date_Ctor] = memoryManager->allocate<DateCtor>(this);
666 jsObjects[RegExp_Ctor] = memoryManager->allocate<RegExpCtor>(this);
667 jsObjects[Error_Ctor] = memoryManager->allocate<ErrorCtor>(this);
668 jsObjects[EvalError_Ctor] = memoryManager->allocate<EvalErrorCtor>(this);
669 jsObjects[RangeError_Ctor] = memoryManager->allocate<RangeErrorCtor>(this);
670 jsObjects[ReferenceError_Ctor] = memoryManager->allocate<ReferenceErrorCtor>(this);
671 jsObjects[SyntaxError_Ctor] = memoryManager->allocate<SyntaxErrorCtor>(this);
672 jsObjects[TypeError_Ctor] = memoryManager->allocate<TypeErrorCtor>(this);
673 jsObjects[URIError_Ctor] = memoryManager->allocate<URIErrorCtor>(this);
674 jsObjects[IteratorProto] = memoryManager->allocate<IteratorPrototype>();
675
676 ic = newInternalClass(ForInIteratorPrototype::staticVTable(), iteratorPrototype());
677 jsObjects[ForInIteratorProto] = memoryManager->allocObject<ForInIteratorPrototype>(ic);
678 ic = newInternalClass(SetIteratorPrototype::staticVTable(), iteratorPrototype());
679 jsObjects[MapIteratorProto] = memoryManager->allocObject<MapIteratorPrototype>(ic);
680 ic = newInternalClass(SetIteratorPrototype::staticVTable(), iteratorPrototype());
681 jsObjects[SetIteratorProto] = memoryManager->allocObject<SetIteratorPrototype>(ic);
682 ic = newInternalClass(ArrayIteratorPrototype::staticVTable(), iteratorPrototype());
683 jsObjects[ArrayIteratorProto] = memoryManager->allocObject<ArrayIteratorPrototype>(ic);
684 ic = newInternalClass(StringIteratorPrototype::staticVTable(), iteratorPrototype());
685 jsObjects[StringIteratorProto] = memoryManager->allocObject<StringIteratorPrototype>(ic);
686
687 //
688 // url
689 //
690
691 jsObjects[Url_Ctor] = memoryManager->allocate<UrlCtor>(this);
692 jsObjects[UrlProto] = memoryManager->allocate<UrlPrototype>();
693 jsObjects[UrlSearchParams_Ctor] = memoryManager->allocate<UrlSearchParamsCtor>(this);
694 jsObjects[UrlSearchParamsProto] = memoryManager->allocate<UrlSearchParamsPrototype>();
695
696 str = newString(QStringLiteral("get [Symbol.species]"));
697 jsObjects[GetSymbolSpecies] = FunctionObject::createBuiltinFunction(this, str, ArrayPrototype::method_get_species, 0);
698
699 static_cast<ObjectPrototype *>(objectPrototype())->init(this, objectCtor());
700 static_cast<StringPrototype *>(stringPrototype())->init(this, stringCtor());
701 static_cast<SymbolPrototype *>(symbolPrototype())->init(this, symbolCtor());
702 static_cast<NumberPrototype *>(numberPrototype())->init(this, numberCtor());
703 static_cast<BooleanPrototype *>(booleanPrototype())->init(this, booleanCtor());
704 static_cast<ArrayPrototype *>(arrayPrototype())->init(this, arrayCtor());
705 static_cast<PropertyListPrototype *>(propertyListPrototype())->init();
706 static_cast<DatePrototype *>(datePrototype())->init(this, dateCtor());
707 static_cast<FunctionPrototype *>(functionPrototype())->init(this, functionCtor());
708 static_cast<GeneratorPrototype *>(generatorPrototype())->init(this, generatorFunctionCtor());
709 static_cast<RegExpPrototype *>(regExpPrototype())->init(this, regExpCtor());
710 static_cast<ErrorPrototype *>(errorPrototype())->init(this, errorCtor());
711 static_cast<EvalErrorPrototype *>(evalErrorPrototype())->init(this, evalErrorCtor());
712 static_cast<RangeErrorPrototype *>(rangeErrorPrototype())->init(this, rangeErrorCtor());
713 static_cast<ReferenceErrorPrototype *>(referenceErrorPrototype())->init(this, referenceErrorCtor());
714 static_cast<SyntaxErrorPrototype *>(syntaxErrorPrototype())->init(this, syntaxErrorCtor());
715 static_cast<TypeErrorPrototype *>(typeErrorPrototype())->init(this, typeErrorCtor());
716 static_cast<URIErrorPrototype *>(uRIErrorPrototype())->init(this, uRIErrorCtor());
717 static_cast<UrlPrototype *>(urlPrototype())->init(this, urlCtor());
718 static_cast<UrlSearchParamsPrototype *>(urlSearchParamsPrototype())->init(this, urlSearchParamsCtor());
719
720 static_cast<IteratorPrototype *>(iteratorPrototype())->init(this);
721 static_cast<ForInIteratorPrototype *>(forInIteratorPrototype())->init(this);
722 static_cast<MapIteratorPrototype *>(mapIteratorPrototype())->init(this);
723 static_cast<SetIteratorPrototype *>(setIteratorPrototype())->init(this);
724 static_cast<ArrayIteratorPrototype *>(arrayIteratorPrototype())->init(this);
725 static_cast<StringIteratorPrototype *>(stringIteratorPrototype())->init(this);
726
727 static_cast<VariantPrototype *>(variantPrototype())->init();
728
729 sequencePrototype()->cast<SequencePrototype>()->init();
730
731 jsObjects[WeakMap_Ctor] = memoryManager->allocate<WeakMapCtor>(this);
732 jsObjects[WeakMapProto] = memoryManager->allocate<WeakMapPrototype>();
733 static_cast<WeakMapPrototype *>(weakMapPrototype())->init(this, weakMapCtor());
734
735 jsObjects[Map_Ctor] = memoryManager->allocate<MapCtor>(this);
736 jsObjects[MapProto] = memoryManager->allocate<MapPrototype>();
737 static_cast<MapPrototype *>(mapPrototype())->init(this, mapCtor());
738
739 jsObjects[WeakSet_Ctor] = memoryManager->allocate<WeakSetCtor>(this);
740 jsObjects[WeakSetProto] = memoryManager->allocate<WeakSetPrototype>();
741 static_cast<WeakSetPrototype *>(weakSetPrototype())->init(this, weakSetCtor());
742
743 jsObjects[Set_Ctor] = memoryManager->allocate<SetCtor>(this);
744 jsObjects[SetProto] = memoryManager->allocate<SetPrototype>();
745 static_cast<SetPrototype *>(setPrototype())->init(this, setCtor());
746
747 //
748 // promises
749 //
750
751 jsObjects[Promise_Ctor] = memoryManager->allocate<PromiseCtor>(this);
752 jsObjects[PromiseProto] = memoryManager->allocate<PromisePrototype>();
753 static_cast<PromisePrototype *>(promisePrototype())->init(this, promiseCtor());
754
755 // typed arrays
756
757 jsObjects[SharedArrayBuffer_Ctor] = memoryManager->allocate<SharedArrayBufferCtor>(this);
758 jsObjects[SharedArrayBufferProto] = memoryManager->allocate<SharedArrayBufferPrototype>();
759 static_cast<SharedArrayBufferPrototype *>(sharedArrayBufferPrototype())->init(this, sharedArrayBufferCtor());
760
761 jsObjects[ArrayBuffer_Ctor] = memoryManager->allocate<ArrayBufferCtor>(this);
762 jsObjects[ArrayBufferProto] = memoryManager->allocate<ArrayBufferPrototype>();
763 static_cast<ArrayBufferPrototype *>(arrayBufferPrototype())->init(this, arrayBufferCtor());
764
765 jsObjects[DataView_Ctor] = memoryManager->allocate<DataViewCtor>(this);
766 jsObjects[DataViewProto] = memoryManager->allocate<DataViewPrototype>();
767 static_cast<DataViewPrototype *>(dataViewPrototype())->init(this, dataViewCtor());
768 jsObjects[ValueTypeProto] = (Heap::Base *) nullptr;
769 jsObjects[SignalHandlerProto] = (Heap::Base *) nullptr;
770 jsObjects[TypeWrapperProto] = (Heap::Base *) nullptr;
771
772 jsObjects[IntrinsicTypedArray_Ctor] = memoryManager->allocate<IntrinsicTypedArrayCtor>(this);
773 jsObjects[IntrinsicTypedArrayProto] = memoryManager->allocate<IntrinsicTypedArrayPrototype>();
774 static_cast<IntrinsicTypedArrayPrototype *>(intrinsicTypedArrayPrototype())
775 ->init(this, static_cast<IntrinsicTypedArrayCtor *>(intrinsicTypedArrayCtor()));
776
777 for (int i = 0; i < NTypedArrayTypes; ++i) {
778 static_cast<Value &>(typedArrayCtors[i]) = memoryManager->allocate<TypedArrayCtor>(this, Heap::TypedArray::Type(i));
779 static_cast<Value &>(typedArrayPrototype[i]) = memoryManager->allocate<TypedArrayPrototype>(Heap::TypedArray::Type(i));
780 typedArrayPrototype[i].as<TypedArrayPrototype>()->init(this, static_cast<TypedArrayCtor *>(typedArrayCtors[i].as<Object>()));
781 }
782
783 //
784 // set up the global object
785 //
786 rootContext()->d()->activation.set(scope.engine, globalObject->d());
787 Q_ASSERT(globalObject->d()->vtable());
788
789 globalObject->defineDefaultProperty(QStringLiteral("Object"), *objectCtor());
790 globalObject->defineDefaultProperty(QStringLiteral("String"), *stringCtor());
791 globalObject->defineDefaultProperty(QStringLiteral("Symbol"), *symbolCtor());
792 FunctionObject *numberObject = numberCtor();
793 globalObject->defineDefaultProperty(QStringLiteral("Number"), *numberObject);
794 globalObject->defineDefaultProperty(QStringLiteral("Boolean"), *booleanCtor());
795 globalObject->defineDefaultProperty(QStringLiteral("Array"), *arrayCtor());
796 globalObject->defineDefaultProperty(QStringLiteral("Function"), *functionCtor());
797 globalObject->defineDefaultProperty(QStringLiteral("Date"), *dateCtor());
798 globalObject->defineDefaultProperty(QStringLiteral("RegExp"), *regExpCtor());
799 globalObject->defineDefaultProperty(QStringLiteral("Error"), *errorCtor());
800 globalObject->defineDefaultProperty(QStringLiteral("EvalError"), *evalErrorCtor());
801 globalObject->defineDefaultProperty(QStringLiteral("RangeError"), *rangeErrorCtor());
802 globalObject->defineDefaultProperty(QStringLiteral("ReferenceError"), *referenceErrorCtor());
803 globalObject->defineDefaultProperty(QStringLiteral("SyntaxError"), *syntaxErrorCtor());
804 globalObject->defineDefaultProperty(QStringLiteral("TypeError"), *typeErrorCtor());
805 globalObject->defineDefaultProperty(QStringLiteral("URIError"), *uRIErrorCtor());
806 globalObject->defineDefaultProperty(QStringLiteral("Promise"), *promiseCtor());
807 globalObject->defineDefaultProperty(QStringLiteral("URL"), *urlCtor());
808 globalObject->defineDefaultProperty(QStringLiteral("URLSearchParams"), *urlSearchParamsCtor());
809
810 globalObject->defineDefaultProperty(QStringLiteral("SharedArrayBuffer"), *sharedArrayBufferCtor());
811 globalObject->defineDefaultProperty(QStringLiteral("ArrayBuffer"), *arrayBufferCtor());
812 globalObject->defineDefaultProperty(QStringLiteral("DataView"), *dataViewCtor());
813 globalObject->defineDefaultProperty(QStringLiteral("WeakSet"), *weakSetCtor());
814 globalObject->defineDefaultProperty(QStringLiteral("Set"), *setCtor());
815 globalObject->defineDefaultProperty(QStringLiteral("WeakMap"), *weakMapCtor());
816 globalObject->defineDefaultProperty(QStringLiteral("Map"), *mapCtor());
817
818 for (int i = 0; i < NTypedArrayTypes; ++i)
819 globalObject->defineDefaultProperty((str = typedArrayCtors[i].as<FunctionObject>()->name()), typedArrayCtors[i]);
820 ScopedObject o(scope);
821 globalObject->defineDefaultProperty(QStringLiteral("Atomics"), (o = memoryManager->allocate<Atomics>()));
822 globalObject->defineDefaultProperty(QStringLiteral("Math"), (o = memoryManager->allocate<MathObject>()));
823 globalObject->defineDefaultProperty(QStringLiteral("JSON"), (o = memoryManager->allocate<JsonObject>()));
824 globalObject->defineDefaultProperty(QStringLiteral("Reflect"), (o = memoryManager->allocate<Reflect>()));
825 globalObject->defineDefaultProperty(QStringLiteral("Proxy"), (o = memoryManager->allocate<Proxy>(this)));
826
827 globalObject->defineReadonlyProperty(QStringLiteral("undefined"), Value::undefinedValue());
828 globalObject->defineReadonlyProperty(QStringLiteral("NaN"), Value::fromDouble(std::numeric_limits<double>::quiet_NaN()));
829 globalObject->defineReadonlyProperty(QStringLiteral("Infinity"), Value::fromDouble(Q_INFINITY));
830
831
832 jsObjects[Eval_Function] = memoryManager->allocate<EvalFunction>(this);
833 globalObject->defineDefaultProperty(QStringLiteral("eval"), *evalFunction());
834
835 // ES6: 20.1.2.12 & 20.1.2.13:
836 // parseInt and parseFloat must be the same FunctionObject on the global &
837 // Number object.
838 {
839 QString piString(QStringLiteral("parseInt"));
840 QString pfString(QStringLiteral("parseFloat"));
841 Scope scope(this);
842 ScopedString pi(scope, newIdentifier(piString));
843 ScopedString pf(scope, newIdentifier(pfString));
844 ScopedFunctionObject parseIntFn(scope, FunctionObject::createBuiltinFunction(this, pi, GlobalFunctions::method_parseInt, 2));
845 ScopedFunctionObject parseFloatFn(scope, FunctionObject::createBuiltinFunction(this, pf, GlobalFunctions::method_parseFloat, 1));
846 globalObject->defineDefaultProperty(piString, parseIntFn);
847 globalObject->defineDefaultProperty(pfString, parseFloatFn);
848 numberObject->defineDefaultProperty(piString, parseIntFn);
849 numberObject->defineDefaultProperty(pfString, parseFloatFn);
850 }
851
852 globalObject->defineDefaultProperty(QStringLiteral("isNaN"), GlobalFunctions::method_isNaN, 1);
853 globalObject->defineDefaultProperty(QStringLiteral("isFinite"), GlobalFunctions::method_isFinite, 1);
854 globalObject->defineDefaultProperty(QStringLiteral("decodeURI"), GlobalFunctions::method_decodeURI, 1);
855 globalObject->defineDefaultProperty(QStringLiteral("decodeURIComponent"), GlobalFunctions::method_decodeURIComponent, 1);
856 globalObject->defineDefaultProperty(QStringLiteral("encodeURI"), GlobalFunctions::method_encodeURI, 1);
857 globalObject->defineDefaultProperty(QStringLiteral("encodeURIComponent"), GlobalFunctions::method_encodeURIComponent, 1);
858 globalObject->defineDefaultProperty(QStringLiteral("escape"), GlobalFunctions::method_escape, 1);
859 globalObject->defineDefaultProperty(QStringLiteral("unescape"), GlobalFunctions::method_unescape, 1);
860
861 ScopedFunctionObject t(
862 scope,
863 memoryManager->allocate<DynamicFunctionObject>(this, nullptr, ::throwTypeError));
864 t->defineReadonlyProperty(id_length(), Value::fromInt32(0));
865 t->setInternalClass(t->internalClass()->cryopreserved());
866 jsObjects[ThrowerObject] = t;
867
868 ScopedProperty pd(scope);
869 pd->value = thrower();
870 pd->set = thrower();
871 functionPrototype()->insertMember(id_caller(), pd, Attr_Accessor|Attr_ReadOnly_ButConfigurable);
872 functionPrototype()->insertMember(id_arguments(), pd, Attr_Accessor|Attr_ReadOnly_ButConfigurable);
873
874 QV4::QObjectWrapper::initializeBindings(this);
875
876 m_delayedCallQueue.init(this);
877 isInitialized = true;
878}
879
880ExecutionEngine::~ExecutionEngine()
881{
882 // The public engine destructors set this before they get here, but an
883 // ExecutionEngine can also be used on its own. Everything below - the final sweep
884 // in ~MemoryManager in particular - runs destruction handlers against an engine
885 // that is already losing its parts, and those must not start a gc.
886 inShutdown = true;
887
888#if QT_CONFIG(qml_network)
889 delete networkAccessManager;
890#endif
891 m_typeLoader.reset();
892 qDeleteAll(m_extensionData);
893 delete m_multiplyWrappedQObjects;
894 m_multiplyWrappedQObjects = nullptr;
895 delete identifierTable;
896 delete memoryManager;
897
898 for (const auto &cu : std::as_const(m_compilationUnits)) {
899 Q_ASSERT(cu->engine == this);
900 cu->clear();
901 cu->engine = nullptr;
902 }
903 m_compilationUnits.clear();
904
905 delete bumperPointerAllocator;
906 delete regExpCache;
907 delete regExpAllocator;
908 delete executableAllocator;
909 jsStack->deallocate();
910 delete jsStack;
911 gcStack->deallocate();
912 delete gcStack;
913
914#if QT_CONFIG(qml_xml_http_request)
915 qt_rem_qmlxmlhttprequest(this, m_xmlHttpRequestData);
916 m_xmlHttpRequestData = nullptr;
917#endif
918
919 QQmlMetaType::freeUnusedTypesAndCaches();
920}
921
922#if QT_CONFIG(qml_network)
923QNetworkAccessManager *ExecutionEngine::getNetworkAccessManager()
924{
925 if (!networkAccessManager)
926 networkAccessManager = typeLoader()->createNetworkAccessManager(nullptr);
927 return networkAccessManager;
928}
929#endif
930
931#if QT_CONFIG(qml_debug)
932void ExecutionEngine::setDebugger(Debugging::Debugger *debugger)
933{
934 Q_ASSERT(!m_debugger);
935 m_debugger.reset(debugger);
936}
937
938void ExecutionEngine::setProfiler(Profiling::Profiler *profiler)
939{
940 Q_ASSERT(!m_profiler);
941 m_profiler.reset(profiler);
942}
943
944void ExecutionEngine::setPreviewing(bool enabled)
945{
946 hasPreview.storeRelease(enabled);
947}
948
949bool ExecutionEngine::isPreviewing()
950{
951 return hasPreview.loadAcquire();
952}
953
954#endif // QT_CONFIG(qml_debug)
955
956void ExecutionEngine::initRootContext()
957{
958 Scope scope(this);
959 Scoped<ExecutionContext> r(scope, memoryManager->allocManaged<ExecutionContext>());
960 r->d()->init(Heap::ExecutionContext::Type_GlobalContext);
961 r->d()->activation.set(this, globalObject->d());
962 jsObjects[RootContext] = r;
963 jsObjects[ScriptContext] = r;
964 jsObjects[IntegerNull] = Encode((int)0);
965}
966
967Heap::InternalClass *ExecutionEngine::newClass(Heap::InternalClass *other)
968{
969 Heap::InternalClass *ic = memoryManager->allocIC<InternalClass>();
970 ic->init(other);
971 return ic;
972}
973
974Heap::InternalClass *ExecutionEngine::newInternalClass(const VTable *vtable, Object *prototype)
975{
976 Scope scope(this);
977 Scoped<InternalClass> ic(scope, internalClasses(Class_Empty)->changeVTable(vtable));
978 return ic->changePrototype(prototype ? prototype->d() : nullptr);
979}
980
981Heap::Object *ExecutionEngine::newObject()
982{
983 return memoryManager->allocate<Object>();
984}
985
986Heap::Object *ExecutionEngine::newObject(Heap::InternalClass *internalClass)
987{
988 return memoryManager->allocObject<Object>(internalClass);
989}
990
991Heap::String *ExecutionEngine::newString(const QString &s)
992{
993 return memoryManager->allocWithStringData<String>(s.size() * sizeof(QChar), s);
994}
995
996Heap::String *ExecutionEngine::newIdentifier(const QString &text)
997{
998 Scope scope(this);
999 ScopedString s(scope, memoryManager->allocWithStringData<String>(text.size() * sizeof(QChar), text));
1000 s->toPropertyKey();
1001 return s->d();
1002}
1003
1004Heap::Object *ExecutionEngine::newStringObject(const String *string)
1005{
1006 return memoryManager->allocate<StringObject>(string);
1007}
1008
1009Heap::Object *ExecutionEngine::newSymbolObject(const Symbol *symbol)
1010{
1011 return memoryManager->allocObject<SymbolObject>(classes[Class_SymbolObject], symbol);
1012}
1013
1014Heap::Object *ExecutionEngine::newNumberObject(double value)
1015{
1016 return memoryManager->allocate<NumberObject>(value);
1017}
1018
1019Heap::Object *ExecutionEngine::newBooleanObject(bool b)
1020{
1021 return memoryManager->allocate<BooleanObject>(b);
1022}
1023
1024Heap::ArrayObject *ExecutionEngine::newArrayObject(int count)
1025{
1026 Scope scope(this);
1027 ScopedArrayObject object(scope, memoryManager->allocate<ArrayObject>());
1028
1029 if (count) {
1030 if (count < 0x1000)
1031 object->arrayReserve(count);
1032 object->setArrayLengthUnchecked(count);
1033 }
1034 return object->d();
1035}
1036
1037Heap::ArrayObject *ExecutionEngine::newArrayObject(const Value *values, int length)
1038{
1039 Scope scope(this);
1040 ScopedArrayObject a(scope, memoryManager->allocate<ArrayObject>());
1041
1042 if (length) {
1043 size_t size = sizeof(Heap::ArrayData) + (length-1)*sizeof(Value);
1044 Heap::SimpleArrayData *d = scope.engine->memoryManager->allocManaged<SimpleArrayData>(size);
1045 d->init();
1046 d->type = Heap::ArrayData::Simple;
1047 d->offset = 0;
1048 d->values.alloc = length;
1049 d->values.size = length;
1050 // this doesn't require a write barrier, things will be ok, when the new array data gets inserted into
1051 // the parent object
1052 memcpy(&d->values.values, values, length*sizeof(Value));
1053 a->d()->arrayData.set(this, d);
1054 a->setArrayLengthUnchecked(length);
1055 }
1056 return a->d();
1057}
1058
1059Heap::ArrayObject *ExecutionEngine::newArrayObject(const QStringList &list)
1060{
1061 return memoryManager->allocate<ArrayObject>(list);
1062}
1063
1064Heap::ArrayObject *ExecutionEngine::newArrayObject(Heap::InternalClass *internalClass)
1065{
1066 return memoryManager->allocObject<ArrayObject>(internalClass);
1067}
1068
1069Heap::ArrayBuffer *ExecutionEngine::newArrayBuffer(const QByteArray &array)
1070{
1071 return memoryManager->allocate<ArrayBuffer>(array);
1072}
1073
1074Heap::ArrayBuffer *ExecutionEngine::newArrayBuffer(size_t length)
1075{
1076 return memoryManager->allocate<ArrayBuffer>(length);
1077}
1078
1079Heap::DateObject *ExecutionEngine::newDateObject(double dateTime)
1080{
1081 return memoryManager->allocate<DateObject>(dateTime);
1082}
1083
1084Heap::DateObject *ExecutionEngine::newDateObject(const QDateTime &dateTime)
1085{
1086 return memoryManager->allocate<DateObject>(dateTime);
1087}
1088
1089Heap::DateObject *ExecutionEngine::newDateObject(
1090 QDate date, Heap::Object *parent, int index, uint flags)
1091{
1092 return memoryManager->allocate<DateObject>(
1093 date, parent, index, Heap::ReferenceObject::Flags(flags));
1094}
1095
1096Heap::DateObject *ExecutionEngine::newDateObject(
1097 QTime time, Heap::Object *parent, int index, uint flags)
1098{
1099 return memoryManager->allocate<DateObject>(
1100 time, parent, index, Heap::ReferenceObject::Flags(flags));
1101}
1102
1103Heap::DateObject *ExecutionEngine::newDateObject(
1104 QDateTime dateTime, Heap::Object *parent, int index, uint flags)
1105{
1106 return memoryManager->allocate<DateObject>(
1107 dateTime, parent, index, Heap::ReferenceObject::Flags(flags));
1108}
1109
1110Heap::RegExpObject *ExecutionEngine::newRegExpObject(const QString &pattern, int flags)
1111{
1112 Scope scope(this);
1113 Scoped<RegExp> re(scope, RegExp::create(this, pattern, static_cast<CompiledData::RegExp::Flags>(flags)));
1114 return newRegExpObject(re);
1115}
1116
1117Heap::RegExpObject *ExecutionEngine::newRegExpObject(RegExp *re)
1118{
1119 return memoryManager->allocate<RegExpObject>(re);
1120}
1121
1122#if QT_CONFIG(regularexpression)
1123Heap::RegExpObject *ExecutionEngine::newRegExpObject(const QRegularExpression &re)
1124{
1125 return memoryManager->allocate<RegExpObject>(re);
1126}
1127#endif
1128
1129Heap::UrlObject *ExecutionEngine::newUrlObject()
1130{
1131 return memoryManager->allocate<UrlObject>();
1132}
1133
1134Heap::UrlObject *ExecutionEngine::newUrlObject(const QUrl &url)
1135{
1136 Scope scope(this);
1137 Scoped<UrlObject> urlObject(scope, newUrlObject());
1138 urlObject->setUrl(url);
1139 return urlObject->d();
1140}
1141
1142Heap::UrlSearchParamsObject *ExecutionEngine::newUrlSearchParamsObject()
1143{
1144 return memoryManager->allocate<UrlSearchParamsObject>();
1145}
1146
1147Heap::Object *ExecutionEngine::newErrorObject(const Value &value)
1148{
1149 return ErrorObject::create<ErrorObject>(this, value, errorCtor());
1150}
1151
1152Heap::Object *ExecutionEngine::newErrorObject(const QString &message)
1153{
1154 return ErrorObject::create<ErrorObject>(this, message);
1155}
1156
1157Heap::Object *ExecutionEngine::newSyntaxErrorObject(const QString &message)
1158{
1159 return ErrorObject::create<SyntaxErrorObject>(this, message);
1160}
1161
1162Heap::Object *ExecutionEngine::newSyntaxErrorObject(const QString &message, const QString &fileName, int line, int column)
1163{
1164 return ErrorObject::create<SyntaxErrorObject>(this, message, fileName, line, column);
1165}
1166
1167
1168Heap::Object *ExecutionEngine::newReferenceErrorObject(const QString &message)
1169{
1170 return ErrorObject::create<ReferenceErrorObject>(this, message);
1171}
1172
1173Heap::Object *ExecutionEngine::newReferenceErrorObject(const QString &message, const QString &fileName, int line, int column)
1174{
1175 return ErrorObject::create<ReferenceErrorObject>(this, message, fileName, line, column);
1176}
1177
1178
1179Heap::Object *ExecutionEngine::newTypeErrorObject(const QString &message)
1180{
1181 return ErrorObject::create<TypeErrorObject>(this, message);
1182}
1183
1184Heap::Object *ExecutionEngine::newRangeErrorObject(const QString &message)
1185{
1186 return ErrorObject::create<RangeErrorObject>(this, message);
1187}
1188
1189Heap::Object *ExecutionEngine::newURIErrorObject(const Value &message)
1190{
1191 return ErrorObject::create<URIErrorObject>(this, message, uRIErrorCtor());
1192}
1193
1194Heap::PromiseObject *ExecutionEngine::newPromiseObject()
1195{
1196 if (!m_reactionHandler) {
1197 m_reactionHandler.reset(new Promise::ReactionHandler);
1198 }
1199
1200 Scope scope(this);
1201 Scoped<PromiseObject> object(scope, memoryManager->allocate<PromiseObject>(this));
1202 return object->d();
1203}
1204
1205Heap::Object *ExecutionEngine::newPromiseObject(const QV4::FunctionObject *thisObject, const QV4::PromiseCapability *capability)
1206{
1207 if (!m_reactionHandler) {
1208 m_reactionHandler.reset(new Promise::ReactionHandler);
1209 }
1210
1211 Scope scope(this);
1212 Scoped<CapabilitiesExecutorWrapper> executor(scope, memoryManager->allocate<CapabilitiesExecutorWrapper>());
1213 executor->d()->capabilities.set(this, capability->d());
1214 executor->insertMember(id_length(), Primitive::fromInt32(2), Attr_NotWritable|Attr_NotEnumerable);
1215
1216 ScopedObject object(scope, thisObject->callAsConstructor(executor, 1));
1217 return object->d();
1218}
1219
1220Promise::ReactionHandler *ExecutionEngine::getPromiseReactionHandler()
1221{
1222 Q_ASSERT(m_reactionHandler);
1223 return m_reactionHandler.data();
1224}
1225
1226Heap::Object *ExecutionEngine::newURIErrorObject(const QString &message)
1227{
1228 return ErrorObject::create<URIErrorObject>(this, message);
1229}
1230
1231Heap::Object *ExecutionEngine::newEvalErrorObject(const QString &message)
1232{
1233 return ErrorObject::create<EvalErrorObject>(this, message);
1234}
1235
1236Heap::Object *ExecutionEngine::newVariantObject(const QMetaType type, const void *data)
1237{
1238 return memoryManager->allocate<VariantObject>(type, data);
1239}
1240
1241Heap::Object *ExecutionEngine::newForInIteratorObject(Object *o)
1242{
1243 Scope scope(this);
1244 ScopedObject obj(scope, memoryManager->allocate<ForInIteratorObject>(o));
1245 return obj->d();
1246}
1247
1248Heap::Object *ExecutionEngine::newMapIteratorObject(Object *o)
1249{
1250 return memoryManager->allocate<MapIteratorObject>(o->d(), this);
1251}
1252
1253Heap::Object *ExecutionEngine::newSetIteratorObject(Object *o)
1254{
1255 return memoryManager->allocate<SetIteratorObject>(o->d(), this);
1256}
1257
1258Heap::Object *ExecutionEngine::newArrayIteratorObject(Object *o)
1259{
1260 return memoryManager->allocate<ArrayIteratorObject>(o->d(), this);
1261}
1262
1263Heap::QmlContext *ExecutionEngine::qmlContext() const
1264{
1265 return currentStackFrame
1266 ? static_cast<Heap::QmlContext *>(qmlContext(currentContext()->d()))
1267 : nullptr;
1268}
1269
1270QObject *ExecutionEngine::qmlScopeObject() const
1271{
1272 Heap::QmlContext *ctx = qmlContext();
1273 if (!ctx)
1274 return nullptr;
1275
1276 return ctx->qml()->scopeObject;
1277}
1278
1279QQmlRefPointer<QQmlContextData> ExecutionEngine::callingQmlContext() const
1280{
1281 Heap::QmlContext *ctx = qmlContext();
1282 if (!ctx)
1283 return nullptr;
1284
1285 return ctx->qml()->context;
1286}
1287
1288StackTrace ExecutionEngine::stackTrace(int frameLimit) const
1289{
1290 StackTrace stack;
1291
1292 CppStackFrame *f = currentStackFrame;
1293 while (f && frameLimit) {
1294 QV4::StackFrame frame;
1295 frame.source = f->source();
1296 frame.function = f->function();
1297 frame.line = f->lineNumber();
1298
1299 stack.append(frame);
1300 if (f->isJSTypesFrame()) {
1301 if (static_cast<JSTypesStackFrame *>(f)->isTailCalling()) {
1302 QV4::StackFrame frame;
1303 frame.function = QStringLiteral("[elided tail calls]");
1304 stack.append(frame);
1305 }
1306 }
1307 --frameLimit;
1308 f = f->parentFrame();
1309 }
1310
1311 return stack;
1312}
1313
1314/* Helper and "C" linkage exported function to format a GDBMI stacktrace for
1315 * invocation by a debugger.
1316 * Sample GDB invocation: print qt_v4StackTrace((void*)0x7fffffffb290)
1317 * Sample CDB invocation: .call Qt5Qmld!qt_v4StackTrace(0x7fffffffb290) ; gh
1318 * Note: The helper is there to suppress MSVC warning 4190 about anything
1319 * with UDT return types in a "C" linkage function. */
1320
1321static inline char *v4StackTrace(const ExecutionContext *context)
1322{
1323 QString result;
1324 QTextStream str(&result);
1325 str << "stack=[";
1326 if (context && context->engine()) {
1327 const QList<StackFrame> stackTrace = context->engine()->stackTrace(20);
1328 for (int i = 0; i < stackTrace.size(); ++i) {
1329 if (i)
1330 str << ',';
1331 const QUrl url(stackTrace.at(i).source);
1332 const QString fileName = url.isLocalFile() ? url.toLocalFile() : url.toString();
1333 str << "frame={level=\"" << i << "\",func=\"" << stackTrace.at(i).function
1334 << "\",file=\"" << fileName << "\",fullname=\"" << fileName
1335 << "\",line=\"" << qAbs(stackTrace.at(i).line) << "\",language=\"js\"}";
1336 }
1337 }
1338 str << ']';
1339 return qstrdup(result.toLocal8Bit().constData());
1340}
1341
1342extern "C" Q_QML_EXPORT char *qt_v4StackTrace(void *executionContext)
1343{
1344 return v4StackTrace(reinterpret_cast<const ExecutionContext *>(executionContext));
1345}
1346
1347extern "C" Q_QML_EXPORT char *qt_v4StackTraceForEngine(void *executionEngine)
1348{
1349 auto engine = (reinterpret_cast<const ExecutionEngine *>(executionEngine));
1350 return v4StackTrace(engine->currentContext());
1351}
1352
1353QUrl ExecutionEngine::resolvedUrl(const QString &file)
1354{
1355 QUrl src(file);
1356 if (!src.isRelative())
1357 return src;
1358
1359 QUrl base;
1360 CppStackFrame *f = currentStackFrame;
1361 while (f) {
1362 if (f->v4Function) {
1363 base = f->v4Function->finalUrl();
1364 break;
1365 }
1366 f = f->parentFrame();
1367 }
1368
1369 if (base.isEmpty() && globalCode)
1370 base = globalCode->finalUrl();
1371
1372 if (base.isEmpty())
1373 return src;
1374
1375 return base.resolved(src);
1376}
1377
1378void ExecutionEngine::markObjects(MarkStack *markStack)
1379{
1380 for (int i = 0; i < NClasses; ++i) {
1381 if (Heap::InternalClass *c = classes[i])
1382 c->mark(markStack);
1383 }
1384
1385 identifierTable->markObjects(markStack);
1386
1387 for (const auto &compilationUnit : std::as_const(m_compilationUnits))
1388 compilationUnit->markObjects(markStack);
1389}
1390
1391ReturnedValue ExecutionEngine::throwError(const Value &value)
1392{
1393 // we can get in here with an exception already set, as the runtime
1394 // doesn't check after every operation that can throw.
1395 // in this case preserve the first exception to give correct error
1396 // information
1397 if (hasException)
1398 return Encode::undefined();
1399
1400 hasException = true;
1401 *exceptionValue = value;
1402 QV4::Scope scope(this);
1403 QV4::Scoped<ErrorObject> error(scope, value);
1404 if (!!error)
1405 exceptionStackTrace = *error->d()->stackTrace;
1406 else
1407 exceptionStackTrace = stackTrace();
1408
1409 if (QV4::Debugging::Debugger *debug = debugger())
1410 debug->aboutToThrow();
1411
1412 return Encode::undefined();
1413}
1414
1415ReturnedValue ExecutionEngine::catchException(StackTrace *trace)
1416{
1417 Q_ASSERT(hasException);
1418 if (trace)
1419 *trace = exceptionStackTrace;
1420 exceptionStackTrace.clear();
1421 hasException = false;
1422 ReturnedValue res = exceptionValue->asReturnedValue();
1423 *exceptionValue = Value::emptyValue();
1424 return res;
1425}
1426
1427ReturnedValue ExecutionEngine::throwError(const QString &message)
1428{
1429 Scope scope(this);
1430 ScopedValue v(scope, newString(message));
1431 v = newErrorObject(v);
1432 return throwError(v);
1433}
1434
1435ReturnedValue ExecutionEngine::throwSyntaxError(const QString &message, const QString &fileName, int line, int column)
1436{
1437 Scope scope(this);
1438 ScopedObject error(scope, newSyntaxErrorObject(message, fileName, line, column));
1439 return throwError(error);
1440}
1441
1442ReturnedValue ExecutionEngine::throwSyntaxError(const QString &message)
1443{
1444 Scope scope(this);
1445 ScopedObject error(scope, newSyntaxErrorObject(message));
1446 return throwError(error);
1447}
1448
1449
1450ReturnedValue ExecutionEngine::throwTypeError()
1451{
1452 Scope scope(this);
1453 ScopedObject error(scope, newTypeErrorObject(QStringLiteral("Type error")));
1454 return throwError(error);
1455}
1456
1457ReturnedValue ExecutionEngine::throwTypeError(const QString &message)
1458{
1459 Scope scope(this);
1460 ScopedObject error(scope, newTypeErrorObject(message));
1461 return throwError(error);
1462}
1463
1464ReturnedValue ExecutionEngine::throwReferenceError(const QString &name)
1465{
1466 Scope scope(this);
1467 QString msg = name + QLatin1String(" is not defined");
1468 ScopedObject error(scope, newReferenceErrorObject(msg));
1469 return throwError(error);
1470}
1471
1472ReturnedValue ExecutionEngine::throwReferenceError(const Value &value)
1473{
1474 Scope scope(this);
1475 ScopedString s(scope, value.toString(this));
1476 QString msg = s->toQString() + QLatin1String(" is not defined");
1477 ScopedObject error(scope, newReferenceErrorObject(msg));
1478 return throwError(error);
1479}
1480
1481ReturnedValue ExecutionEngine::throwReferenceError(const QString &message, const QString &fileName, int line, int column)
1482{
1483 Scope scope(this);
1484 QString msg = message;
1485 ScopedObject error(scope, newReferenceErrorObject(msg, fileName, line, column));
1486 return throwError(error);
1487}
1488
1489ReturnedValue ExecutionEngine::throwRangeError(const QString &message)
1490{
1491 Scope scope(this);
1492 ScopedObject error(scope, newRangeErrorObject(message));
1493 return throwError(error);
1494}
1495
1496ReturnedValue ExecutionEngine::throwRangeError(const Value &value)
1497{
1498 Scope scope(this);
1499 ScopedString s(scope, value.toString(this));
1500 QString msg = s->toQString() + QLatin1String(" out of range");
1501 ScopedObject error(scope, newRangeErrorObject(msg));
1502 return throwError(error);
1503}
1504
1505ReturnedValue ExecutionEngine::throwURIError(const Value &msg)
1506{
1507 Scope scope(this);
1508 ScopedObject error(scope, newURIErrorObject(msg));
1509 return throwError(error);
1510}
1511
1512ReturnedValue ExecutionEngine::throwUnimplemented(const QString &message)
1513{
1514 Scope scope(this);
1515 ScopedValue v(scope, newString(QLatin1String("Unimplemented ") + message));
1516 v = newErrorObject(v);
1517 return throwError(v);
1518}
1519
1520
1521QQmlError ExecutionEngine::catchExceptionAsQmlError()
1522{
1523 QV4::StackTrace trace;
1524 QV4::Scope scope(this);
1525 QV4::ScopedValue exception(scope, catchException(&trace));
1526 QQmlError error;
1527 if (!trace.isEmpty()) {
1528 QV4::StackFrame frame = trace.constFirst();
1529 error.setUrl(QUrl(frame.source));
1530 error.setLine(qAbs(frame.line));
1531 error.setColumn(frame.column);
1532 }
1533 QV4::Scoped<QV4::ErrorObject> errorObj(scope, exception);
1534 error.setDescription(exception->toQStringNoThrow());
1535 return error;
1536}
1537
1538void ExecutionEngine::amendException()
1539{
1540 const int missingLineNumber = currentStackFrame->missingLineNumber();
1541 const int lineNumber = currentStackFrame->lineNumber();
1542 Q_ASSERT(missingLineNumber != lineNumber);
1543
1544 auto amendStackTrace = [&](QV4::StackTrace *stackTrace) {
1545 for (auto it = stackTrace->begin(), end = stackTrace->end(); it != end; ++it) {
1546 if (it->line == missingLineNumber) {
1547 it->line = lineNumber;
1548 break;
1549 }
1550 }
1551 };
1552
1553 amendStackTrace(&exceptionStackTrace);
1554
1555 QV4::Scope scope(this);
1556 QV4::Scoped<QV4::ErrorObject> error(scope, *exceptionValue);
1557 if (error) // else some other value was thrown
1558 amendStackTrace(error->d()->stackTrace);
1559}
1560
1561// Variant conversion code
1562
1565static QVariant toVariant(
1566 const QV4::Value &value, QMetaType typeHint, JSToQVariantConversionBehavior conversionBehavior,
1567 V4ObjectSet *visitedObjects);
1568static QObject *qtObjectFromJS(const QV4::Value &value);
1569static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects = nullptr,
1571static bool convertToNativeQObject(const QV4::Value &value, QMetaType targetType, void **result);
1572
1573static QVariant toVariant(const QV4::Value &value, QMetaType metaType, JSToQVariantConversionBehavior conversionBehavior,
1574 V4ObjectSet *visitedObjects)
1575{
1576 Q_ASSERT (!value.isEmpty());
1577
1578 if (const QV4::VariantObject *v = value.as<QV4::VariantObject>())
1579 return v->d()->data();
1580
1581 if (metaType == QMetaType::fromType<bool>())
1582 return QVariant(value.toBoolean());
1583
1584 if (metaType == QMetaType::fromType<double>())
1585 return QVariant(value.toNumber());
1586
1587 if (metaType == QMetaType::fromType<float>())
1588 return QVariant(float(value.toNumber()));
1589
1590 if (metaType == QMetaType::fromType<QJsonValue>())
1591 return QVariant::fromValue(QV4::JsonObject::toJsonValue(value));
1592
1593 if (metaType == QMetaType::fromType<QJSValue>())
1594 return QVariant::fromValue(QJSValuePrivate::fromReturnedValue(value.asReturnedValue()));
1595
1596 if (const QV4::Object *o = value.as<QV4::Object>()) {
1597 QV4::Scope scope(o->engine());
1598 QV4::ScopedObject object(scope, o);
1599 if (metaType == QMetaType::fromType<QJsonObject>()
1600 && !value.as<ArrayObject>() && !value.as<FunctionObject>()) {
1601 return QVariant::fromValue(QV4::JsonObject::toJsonObject(object));
1602 } else if (QV4::QObjectWrapper *wrapper = object->as<QV4::QObjectWrapper>()) {
1603 return QVariant::fromValue<QObject *>(wrapper->object());
1604 } else if (object->as<QV4::QQmlContextWrapper>()) {
1605 return QVariant();
1606 } else if (QV4::QQmlTypeWrapper *w = object->as<QV4::QQmlTypeWrapper>()) {
1607 return w->toVariant();
1608 } else if (QV4::QQmlValueTypeWrapper *v = object->as<QV4::QQmlValueTypeWrapper>()) {
1609 return v->toVariant();
1610 } else if (QV4::QmlListWrapper *l = object->as<QV4::QmlListWrapper>()) {
1611 return l->toVariant();
1612 } else if (QV4::Sequence *s = object->as<QV4::Sequence>()) {
1613 if (metaType.isValid()
1614 && metaType != QMetaType::fromType<QVariant>()
1615 && metaType != s->d()->listType()) {
1616 // If we can, produce an accurate result.
1617 const QVariant result = QV4::SequencePrototype::toVariant(value, metaType);
1618 if (result.isValid())
1619 return result;
1620 }
1621
1622 // Otherwise produce the "natural" type of the sequence.
1623 return QV4::SequencePrototype::toVariant(s);
1624 } else if (auto association = object->as<QV4::VariantAssociationObject>()) {
1625 return association->d()->toVariant();
1626 }
1627 }
1628
1629 if (const QV4::ArrayObject *o = value.as<ArrayObject>()) {
1630 QV4::Scope scope(o->engine());
1631 QV4::ScopedArrayObject a(scope, o);
1632 if (metaType == QMetaType::fromType<QList<QObject *>>()) {
1633 QList<QObject *> list;
1634 uint length = a->getLength();
1635 QV4::Scoped<QV4::Object> qobjectLike(scope);
1636 for (uint ii = 0; ii < length; ++ii) {
1637 qobjectLike = a->get(ii);
1638 list << qtObjectFromJS(qobjectLike);
1639 }
1640
1641 return QVariant::fromValue<QList<QObject*> >(list);
1642 } else if (metaType == QMetaType::fromType<QJsonArray>()) {
1643 return QVariant::fromValue(QV4::JsonObject::toJsonArray(a));
1644 }
1645
1646 QVariant retn = QV4::SequencePrototype::toVariant(value, metaType);
1647 if (retn.isValid())
1648 return retn;
1649 }
1650
1651 if (value.isUndefined())
1652 return QVariant();
1653 if (value.isNull())
1654 return QVariant::fromValue(nullptr);
1655 if (value.isBoolean())
1656 return value.booleanValue();
1657 if (value.isInteger())
1658 return value.integerValue();
1659 if (value.isNumber())
1660 return value.asDouble();
1661 if (String *s = value.stringValue()) {
1662 const QString &str = s->toQString();
1663 // QChars are stored as a strings
1664 if (metaType == QMetaType::fromType<QChar>() && str.size() == 1)
1665 return str.at(0);
1666 return str;
1667 }
1668 if (const QV4::DateObject *d = value.as<DateObject>()) {
1669 // NOTE: since we convert QTime to JS Date,
1670 // round trip will change the variant type (to QDateTime)!
1671
1672 if (metaType == QMetaType::fromType<QDate>())
1673 return DateObject::dateTimeToDate(d->toQDateTime());
1674
1675 if (metaType == QMetaType::fromType<QTime>())
1676 return d->toQDateTime().time();
1677
1678 if (metaType == QMetaType::fromType<QString>())
1679 return d->toString();
1680
1681 return d->toQDateTime();
1682 }
1683 if (const QV4::UrlObject *d = value.as<UrlObject>())
1684 return d->toQUrl();
1685 if (const ArrayBuffer *d = value.as<ArrayBuffer>())
1686 return d->asByteArray();
1687 if (const Symbol *symbol = value.as<Symbol>()) {
1688 return conversionBehavior == JSToQVariantConversionBehavior::Never
1689 ? QVariant::fromValue(QJSValuePrivate::fromReturnedValue(symbol->asReturnedValue()))
1690 : symbol->descriptiveString();
1691 }
1692
1693 const QV4::Object *object = value.as<QV4::Object>();
1694 Q_ASSERT(object);
1695 QV4::Scope scope(object->engine());
1696 QV4::ScopedObject o(scope, object);
1697
1698#if QT_CONFIG(regularexpression)
1699 if (QV4::RegExpObject *re = o->as<QV4::RegExpObject>())
1700 return re->toQRegularExpression();
1701#endif
1702
1703 if (metaType.isValid() && !(metaType.flags() & QMetaType::PointerToQObject)) {
1704 const QVariant result
1705 = QQmlValueTypeProvider::createValueType(value, metaType, scope.engine);
1706 if (result.isValid())
1707 return result;
1708 }
1709
1710 if (conversionBehavior == JSToQVariantConversionBehavior::Never)
1711 return QVariant::fromValue(QJSValuePrivate::fromReturnedValue(o->asReturnedValue()));
1712
1713 return objectToVariant(o, visitedObjects, conversionBehavior);
1714}
1715
1716QVariant ExecutionEngine::toVariantLossy(const Value &value)
1717{
1718 return ::toVariant(value, QMetaType(), JSToQVariantConversionBehavior::Aggressive, nullptr);
1719}
1720
1721QVariant ExecutionEngine::toVariant(
1722 const Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols)
1723{
1724 auto behavior = createJSValueForObjectsAndSymbols ? JSToQVariantConversionBehavior::Never
1725 : JSToQVariantConversionBehavior::Safish;
1726 return ::toVariant(value, typeHint, behavior, nullptr);
1727}
1728
1729template<typename Association>
1731 const QV4::Object *o, V4ObjectSet *visitedObjects,
1732 JSToQVariantConversionBehavior conversionBehvior)
1733{
1734 Association association;
1735 QV4::Scope scope(o->engine());
1736 QV4::ObjectIterator it(scope, o, QV4::ObjectIterator::EnumerableOnly);
1737 QV4::ScopedValue name(scope);
1738 QV4::ScopedValue val(scope);
1739 while (1) {
1740 name = it.nextPropertyNameAsString(val);
1741 if (name->isNull())
1742 break;
1743
1744 QString key = name->toQStringNoThrow();
1745 association.insert(key, ::toVariant(
1746 val, /*type hint*/ QMetaType {},
1747 conversionBehvior, visitedObjects));
1748 }
1749 return association;
1750}
1751
1752static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects,
1753 JSToQVariantConversionBehavior conversionBehvior)
1754{
1755 Q_ASSERT(o);
1756
1757 QV4::ExecutionEngine *engine = o->engine();
1758 if (Q_UNLIKELY(engine->checkStackLimits())) {
1759 if (o->as<ArrayObject>())
1760 return QVariantList();
1761 return QVariantMap();
1762 }
1763
1764 V4ObjectSet recursionGuardSet;
1765 if (!visitedObjects) {
1766 visitedObjects = &recursionGuardSet;
1767 } else if (visitedObjects->contains(o->d())) {
1768 // Avoid recursion.
1769 // For compatibility with QVariant{List,Map} conversion, we return an
1770 // empty object (and no error is thrown).
1771 if (o->as<ArrayObject>())
1772 return QVariantList();
1773 return QVariantMap();
1774 }
1775 visitedObjects->insert(o->d());
1776
1777 QVariant result;
1778
1779 if (o->as<ArrayObject>()) {
1780 QV4::Scope scope(engine);
1781 QV4::ScopedArrayObject a(scope, o->asReturnedValue());
1782 QV4::ScopedValue v(scope);
1783 QVariantList list;
1784
1785 int length = a->getLength();
1786 for (int ii = 0; ii < length; ++ii) {
1787 v = a->get(ii);
1788 list << ::toVariant(v, QMetaType {}, conversionBehvior,
1789 visitedObjects);
1790 }
1791
1792 result = list;
1793 } else if (o->getPrototypeOf() == engine->objectPrototype()->d()
1794 || (conversionBehvior == JSToQVariantConversionBehavior::Aggressive &&
1795 !o->as<QV4::FunctionObject>())) {
1796 /* FunctionObject is excluded for historical reasons, even though
1797 objects with a custom prototype risk losing information
1798 But the Aggressive path is used only in QJSValue::toVariant
1799 which is documented to be lossy
1800 */
1801 result = objectToVariantAssociation<QVariantMap>(o, visitedObjects, conversionBehvior);
1802 } else {
1803 // If it's not a plain object, we can only save it as QJSValue.
1804 result = QVariant::fromValue(QJSValuePrivate::fromReturnedValue(o->asReturnedValue()));
1805 }
1806
1807 visitedObjects->remove(o->d());
1808 return result;
1809}
1810
1811/*!
1812 \internal
1813
1814 Transform the given \a metaType and \a ptr into a JavaScript representation.
1815 */
1816QV4::ReturnedValue ExecutionEngine::fromData(
1817 QMetaType metaType, const void *ptr,
1818 QV4::Heap::Object *container, int property, uint flags)
1819{
1820 const auto createSequence = [&](const QMetaSequence metaSequence) {
1821 QV4::Scope scope(this);
1822 QV4::Scoped<Sequence> sequence(scope);
1823 if (container) {
1824 return QV4::SequencePrototype::newSequence(
1825 this, metaType, metaSequence, ptr,
1826 container, property, Heap::ReferenceObject::Flags(flags));
1827 } else {
1828 return QV4::SequencePrototype::fromData(this, metaType, metaSequence, ptr);
1829 }
1830 };
1831
1832 const int type = metaType.id();
1833 if (type < QMetaType::User) {
1834 switch (QMetaType::Type(type)) {
1835 case QMetaType::UnknownType:
1836 case QMetaType::Void:
1837 return QV4::Encode::undefined();
1838 case QMetaType::Nullptr:
1839 case QMetaType::VoidStar:
1840 return QV4::Encode::null();
1841 case QMetaType::Bool:
1842 return QV4::Encode(*reinterpret_cast<const bool*>(ptr));
1843 case QMetaType::Int:
1844 return QV4::Encode(*reinterpret_cast<const int*>(ptr));
1845 case QMetaType::UInt:
1846 return QV4::Encode(*reinterpret_cast<const uint*>(ptr));
1847 case QMetaType::Long:
1848 return QV4::Encode((double)*reinterpret_cast<const long *>(ptr));
1849 case QMetaType::ULong:
1850 return QV4::Encode((double)*reinterpret_cast<const ulong *>(ptr));
1851 case QMetaType::LongLong:
1852 return QV4::Encode((double)*reinterpret_cast<const qlonglong*>(ptr));
1853 case QMetaType::ULongLong:
1854 return QV4::Encode((double)*reinterpret_cast<const qulonglong*>(ptr));
1855 case QMetaType::Double:
1856 return QV4::Encode(*reinterpret_cast<const double*>(ptr));
1857 case QMetaType::QString:
1858 return newString(*reinterpret_cast<const QString*>(ptr))->asReturnedValue();
1859 case QMetaType::QByteArray:
1860 return newArrayBuffer(*reinterpret_cast<const QByteArray*>(ptr))->asReturnedValue();
1861 case QMetaType::Float:
1862 return QV4::Encode(*reinterpret_cast<const float*>(ptr));
1863 case QMetaType::Short:
1864 return QV4::Encode((int)*reinterpret_cast<const short*>(ptr));
1865 case QMetaType::UShort:
1866 return QV4::Encode((int)*reinterpret_cast<const unsigned short*>(ptr));
1867 case QMetaType::Char:
1868 return QV4::Encode((int)*reinterpret_cast<const char*>(ptr));
1869 case QMetaType::UChar:
1870 return QV4::Encode((int)*reinterpret_cast<const unsigned char*>(ptr));
1871 case QMetaType::SChar:
1872 return QV4::Encode((int)*reinterpret_cast<const signed char*>(ptr));
1873 case QMetaType::QChar:
1874 return newString(*reinterpret_cast<const QChar *>(ptr))->asReturnedValue();
1875 case QMetaType::Char16:
1876 return newString(QChar(*reinterpret_cast<const char16_t *>(ptr)))->asReturnedValue();
1877 case QMetaType::QDateTime:
1878 return QV4::Encode(newDateObject(
1879 *reinterpret_cast<const QDateTime *>(ptr),
1880 container, property, flags));
1881 case QMetaType::QDate:
1882 return QV4::Encode(newDateObject(
1883 *reinterpret_cast<const QDate *>(ptr),
1884 container, property, flags));
1885 case QMetaType::QTime:
1886 return QV4::Encode(newDateObject(
1887 *reinterpret_cast<const QTime *>(ptr),
1888 container, property, flags));
1889#if QT_CONFIG(regularexpression)
1890 case QMetaType::QRegularExpression:
1891 return QV4::Encode(newRegExpObject(*reinterpret_cast<const QRegularExpression *>(ptr)));
1892#endif
1893 case QMetaType::QObjectStar:
1894 return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
1895 case QMetaType::QStringList:
1896 return createSequence(QMetaSequence::fromContainer<QStringList>());
1897 case QMetaType::QVariantList:
1898 return createSequence(QMetaSequence::fromContainer<QVariantList>());
1899 case QMetaType::QVariantMap:
1900 return VariantAssociationPrototype::fromQVariantMap(
1901 this,
1902 *reinterpret_cast<const QVariantMap *>(ptr),
1903 container, property, Heap::ReferenceObject::Flags(flags));
1904 case QMetaType::QVariantHash:
1905 return VariantAssociationPrototype::fromQVariantHash(
1906 this,
1907 *reinterpret_cast<const QVariantHash *>(ptr),
1908 container, property, Heap::ReferenceObject::Flags(flags));
1909 case QMetaType::QJsonValue:
1910 return QV4::JsonObject::fromJsonValue(this, *reinterpret_cast<const QJsonValue *>(ptr));
1911 case QMetaType::QJsonObject:
1912 return QV4::JsonObject::fromJsonObject(this, *reinterpret_cast<const QJsonObject *>(ptr));
1913 case QMetaType::QJsonArray:
1914 return QV4::JsonObject::fromJsonArray(this, *reinterpret_cast<const QJsonArray *>(ptr));
1915 case QMetaType::QPixmap:
1916 case QMetaType::QImage:
1917 // Scarce value types
1918 return QV4::Encode(newVariantObject(metaType, ptr));
1919 default:
1920 break;
1921 }
1922 }
1923
1924 if (metaType.flags() & QMetaType::IsEnumeration)
1925 return fromData(metaType.underlyingType(), ptr, container, property, flags);
1926
1927 QV4::Scope scope(this);
1928 if (metaType == QMetaType::fromType<QQmlListReference>()) {
1929 typedef QQmlListReferencePrivate QDLRP;
1930 QDLRP *p = QDLRP::get((QQmlListReference*)const_cast<void *>(ptr));
1931 if (p->object)
1932 return QV4::QmlListWrapper::create(scope.engine, p->property, p->propertyType);
1933 else
1934 return QV4::Encode::null();
1935 } else if (auto flags = metaType.flags(); flags & QMetaType::IsQmlList) {
1936 // casting to QQmlListProperty<QObject> is slightly nasty, but it's the
1937 // same QQmlListReference does.
1938 const auto *p = static_cast<const QQmlListProperty<QObject> *>(ptr);
1939 if (p->object)
1940 return QV4::QmlListWrapper::create(scope.engine, *p, metaType);
1941 else
1942 return QV4::Encode::null();
1943 } else if (metaType == QMetaType::fromType<QJSValue>()) {
1944 return QJSValuePrivate::convertToReturnedValue(
1945 this, *reinterpret_cast<const QJSValue *>(ptr));
1946 } else if (metaType == QMetaType::fromType<QList<QObject *> >()) {
1947 // XXX Can this be made more by using Array as a prototype and implementing
1948 // directly against QList<QObject*>?
1949 const QList<QObject *> &list = *(const QList<QObject *>*)ptr;
1950 QV4::ScopedArrayObject a(scope, newArrayObject());
1951 a->arrayReserve(list.size());
1952 QV4::ScopedValue v(scope);
1953 for (int ii = 0; ii < list.size(); ++ii)
1954 a->arrayPut(ii, (v = QV4::QObjectWrapper::wrap(this, list.at(ii))));
1955 a->setArrayLengthUnchecked(list.size());
1956 return a.asReturnedValue();
1957 } else if (auto flags = metaType.flags(); flags & QMetaType::PointerToQObject) {
1958 if (flags.testFlag(QMetaType::IsConst))
1959 return QV4::QObjectWrapper::wrapConst(this, *reinterpret_cast<QObject* const *>(ptr));
1960 else
1961 return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
1962 } else if (metaType == QMetaType::fromType<QJSPrimitiveValue>()) {
1963 const QJSPrimitiveValue *primitive = static_cast<const QJSPrimitiveValue *>(ptr);
1964 switch (primitive->type()) {
1965 case QJSPrimitiveValue::Boolean:
1966 return Encode(primitive->asBoolean());
1967 case QJSPrimitiveValue::Integer:
1968 return Encode(primitive->asInteger());
1969 case QJSPrimitiveValue::String:
1970 return newString(primitive->asString())->asReturnedValue();
1971 case QJSPrimitiveValue::Undefined:
1972 return Encode::undefined();
1973 case QJSPrimitiveValue::Null:
1974 return Encode::null();
1975 case QJSPrimitiveValue::Double:
1976 return Encode(primitive->asDouble());
1977 }
1978 }
1979
1980 if (const QMetaObject *vtmo = QQmlMetaType::metaObjectForValueType(metaType)) {
1981 if (container) {
1982 return QV4::QQmlValueTypeWrapper::create(
1983 this, ptr, vtmo, metaType,
1984 container, property, Heap::ReferenceObject::Flags(flags));
1985 } else {
1986 return QV4::QQmlValueTypeWrapper::create(this, ptr, vtmo, metaType);
1987 }
1988 }
1989
1990 const QQmlType listType = QQmlMetaType::qmlListType(metaType);
1991 if (listType.isSequentialContainer())
1992 return createSequence(listType.listMetaSequence());
1993
1994 QMetaSequence::Iterable iterable;
1995 if (QMetaType::convert(metaType, ptr, QMetaType::fromType<QMetaSequence::Iterable>(), &iterable)) {
1996
1997 // If the resulting iterable is useful for anything, turn it into a QV4::Sequence.
1998 const QMetaSequence sequence = iterable.metaContainer();
1999 if (sequence.hasSize() && sequence.canGetValueAtIndex())
2000 return createSequence(sequence);
2001
2002 // As a last resort, try to read the contents of the container via an iterator
2003 // and build a JS array from them.
2004 if (sequence.hasConstIterator() && sequence.canGetValueAtConstIterator()) {
2005 QV4::ScopedArrayObject a(scope, newArrayObject());
2006 QV4::ScopedValue v(scope);
2007 for (auto it = iterable.constBegin(), end = iterable.constEnd(); it != end; ++it) {
2008 v = fromVariant(*it);
2009 a->push_back(v);
2010 }
2011 return a.asReturnedValue();
2012 }
2013 }
2014
2015 return QV4::Encode(newVariantObject(metaType, ptr));
2016}
2017
2018QV4::ReturnedValue QV4::ExecutionEngine::fromVariant(const QVariant &variant)
2019{
2020 return fromData(variant.metaType(), variant.constData());
2021}
2022
2023ReturnedValue ExecutionEngine::fromVariant(
2024 const QVariant &variant, Heap::Object *parent, int property, uint flags)
2025{
2026 return fromData(variant.metaType(), variant.constData(), parent, property, flags);
2027}
2028
2029QVariantMap ExecutionEngine::variantMapFromJS(const Object *o)
2030{
2031 Q_ASSERT(o);
2032 V4ObjectSet visitedObjects;
2033 visitedObjects.insert(o->d());
2034 return objectToVariantAssociation<QVariantMap>(
2035 o, &visitedObjects, JSToQVariantConversionBehavior::Safish);
2036}
2037
2038QVariantHash ExecutionEngine::variantHashFromJS(const Object *o)
2039{
2040 Q_ASSERT(o);
2041 V4ObjectSet visitedObjects;
2042 visitedObjects.insert(o->d());
2043 return objectToVariantAssociation<QVariantHash>(
2044 o, &visitedObjects, JSToQVariantConversionBehavior::Safish);
2045}
2046
2047// Converts the meta-type defined by the given type and data to JS.
2048// Returns the value if conversion succeeded, an empty handle otherwise.
2049QV4::ReturnedValue ExecutionEngine::metaTypeToJS(QMetaType type, const void *data)
2050{
2051 Q_ASSERT(data != nullptr);
2052
2053 if (type == QMetaType::fromType<QVariant>()) {
2054 // unwrap it: this is tested in QJSEngine, and makes the most sense for
2055 // end-user code too.
2056 return fromVariant(*reinterpret_cast<const QVariant*>(data));
2057 } else if (type == QMetaType::fromType<QUrl>()) {
2058 // Create a proper URL object here, rather than a variant.
2059 return newUrlObject(*reinterpret_cast<const QUrl *>(data))->asReturnedValue();
2060 }
2061
2062 return fromData(type, data);
2063}
2064
2065int ExecutionEngine::maxJSStackSize() const
2066{
2067 return s_maxJSStackSize;
2068}
2069
2070int ExecutionEngine::maxGCStackSize() const
2071{
2072 return s_maxGCStackSize;
2073}
2074
2075/*!
2076 \internal
2077 Returns \a length converted to int if its safe to
2078 pass to \c Scope::alloc.
2079 Otherwise it throws a RangeError, and returns 0.
2080 */
2081int ExecutionEngine::safeForAllocLength(qint64 len64)
2082{
2083 if (len64 < 0ll || len64 > qint64(std::numeric_limits<int>::max())) {
2084 throwRangeError(QStringLiteral("Invalid array length."));
2085 return 0;
2086 }
2087 if (len64 > qint64(this->jsStackLimit - this->jsStackTop)) {
2088 throwRangeError(QStringLiteral("Array too large for apply()."));
2089 return 0;
2090 }
2091 return len64;
2092}
2093
2094ReturnedValue ExecutionEngine::global()
2095{
2096 return globalObject->asReturnedValue();
2097}
2098
2099QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::compilationUnitForUrl(const QUrl &url) const
2100{
2101 // Gives the _most recently inserted_ CU of that URL. That's what we want.
2102 return m_compilationUnits.value(url);
2103}
2104
2105QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::executableCompilationUnit(
2106 QQmlRefPointer<CompiledData::CompilationUnit> &&unit)
2107{
2108 const QUrl url = unit->finalUrl();
2109 auto [begin, end] = std::as_const(m_compilationUnits).equal_range(url);
2110
2111 for (auto it = begin; it != end; ++it) {
2112 if ((*it)->baseCompilationUnit() == unit)
2113 return *it;
2114 }
2115
2116 auto executableUnit = m_compilationUnits.insert(
2117 url, ExecutableCompilationUnit::create(std::move(unit), this));
2118 // runtime data should not be initialized yet, so we don't need to mark the CU
2119 Q_ASSERT(!(*executableUnit)->runtimeStrings);
2120 return *executableUnit;
2121}
2122
2123QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::insertCompilationUnit(QQmlRefPointer<CompiledData::CompilationUnit> &&unit) {
2124 QUrl url = unit->finalUrl();
2125 auto executableUnit = ExecutableCompilationUnit::create(std::move(unit), this);
2126 /* Compilation Units stored in the engine are part of the gc roots,
2127 so we don't trigger any write-barrier when they are added. Use
2128 markCustom to make sure they are still marked when we insert them */
2129 QV4::WriteBarrier::markCustom(this, [&executableUnit](QV4::MarkStack *ms) {
2130 executableUnit->markObjects(ms);
2131 });
2132 return *m_compilationUnits.insert(std::move(url), std::move(executableUnit));
2133}
2134
2135void ExecutionEngine::trimCompilationUnits()
2136{
2137 for (auto it = m_compilationUnits.begin(); it != m_compilationUnits.end();) {
2138 if ((*it)->count() == 1)
2139 it = m_compilationUnits.erase(it);
2140 else
2141 ++it;
2142 }
2143}
2144
2145void ExecutionEngine::trimCompilationUnitsForUrl(const QUrl &url)
2146{
2147 auto it = m_compilationUnits.find(url);
2148 while (it != m_compilationUnits.end() && it.key() == url) {
2149 if ((*it)->count() == 1)
2150 it = m_compilationUnits.erase(it);
2151 else
2152 ++it;
2153 }
2154}
2155
2156/*!
2157 * Returns any _existing_ module for the given \a url and \a referrer. This
2158 * does not actually load anything. You must guarantee the existence of the
2159 * module by keeping a (direct or indirect) reference to it somewhere, or you
2160 * must live with a possible nullptr being returned.
2161 */
2162ExecutionEngine::Module ExecutionEngine::moduleForUrl(
2163 const QUrl &url, const ExecutableCompilationUnit *referrer)
2164{
2165 QUrl resolved = referrer
2166 ? referrer->finalUrl().resolved(QQmlMetaType::normalizedUrl(url))
2167 : QQmlMetaType::normalizedUrl(url);
2168 if (!resolved.path().endsWith(QLatin1String(".mjs")))
2169 resolved.setFragment(QLatin1String("module"));
2170
2171 // Executable compilation unit already present in engine
2172 auto existingModule = m_compilationUnits.constFind(resolved);
2173 if (existingModule != m_compilationUnits.constEnd())
2174 return *existingModule;
2175
2176 // Also try with the relative url, to support native modules.
2177 if (resolved != url) {
2178 existingModule = m_compilationUnits.constFind(url);
2179 if (existingModule != m_compilationUnits.constEnd())
2180 return *existingModule;
2181 }
2182
2183 if (const auto blob = m_typeLoader->getScript(resolved, QQmlTypeLoader::Synchronous))
2184 return executableCompilationUnit(blob->scriptData()->compilationUnit());
2185
2186 // Unavailable
2187 return Module();
2188}
2189
2190ExecutionEngine::Module ExecutionEngine::registerNativeModule(
2191 const QUrl &url, const QV4::Value &value)
2192{
2193 Q_ASSERT(!m_compilationUnits.contains(url));
2194
2195 const QV4::CompiledData::Unit *unit = Compiler::Codegen::generateNativeModuleUnitData(
2196 /*debugMode*/debugger() != nullptr, url.toString(), value);
2197 if (!unit)
2198 return Module();
2199
2200 QQmlRefPointer<CompiledData::CompilationUnit> cu;
2201
2202 // Make sure the type loader doesn't try to resolve the module anymore.
2203 // If some other code requests that same module, we need to produce the same CU.
2204 cu = typeLoader()->injectModule(url, unit);
2205
2206 QQmlRefPointer<ExecutableCompilationUnit> newModule = insertCompilationUnit(std::move(cu));
2207
2208 Q_ASSERT(m_compilationUnits.contains(url, newModule));
2209
2210 Scope scope(this);
2211 Scoped<QV4::Module> instance(scope, newModule->instantiate());
2212 Scoped<CallContext> context(scope, instance->d()->scope);
2213
2214 const CompiledData::Unit *unitData = newModule->baseCompilationUnit()->data;
2215 const CompiledData::ExportEntry *exportEntries = unitData->localExportEntryTable();
2216
2217 ScopedObject object(scope, value);
2218 for (uint i = 0, end = unitData->localExportEntryTableSize; i < end; ++i) {
2219 const CompiledData::ExportEntry *localExport = exportEntries + i;
2220 ScopedString localName(scope, newModule->runtimeStrings[localExport->localName]);
2221 const uint index
2222 = context->internalClass()->indexOfValueOrGetter(localName->toPropertyKey());
2223 QV4::Heap::CallContext *cc = context->d();
2224 Q_ASSERT(index < cc->locals.size);
2225 if (localName->toQString() == QLatin1String("default")) {
2226 cc->locals.set(this, index, value);
2227 continue;
2228 }
2229
2230 Q_ASSERT(object);
2231
2232 ScopedValue localValue(scope, object->get(localName));
2233 cc->locals.set(this, index, localValue);
2234 }
2235
2236 instance->d()->evaluated = true;
2237 return newModule;
2238}
2239
2241{
2242 using DiskCache = ExecutionEngine::DiskCache;
2243
2244 if (v == nullptr)
2245 return DiskCache::Enabled;
2246
2247 ExecutionEngine::DiskCacheOptions result = DiskCache::Disabled;
2248 const QList<QByteArray> options = QByteArray(v).split(',');
2249 for (const QByteArray &option : options) {
2250 if (option == "aot-bytecode")
2251 result |= DiskCache::AotByteCode;
2252 else if (option == "aot-native")
2253 result |= DiskCache::AotNative;
2254 else if (option == "aot")
2255 result |= DiskCache::Aot;
2256 else if (option == "qmlc-read")
2257 result |= DiskCache::QmlcRead;
2258 else if (option == "qmlc-write")
2259 result |= DiskCache::QmlcWrite;
2260 else if (option == "qmlc")
2261 result |= DiskCache::Qmlc;
2262 else
2263 qWarning() << "Ignoring unknown option to QML_DISK_CACHE:" << option;
2264 }
2265
2266 return result;
2267}
2268
2269ExecutionEngine::DiskCacheOptions ExecutionEngine::diskCacheOptions() const
2270{
2271 if (forceDiskCache())
2272 return DiskCache::Enabled;
2273 if (disableDiskCache() || debugger())
2274 return DiskCache::Disabled;
2275 static const DiskCacheOptions options = qmlGetConfigOption<
2276 DiskCacheOptions, transFormDiskCache>("QML_DISK_CACHE");
2277 return hasPreview.loadAcquire()
2278 ? (options & ~DiskCacheOptions(DiskCache::Aot)) // Disable AOT if preview enabled
2279 : options;
2280}
2281
2282void ExecutionEngine::callInContext(QV4::Function *function, QObject *self,
2283 QV4::ExecutionContext *context, int argc, void **args,
2284 QMetaType *types)
2285{
2286 if (!args) {
2287 Q_ASSERT(argc == 0);
2288 void *dummyArgs[] = { nullptr };
2289 QMetaType dummyTypes[] = { QMetaType::fromType<void>() };
2290 function->call(self, dummyArgs, dummyTypes, argc, context);
2291 return;
2292 }
2293 Q_ASSERT(types); // both args and types must be present
2294 // implicitly sets the return value, which is args[0]
2295 function->call(self, args, types, argc, context);
2296}
2297
2298QV4::ReturnedValue ExecutionEngine::callInContext(QV4::Function *function, QObject *self,
2299 QV4::ExecutionContext *context, int argc,
2300 const QV4::Value *argv)
2301{
2302 QV4::Scope scope(this);
2303 QV4::ScopedObject jsSelf(scope, QV4::QObjectWrapper::wrap(this, self));
2304 Q_ASSERT(jsSelf);
2305 return function->call(jsSelf, argv, argc, context);
2306}
2307
2308void ExecutionEngine::initQmlGlobalObject()
2309{
2310 initializeGlobal();
2311 lockObject(*globalObject);
2312}
2313
2314static bool globalNamesAreStaticallyKnown(QV4::Object *globalObject)
2315{
2316 const Heap::InternalClass *ic = globalObject->internalClass();
2317 const SharedInternalClassData<PropertyKey> &nameMap = ic->nameMap;
2318 bool clean = true;
2319 for (uint i = 0, end = ic->size; i < end; ++i) {
2320 const QV4::PropertyKey id = nameMap.at(i);
2321 if (id.isString()) {
2322 if (!Compiler::Codegen::isNameGlobal(id.toQString())) {
2323 qCritical() << id.toQString()
2324 << "is part of the JavaScript global object "
2325 "but not statically known to be global";
2326 clean = false;
2327 }
2328 }
2329 }
2330 return clean;
2331}
2332
2333#if QT_CONFIG(qml_xml_http_request)
2334void ExecutionEngine::setupXmlHttpRequestExtension()
2335{
2336 qt_add_domexceptions(this);
2337 m_xmlHttpRequestData = qt_add_qmlxmlhttprequest(this);
2338}
2339#endif
2340
2341void ExecutionEngine::initializeGlobal()
2342{
2343 createQtObject();
2344
2345 QV4::GlobalExtensions::init(globalObject, QJSEngine::AllExtensions);
2346
2347#if QT_CONFIG(qml_locale)
2348 QQmlLocale::registerStringLocaleCompare(this);
2349 QQmlDateExtension::registerExtension(this);
2350 QQmlNumberExtension::registerExtension(this);
2351#endif
2352
2353#if QT_CONFIG(qml_xml_http_request)
2354 setupXmlHttpRequestExtension();
2355#endif
2356
2357 qt_add_sqlexceptions(this);
2358
2359 Q_ASSERT(globalNamesAreStaticallyKnown(globalObject));
2360}
2361
2362void ExecutionEngine::createQtObject()
2363{
2364 QV4::Scope scope(this);
2365 QtObject *qtObject = new QtObject(this);
2366
2367 if (publicEngine) {
2368 // If we have a public engine, parent to that one so that we don't delete
2369 // the instance on QQmlEngine::clearSingletons() we still need it as property
2370 // of the global object after all.
2371 QJSEngine::setObjectOwnership(qtObject, QJSEngine::CppOwnership);
2372 qtObject->setParent(publicEngine);
2373 } else {
2374 // If we don't have a public engine, the object isn't exposed as a singleton.
2375 // We can use JavaScript ownership to have it deleted by the last GC run.
2376 QJSEngine::setObjectOwnership(qtObject, QJSEngine::JavaScriptOwnership);
2377 }
2378
2379 QV4::ScopedObject qtObjectWrapper(
2380 scope, QV4::QObjectWrapper::wrap(this, qtObject));
2381 QV4::ScopedObject qtNamespaceWrapper(
2382 scope, QV4::QMetaObjectWrapper::create(this, &Qt::staticMetaObject));
2383 QV4::ScopedObject qtObjectProtoWrapper(
2384 scope, qtObjectWrapper->getPrototypeOf());
2385
2386 qtNamespaceWrapper->setPrototypeOf(qtObjectProtoWrapper);
2387 qtObjectWrapper->setPrototypeOf(qtNamespaceWrapper);
2388
2389 globalObject->defineDefaultProperty(QStringLiteral("Qt"), qtObjectWrapper);
2390}
2391
2392void ExecutionEngine::setQmlEngine(QQmlEngine *engine)
2393{
2394 // Second stage of initialization. We're updating some more prototypes here.
2395 isInitialized = false;
2396 m_qmlEngine = engine;
2397 initQmlGlobalObject();
2398 isInitialized = true;
2399}
2400
2401static void freeze_recursive(QV4::ExecutionEngine *v4, QV4::Object *object)
2402{
2403 if (object->as<QV4::QObjectWrapper>() || object->internalClass()->isFrozen())
2404 return;
2405
2406 QV4::Scope scope(v4);
2407
2408 bool instanceOfObject = false;
2409 QV4::ScopedObject p(scope, object->getPrototypeOf());
2410 while (p) {
2411 if (p->d() == v4->objectPrototype()->d()) {
2412 instanceOfObject = true;
2413 break;
2414 }
2415 p = p->getPrototypeOf();
2416 }
2417 if (!instanceOfObject)
2418 return;
2419
2420 Heap::InternalClass *frozen = object->internalClass()->frozen();
2421 object->setInternalClass(frozen); // Immediately assign frozen to prevent it from getting GC'd
2422
2423 QV4::ScopedObject o(scope);
2424 for (uint i = 0; i < frozen->size; ++i) {
2425 if (!frozen->nameMap.at(i).isStringOrSymbol())
2426 continue;
2427 o = *object->propertyData(i);
2428 if (o)
2429 freeze_recursive(v4, o);
2430 }
2431}
2432
2433void ExecutionEngine::freezeObject(const QV4::Value &value)
2434{
2435 QV4::Scope scope(this);
2436 QV4::ScopedObject o(scope, value);
2437 freeze_recursive(this, o);
2438}
2439
2440void ExecutionEngine::lockObject(const QV4::Value &value)
2441{
2442 QV4::Scope scope(this);
2443 ScopedObject object(scope, value);
2444 if (!object)
2445 return;
2446
2447 std::vector<Heap::Object *> stack { object->d() };
2448
2449 // Methods meant to be overridden
2450 const PropertyKey writableMembers[] = {
2451 id_toString()->propertyKey(),
2452 id_toLocaleString()->propertyKey(),
2453 id_valueOf()->propertyKey(),
2454 id_constructor()->propertyKey()
2455 };
2456 const auto writableBegin = std::begin(writableMembers);
2457 const auto writableEnd = std::end(writableMembers);
2458
2459 while (!stack.empty()) {
2460 object = stack.back();
2461 stack.pop_back();
2462
2463 if (object->as<QV4::QObjectWrapper>() || object->internalClass()->isLocked())
2464 continue;
2465
2466 Scoped<InternalClass> locked(scope, object->internalClass()->locked());
2467 QV4::ScopedObject member(scope);
2468
2469 // Taking this copy is cheap. It's refcounted. This avoids keeping a reference
2470 // to the original IC.
2471 const SharedInternalClassData<PropertyKey> nameMap = locked->d()->nameMap;
2472
2473 for (uint i = 0, end = locked->d()->size; i < end; ++i) {
2474 const PropertyKey key = nameMap.at(i);
2475 if (!key.isStringOrSymbol())
2476 continue;
2477 if ((member = *object->propertyData(i))) {
2478 stack.push_back(member->d());
2479 if (std::find(writableBegin, writableEnd, key) == writableEnd) {
2480 PropertyAttributes attributes = locked->d()->find(key).attributes;
2481 attributes.setConfigurable(false);
2482 attributes.setWritable(false);
2483 locked = locked->changeMember(key, attributes);
2484 }
2485 }
2486 }
2487
2488 object->setInternalClass(locked->d());
2489 }
2490}
2491
2492void ExecutionEngine::startTimer(const QString &timerName)
2493{
2494 if (!m_time.isValid())
2495 m_time.start();
2496 m_startedTimers[timerName] = m_time.elapsed();
2497}
2498
2499qint64 ExecutionEngine::stopTimer(const QString &timerName, bool *wasRunning)
2500{
2501 if (!m_startedTimers.contains(timerName)) {
2502 *wasRunning = false;
2503 return 0;
2504 }
2505 *wasRunning = true;
2506 qint64 startedAt = m_startedTimers.take(timerName);
2507 return m_time.elapsed() - startedAt;
2508}
2509
2510int ExecutionEngine::consoleCountHelper(const QString &file, quint16 line, quint16 column)
2511{
2512 const QString key = file + QString::number(line) + QString::number(column);
2513 int number = m_consoleCount.value(key, 0);
2514 number++;
2515 m_consoleCount.insert(key, number);
2516 return number;
2517}
2518
2519void ExecutionEngine::setExtensionData(int index, Deletable *data)
2520{
2521 if (m_extensionData.size() <= index)
2522 m_extensionData.resize(index + 1);
2523
2524 if (m_extensionData.at(index))
2525 delete m_extensionData.at(index);
2526
2527 m_extensionData[index] = data;
2528}
2529
2530template<typename Source>
2531bool convertToIterable(QMetaType metaType, void *data, Source *sequence)
2532{
2533 QMetaSequence::Iterable iterable;
2534 if (!QMetaType::view(metaType, data, QMetaType::fromType<QMetaSequence::Iterable>(), &iterable))
2535 return false;
2536
2537 // Clear the sequence before appending. There may be stale data in there.
2538 metaType.destruct(data);
2539 metaType.construct(data);
2540
2541 QV4::Scope scope(sequence->engine());
2542 QV4::ScopedValue v(scope);
2543
2544 const QMetaType elementMetaType = iterable.metaContainer().valueMetaType();
2545 QVariant element;
2546 void *elementData = nullptr;
2547 if (elementMetaType == QMetaType::fromType<QVariant>()) {
2548 elementData = &element;
2549 } else {
2550 element = QVariant(elementMetaType);
2551 elementData = element.data();
2552 }
2553
2554 for (qsizetype i = 0, end = sequence->getLength(); i < end; ++i) {
2555 v = sequence->get(i);
2556 ExecutionEngine::metaTypeFromJS(v, elementMetaType, elementData);
2557 iterable.append(element);
2558 }
2559
2560 return true;
2561}
2562
2563/*!
2564 * \internal
2565 *
2566 * Converts a JS value to a meta-type.
2567 * \a data must point to a default-constructed instance of \a metaType.
2568 * Returns \c true if conversion succeeded, \c false otherwise. In the latter case,
2569 * \a data is not modified.
2570 */
2571bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, void *data)
2572{
2573 // check if it's one of the types we know
2574 switch (metaType.id()) {
2575 case QMetaType::Bool:
2576 *reinterpret_cast<bool*>(data) = value.toBoolean();
2577 return true;
2578 case QMetaType::Int:
2579 *reinterpret_cast<int*>(data) = value.toInt32();
2580 return true;
2581 case QMetaType::UInt:
2582 *reinterpret_cast<uint*>(data) = value.toUInt32();
2583 return true;
2584 case QMetaType::Long:
2585 *reinterpret_cast<long*>(data) = long(value.toInteger());
2586 return true;
2587 case QMetaType::ULong:
2588 *reinterpret_cast<ulong*>(data) = ulong(value.toInteger());
2589 return true;
2590 case QMetaType::LongLong:
2591 *reinterpret_cast<qlonglong*>(data) = qlonglong(value.toInteger());
2592 return true;
2593 case QMetaType::ULongLong:
2594 *reinterpret_cast<qulonglong*>(data) = qulonglong(value.toInteger());
2595 return true;
2596 case QMetaType::Double:
2597 *reinterpret_cast<double*>(data) = value.toNumber();
2598 return true;
2599 case QMetaType::QString:
2600 if (value.isUndefined())
2601 *reinterpret_cast<QString*>(data) = QStringLiteral("undefined");
2602 else if (value.isNull())
2603 *reinterpret_cast<QString*>(data) = QStringLiteral("null");
2604 else
2605 *reinterpret_cast<QString*>(data) = value.toQString();
2606 return true;
2607 case QMetaType::QByteArray:
2608 if (const ArrayBuffer *ab = value.as<ArrayBuffer>()) {
2609 *reinterpret_cast<QByteArray*>(data) = ab->asByteArray();
2610 } else if (const String *string = value.as<String>()) {
2611 *reinterpret_cast<QByteArray*>(data) = string->toQString().toUtf8();
2612 } else if (const ArrayObject *ao = value.as<ArrayObject>()) {
2613 // Since QByteArray is sequentially iterable, we have to construct it from a JS Array.
2614 QByteArray result;
2615 const qint64 length = ao->getLength();
2616 result.reserve(length);
2617 QV4::Scope scope(ao->engine());
2618 QV4::ScopedValue v(scope);
2619 for (qint64 i = 0; i < length; ++i) {
2620 char value = 0;
2621 v = ao->get(i);
2622 ExecutionEngine::metaTypeFromJS(v, QMetaType::fromType<char>(), &value);
2623 result.push_back(value);
2624 }
2625 *reinterpret_cast<QByteArray*>(data) = std::move(result);
2626 } else {
2627 *reinterpret_cast<QByteArray*>(data) = QByteArray();
2628 }
2629 return true;
2630 case QMetaType::Float:
2631 *reinterpret_cast<float*>(data) = value.toNumber();
2632 return true;
2633 case QMetaType::Short:
2634 *reinterpret_cast<short*>(data) = short(value.toInt32());
2635 return true;
2636 case QMetaType::UShort:
2637 *reinterpret_cast<unsigned short*>(data) = value.toUInt16();
2638 return true;
2639 case QMetaType::Char:
2640 *reinterpret_cast<char*>(data) = char(value.toInt32());
2641 return true;
2642 case QMetaType::UChar:
2643 *reinterpret_cast<unsigned char*>(data) = (unsigned char)(value.toInt32());
2644 return true;
2645 case QMetaType::SChar:
2646 *reinterpret_cast<signed char*>(data) = (signed char)(value.toInt32());
2647 return true;
2648 case QMetaType::QChar:
2649 if (String *s = value.stringValue()) {
2650 QString str = s->toQString();
2651 *reinterpret_cast<QChar*>(data) = str.isEmpty() ? QChar() : str.at(0);
2652 } else {
2653 *reinterpret_cast<QChar*>(data) = QChar(ushort(value.toUInt16()));
2654 }
2655 return true;
2656 case QMetaType::QDateTime:
2657 if (const QV4::DateObject *d = value.as<DateObject>()) {
2658 *reinterpret_cast<QDateTime *>(data) = d->toQDateTime();
2659 return true;
2660 } break;
2661 case QMetaType::QDate:
2662 if (const QV4::DateObject *d = value.as<DateObject>()) {
2663 *reinterpret_cast<QDate *>(data) = DateObject::dateTimeToDate(d->toQDateTime());
2664 return true;
2665 } break;
2666 case QMetaType::QTime:
2667 if (const QV4::DateObject *d = value.as<DateObject>()) {
2668 *reinterpret_cast<QTime *>(data) = d->toQDateTime().time();
2669 return true;
2670 } break;
2671 case QMetaType::QUrl:
2672 if (String *s = value.stringValue()) {
2673 *reinterpret_cast<QUrl *>(data) = QUrl(s->toQString());
2674 return true;
2675 } else if (const QV4::UrlObject *d = value.as<UrlObject>()) {
2676 *reinterpret_cast<QUrl *>(data) = d->toQUrl();
2677 return true;
2678 } else if (const QV4::VariantObject *d = value.as<VariantObject>()) {
2679 const QVariant *variant = &d->d()->data();
2680 if (variant->metaType() == QMetaType::fromType<QUrl>()) {
2681 *reinterpret_cast<QUrl *>(data)
2682 = *reinterpret_cast<const QUrl *>(variant->constData());
2683 return true;
2684 }
2685 }
2686 break;
2687#if QT_CONFIG(regularexpression)
2688 case QMetaType::QRegularExpression:
2689 if (const QV4::RegExpObject *r = value.as<QV4::RegExpObject>()) {
2690 *reinterpret_cast<QRegularExpression *>(data) = r->toQRegularExpression();
2691 return true;
2692 } break;
2693#endif
2694 case QMetaType::QObjectStar: {
2695 if (value.isNull()) {
2696 *reinterpret_cast<QObject* *>(data) = nullptr;
2697 return true;
2698 }
2699 if (value.as<QV4::QObjectWrapper>()) {
2700 *reinterpret_cast<QObject* *>(data) = qtObjectFromJS(value);
2701 return true;
2702 }
2703 break;
2704 }
2705 case QMetaType::QStringList: {
2706 const QV4::ArrayObject *a = value.as<QV4::ArrayObject>();
2707 if (a) {
2708 *reinterpret_cast<QStringList *>(data) = a->toQStringList();
2709 return true;
2710 }
2711 break;
2712 }
2713 case QMetaType::QVariantList: {
2714 const QV4::ArrayObject *a = value.as<QV4::ArrayObject>();
2715 if (a) {
2716 *reinterpret_cast<QVariantList *>(data) = ExecutionEngine::toVariant(
2717 *a, /*typeHint*/QMetaType{}, /*createJSValueForObjectsAndSymbols*/false)
2718 .toList();
2719 return true;
2720 }
2721 break;
2722 }
2723 case QMetaType::QVariantMap: {
2724 const QV4::Object *o = value.as<QV4::Object>();
2725 if (o) {
2726 *reinterpret_cast<QVariantMap *>(data) = o->engine()->variantMapFromJS(o);
2727 return true;
2728 }
2729 break;
2730 }
2731 case QMetaType::QVariant:
2732 if (value.as<QV4::Managed>()) {
2733 *reinterpret_cast<QVariant*>(data) = ExecutionEngine::toVariant(
2734 value, /*typeHint*/QMetaType{}, /*createJSValueForObjectsAndSymbols*/false);
2735 } else if (value.isNull()) {
2736 *reinterpret_cast<QVariant*>(data) = QVariant::fromValue(nullptr);
2737 } else if (value.isUndefined()) {
2738 *reinterpret_cast<QVariant*>(data) = QVariant();
2739 } else if (value.isBoolean()) {
2740 *reinterpret_cast<QVariant*>(data) = QVariant(value.booleanValue());
2741 } else if (value.isInteger()) {
2742 *reinterpret_cast<QVariant*>(data) = QVariant(value.integerValue());
2743 } else if (value.isDouble()) {
2744 *reinterpret_cast<QVariant*>(data) = QVariant(value.doubleValue());
2745 }
2746 return true;
2747 case QMetaType::QJsonValue:
2748 *reinterpret_cast<QJsonValue *>(data) = QV4::JsonObject::toJsonValue(value);
2749 return true;
2750 case QMetaType::QJsonObject: {
2751 *reinterpret_cast<QJsonObject *>(data) = QV4::JsonObject::toJsonObject(value.as<Object>());
2752 return true;
2753 }
2754 case QMetaType::QJsonArray: {
2755 *reinterpret_cast<QJsonArray *>(data) = QV4::JsonObject::toJsonArray(value.as<Object>());
2756 return true;
2757 }
2758 default:
2759 break;
2760 }
2761
2762 if (metaType.flags() & QMetaType::IsEnumeration) {
2763 *reinterpret_cast<int *>(data) = value.toInt32();
2764 return true;
2765 }
2766
2767 if (const QV4::QmlListWrapper *wrapper = value.as<QV4::QmlListWrapper>()) {
2768 if (metaType == QMetaType::fromType<QQmlListReference>()) {
2769 *reinterpret_cast<QQmlListReference *>(data) = wrapper->toListReference();
2770 return true;
2771 }
2772
2773 const auto wrapperPrivate = wrapper->d();
2774 if (metaType == QMetaType::fromType<QQmlListProperty<QObject> *>()
2775 || metaType == wrapperPrivate->propertyType()) {
2776 *reinterpret_cast<QQmlListProperty<QObject> *>(data) = *wrapperPrivate->property();
2777 return true;
2778 }
2779
2780 if (metaType == QMetaType::fromType<QObjectList>()) {
2781 *reinterpret_cast<QObjectList *>(data)
2782 = wrapperPrivate->property()->toList<QObjectList>();
2783 return true;
2784 }
2785
2786 if (convertToIterable(metaType, data, wrapper))
2787 return true;
2788 }
2789
2790 if (const QQmlValueTypeWrapper *vtw = value.as<QQmlValueTypeWrapper>()) {
2791 const QMetaType valueType = vtw->type();
2792 if (valueType == metaType)
2793 return vtw->toGadget(data);
2794
2795 Heap::QQmlValueTypeWrapper *d = vtw->d();
2796 if (d->isReference())
2797 d->readReference();
2798
2799 if (void *gadgetPtr = d->gadgetPtr()) {
2800 if (QQmlValueTypeProvider::populateValueType(
2801 metaType, data, valueType, gadgetPtr, vtw->engine())) {
2802 return true;
2803 }
2804 if (QMetaType::canConvert(valueType, metaType))
2805 return QMetaType::convert(valueType, gadgetPtr, metaType, data);
2806 } else {
2807 QVariant empty(valueType);
2808 if (QQmlValueTypeProvider::populateValueType(
2809 metaType, data, valueType, empty.data(), vtw->engine())) {
2810 return true;
2811 }
2812 if (QMetaType::canConvert(valueType, metaType))
2813 return QMetaType::convert(valueType, empty.data(), metaType, data);
2814 }
2815 }
2816
2817 // Try to use magic; for compatibility with qjsvalue_cast.
2818
2819 if (convertToNativeQObject(value, metaType, reinterpret_cast<void **>(data)))
2820 return true;
2821
2822 const bool isPointer = (metaType.flags() & QMetaType::IsPointer);
2823 const QV4::VariantObject *variantObject = value.as<QV4::VariantObject>();
2824 if (variantObject) {
2825 // Actually a reference, because we're poking it for its data() below and we want
2826 // the _original_ data, not some copy.
2827 QVariant &var = variantObject->d()->data();
2828
2829 if (var.metaType() == metaType) {
2830 metaType.destruct(data);
2831 metaType.construct(data, var.data());
2832 return true;
2833 }
2834
2835 if (isPointer) {
2836 const QByteArray pointedToTypeName = QByteArray(metaType.name()).chopped(1);
2837 const QMetaType valueType = QMetaType::fromName(pointedToTypeName);
2838
2839 if (valueType == var.metaType()) {
2840 // ### Qt7: Remove this. Returning pointers to potentially gc'd data is crazy.
2841 // We have T t, T* is requested, so return &t.
2842 *reinterpret_cast<const void **>(data) = var.data();
2843 return true;
2844 } else if (Object *o = value.objectValue()) {
2845 // Look in the prototype chain.
2846 QV4::Scope scope(o->engine());
2847 QV4::ScopedObject proto(scope, o->getPrototypeOf());
2848 while (proto) {
2849 bool canCast = false;
2850 if (QV4::VariantObject *vo = proto->as<QV4::VariantObject>()) {
2851 const QVariant &v = vo->d()->data();
2852 canCast = (metaType == v.metaType());
2853 }
2854 else if (proto->as<QV4::QObjectWrapper>()) {
2855 QV4::ScopedObject p(scope, proto.getPointer());
2856 if (QObject *qobject = qtObjectFromJS(p)) {
2857 if (const QMetaObject *metaObject = metaType.metaObject())
2858 canCast = metaObject->cast(qobject) != nullptr;
2859 else
2860 canCast = qobject->qt_metacast(pointedToTypeName);
2861 }
2862 }
2863 if (canCast) {
2864 const QMetaType varType = var.metaType();
2865 if (varType.flags() & QMetaType::IsPointer) {
2866 *reinterpret_cast<const void **>(data)
2867 = *reinterpret_cast<void *const *>(var.data());
2868 } else {
2869 *reinterpret_cast<const void **>(data) = var.data();
2870 }
2871 return true;
2872 }
2873 proto = proto->getPrototypeOf();
2874 }
2875 }
2876 } else if (QQmlValueTypeProvider::populateValueType(
2877 metaType, data, var.metaType(), var.data(), variantObject->engine())) {
2878 return true;
2879 }
2880 } else if (value.isNull() && isPointer) {
2881 *reinterpret_cast<void* *>(data) = nullptr;
2882 return true;
2883 } else if (metaType == QMetaType::fromType<QJSValue>()) {
2884 QJSValuePrivate::setValue(reinterpret_cast<QJSValue*>(data), value);
2885 return true;
2886 } else if (metaType == QMetaType::fromType<QJSPrimitiveValue>()) {
2887 *reinterpret_cast<QJSPrimitiveValue *>(data) = createPrimitive(&value);
2888 return true;
2889 } else if (metaType == QMetaType::fromType<QJSManagedValue>()) {
2890 QJSManagedValue *managedData = reinterpret_cast<QJSManagedValue *>(data);
2891
2892 const Managed *managedValue = value.as<Managed>();
2893 if (Value *d = QJSManagedValuePrivate::member(managedData)) {
2894 // If the QJSManagedValue has an engine already,
2895 // we can assign a primitive without risking crosstalk between engines.
2896 if (!managedValue) {
2897 *d = value;
2898 return true;
2899 }
2900
2901 // If the engines match, we don't have to allocate a new persistent value
2902 if (reinterpret_cast<const Managed *>(d)->engine() == managedValue->engine()) {
2903 *d = value;
2904 return true;
2905 }
2906 }
2907
2908 if (managedValue) {
2909 // If value is itself managed, we create a new QJSManagedValue
2910 // to make sure the engine matches.
2911 *managedData = QJSManagedValuePrivate::create(*managedValue);
2912 return true;
2913 }
2914
2915 // The default-constructed QJSManagedValue (without engine) equals undefined.
2916 // So we can get away with doing nothing in this case.
2917 if (value.isUndefined())
2918 return true;
2919
2920 // Otherwise we can't do it. We need an engine from somewhere.
2921 return false;
2922 } else if (!isPointer) {
2923 const QV4::Managed *managed = value.as<QV4::Managed>();
2924 if (QQmlValueTypeProvider::populateValueType(
2925 metaType, data, value, managed ? managed->engine() : nullptr)) {
2926 return true;
2927 }
2928 }
2929
2930 if (const QV4::Sequence *sequence = value.as<Sequence>()) {
2931 const QVariant result = QV4::SequencePrototype::toVariant(sequence);
2932 if (result.metaType() == metaType) {
2933 metaType.destruct(data);
2934 metaType.construct(data, result.constData());
2935 return true;
2936 }
2937
2938 if (convertToIterable(metaType, data, sequence))
2939 return true;
2940 }
2941
2942 if (const QV4::ArrayObject *array = value.as<ArrayObject>()) {
2943 if (convertToIterable(metaType, data, array))
2944 return true;
2945 }
2946
2947 return false;
2948}
2949
2950static bool convertToNativeQObject(const QV4::Value &value, QMetaType targetType, void **result)
2951{
2952 if (!(targetType.flags() & QMetaType::IsPointer))
2953 return false;
2954 if (QObject *qobject = qtObjectFromJS(value)) {
2955 // If the target type has a metaObject, use that for casting.
2956 if (const QMetaObject *targetMetaObject = targetType.metaObject()) {
2957 if (QObject *instance = targetMetaObject->cast(qobject)) {
2958 *result = instance;
2959 return true;
2960 }
2961 return false;
2962 }
2963
2964 // We have to call the generated qt_metacast rather than metaObject->cast() here so that
2965 // it works for types without QMetaObject, such as QStandardItem.
2966 const QByteArray targetTypeName = targetType.name();
2967 const int start = targetTypeName.startsWith("const ") ? 6 : 0;
2968 const QByteArray className = targetTypeName.mid(start, targetTypeName.size() - start - 1);
2969 if (void *instance = qobject->qt_metacast(className)) {
2970 *result = instance;
2971 return true;
2972 }
2973 }
2974 return false;
2975}
2976
2977static QObject *qtObjectFromJS(const QV4::Value &value)
2978{
2979 if (!value.isObject())
2980 return nullptr;
2981
2982 QV4::Scope scope(value.as<QV4::Managed>()->engine());
2983 QV4::Scoped<QV4::VariantObject> v(scope, value);
2984
2985 if (v) {
2986 QVariant variant = v->d()->data();
2987 int type = variant.userType();
2988 if (type == QMetaType::QObjectStar)
2989 return *reinterpret_cast<QObject* const *>(variant.constData());
2990 }
2991 QV4::Scoped<QV4::QObjectWrapper> wrapper(scope, value);
2992 if (wrapper)
2993 return wrapper->object();
2994
2995 QV4::Scoped<QV4::QQmlTypeWrapper> typeWrapper(scope, value);
2996 if (typeWrapper)
2997 return typeWrapper->object();
2998
2999 return nullptr;
3000}
3001
3010
3011QMutex *ExecutionEngine::registrationMutex()
3012{
3013 return &registrationData()->mutex;
3014}
3015
3016int ExecutionEngine::registerExtension()
3017{
3018 return registrationData()->extensionCount++;
3019}
3020
3021QT_END_NAMESPACE
Combined button and popup list for selecting options.
Definition qjsvalue.h:24
static char * v4StackTrace(const ExecutionContext *context)
static bool globalNamesAreStaticallyKnown(QV4::Object *globalObject)
static QObject * qtObjectFromJS(const QV4::Value &value)
static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects=nullptr, JSToQVariantConversionBehavior behavior=JSToQVariantConversionBehavior::Safish)
Q_QML_EXPORT char * qt_v4StackTraceForEngine(void *executionEngine)
DEFINE_BOOL_CONFIG_OPTION(forceDiskCache, QML_FORCE_DISK_CACHE)
static void freeze_recursive(QV4::ExecutionEngine *v4, QV4::Object *object)
ReturnType convertJSValueToVariantType(const QJSValue &value)
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, JSToQVariantConversionBehavior conversionBehavior, V4ObjectSet *visitedObjects)
JSToQVariantConversionBehavior
static QtMetaContainerPrivate::QMetaSequenceInterface emptySequenceInterface()
static ExecutionEngine::DiskCacheOptions transFormDiskCache(const char *v)
Q_GLOBAL_STATIC(QV4EngineRegistrationData, registrationData)
static QtMetaContainerPrivate::QMetaSequenceInterface sequenceInterface()
Q_QML_EXPORT char * qt_v4StackTrace(void *executionContext)
static bool convertToNativeQObject(const QV4::Value &value, QMetaType targetType, void **result)
Association objectToVariantAssociation(const QV4::Object *o, V4ObjectSet *visitedObjects, JSToQVariantConversionBehavior conversionBehvior)
static QMetaSequence::Iterable jsvalueToSequence(const QJSValue &value)
bool convertToIterable(QMetaType metaType, void *data, Source *sequence)
QSet< QV4::Heap::Object * > V4ObjectSet
ReturnedValue throwTypeError(const FunctionObject *b, const QV4::Value *, const QV4::Value *, int)
QJSValue const * data