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#if QT_CONFIG(qml_network)
883 delete networkAccessManager;
884#endif
885 m_typeLoader.reset();
886 qDeleteAll(m_extensionData);
887 delete m_multiplyWrappedQObjects;
888 m_multiplyWrappedQObjects = nullptr;
889 delete identifierTable;
890 delete memoryManager;
891
892 for (const auto &cu : std::as_const(m_compilationUnits)) {
893 Q_ASSERT(cu->engine == this);
894 cu->clear();
895 cu->engine = nullptr;
896 }
897 m_compilationUnits.clear();
898
899 delete bumperPointerAllocator;
900 delete regExpCache;
901 delete regExpAllocator;
902 delete executableAllocator;
903 jsStack->deallocate();
904 delete jsStack;
905 gcStack->deallocate();
906 delete gcStack;
907
908#if QT_CONFIG(qml_xml_http_request)
909 qt_rem_qmlxmlhttprequest(this, m_xmlHttpRequestData);
910 m_xmlHttpRequestData = nullptr;
911#endif
912
913 QQmlMetaType::freeUnusedTypesAndCaches();
914}
915
916#if QT_CONFIG(qml_network)
917QNetworkAccessManager *ExecutionEngine::getNetworkAccessManager()
918{
919 if (!networkAccessManager)
920 networkAccessManager = typeLoader()->createNetworkAccessManager(nullptr);
921 return networkAccessManager;
922}
923#endif
924
925#if QT_CONFIG(qml_debug)
926void ExecutionEngine::setDebugger(Debugging::Debugger *debugger)
927{
928 Q_ASSERT(!m_debugger);
929 m_debugger.reset(debugger);
930}
931
932void ExecutionEngine::setProfiler(Profiling::Profiler *profiler)
933{
934 Q_ASSERT(!m_profiler);
935 m_profiler.reset(profiler);
936}
937
938void ExecutionEngine::setPreviewing(bool enabled)
939{
940 hasPreview.storeRelease(enabled);
941}
942
943#endif // QT_CONFIG(qml_debug)
944
945void ExecutionEngine::initRootContext()
946{
947 Scope scope(this);
948 Scoped<ExecutionContext> r(scope, memoryManager->allocManaged<ExecutionContext>());
949 r->d_unchecked()->init(Heap::ExecutionContext::Type_GlobalContext);
950 r->d()->activation.set(this, globalObject->d());
951 jsObjects[RootContext] = r;
952 jsObjects[ScriptContext] = r;
953 jsObjects[IntegerNull] = Encode((int)0);
954}
955
956Heap::InternalClass *ExecutionEngine::newClass(Heap::InternalClass *other)
957{
958 Heap::InternalClass *ic = memoryManager->allocIC<InternalClass>();
959 ic->init(other);
960 return ic;
961}
962
963Heap::InternalClass *ExecutionEngine::newInternalClass(const VTable *vtable, Object *prototype)
964{
965 Scope scope(this);
966 Scoped<InternalClass> ic(scope, internalClasses(Class_Empty)->changeVTable(vtable));
967 return ic->changePrototype(prototype ? prototype->d() : nullptr);
968}
969
970Heap::Object *ExecutionEngine::newObject()
971{
972 return memoryManager->allocate<Object>();
973}
974
975Heap::Object *ExecutionEngine::newObject(Heap::InternalClass *internalClass)
976{
977 return memoryManager->allocObject<Object>(internalClass);
978}
979
980Heap::String *ExecutionEngine::newString(const QString &s)
981{
982 return memoryManager->allocWithStringData<String>(s.size() * sizeof(QChar), s);
983}
984
985Heap::String *ExecutionEngine::newIdentifier(const QString &text)
986{
987 Scope scope(this);
988 ScopedString s(scope, memoryManager->allocWithStringData<String>(text.size() * sizeof(QChar), text));
989 s->toPropertyKey();
990 return s->d();
991}
992
993Heap::Object *ExecutionEngine::newStringObject(const String *string)
994{
995 return memoryManager->allocate<StringObject>(string);
996}
997
998Heap::Object *ExecutionEngine::newSymbolObject(const Symbol *symbol)
999{
1000 return memoryManager->allocObject<SymbolObject>(classes[Class_SymbolObject], symbol);
1001}
1002
1003Heap::Object *ExecutionEngine::newNumberObject(double value)
1004{
1005 return memoryManager->allocate<NumberObject>(value);
1006}
1007
1008Heap::Object *ExecutionEngine::newBooleanObject(bool b)
1009{
1010 return memoryManager->allocate<BooleanObject>(b);
1011}
1012
1013Heap::ArrayObject *ExecutionEngine::newArrayObject(int count)
1014{
1015 Scope scope(this);
1016 ScopedArrayObject object(scope, memoryManager->allocate<ArrayObject>());
1017
1018 if (count) {
1019 if (count < 0x1000)
1020 object->arrayReserve(count);
1021 object->setArrayLengthUnchecked(count);
1022 }
1023 return object->d();
1024}
1025
1026Heap::ArrayObject *ExecutionEngine::newArrayObject(const Value *values, int length)
1027{
1028 Scope scope(this);
1029 ScopedArrayObject a(scope, memoryManager->allocate<ArrayObject>());
1030
1031 if (length) {
1032 size_t size = sizeof(Heap::ArrayData) + (length-1)*sizeof(Value);
1033 Heap::SimpleArrayData *d = scope.engine->memoryManager->allocManaged<SimpleArrayData>(size);
1034 d->init();
1035 d->type = Heap::ArrayData::Simple;
1036 d->offset = 0;
1037 d->values.alloc = length;
1038 d->values.size = length;
1039 // this doesn't require a write barrier, things will be ok, when the new array data gets inserted into
1040 // the parent object
1041 memcpy(&d->values.values, values, length*sizeof(Value));
1042 a->d()->arrayData.set(this, d);
1043 a->setArrayLengthUnchecked(length);
1044 }
1045 return a->d();
1046}
1047
1048Heap::ArrayObject *ExecutionEngine::newArrayObject(const QStringList &list)
1049{
1050 return memoryManager->allocate<ArrayObject>(list);
1051}
1052
1053Heap::ArrayObject *ExecutionEngine::newArrayObject(Heap::InternalClass *internalClass)
1054{
1055 return memoryManager->allocObject<ArrayObject>(internalClass);
1056}
1057
1058Heap::ArrayBuffer *ExecutionEngine::newArrayBuffer(const QByteArray &array)
1059{
1060 return memoryManager->allocate<ArrayBuffer>(array);
1061}
1062
1063Heap::ArrayBuffer *ExecutionEngine::newArrayBuffer(size_t length)
1064{
1065 return memoryManager->allocate<ArrayBuffer>(length);
1066}
1067
1068Heap::DateObject *ExecutionEngine::newDateObject(double dateTime)
1069{
1070 return memoryManager->allocate<DateObject>(dateTime);
1071}
1072
1073Heap::DateObject *ExecutionEngine::newDateObject(const QDateTime &dateTime)
1074{
1075 return memoryManager->allocate<DateObject>(dateTime);
1076}
1077
1078Heap::DateObject *ExecutionEngine::newDateObject(
1079 QDate date, Heap::Object *parent, int index, uint flags)
1080{
1081 return memoryManager->allocate<DateObject>(
1082 date, parent, index, Heap::ReferenceObject::Flags(flags));
1083}
1084
1085Heap::DateObject *ExecutionEngine::newDateObject(
1086 QTime time, Heap::Object *parent, int index, uint flags)
1087{
1088 return memoryManager->allocate<DateObject>(
1089 time, parent, index, Heap::ReferenceObject::Flags(flags));
1090}
1091
1092Heap::DateObject *ExecutionEngine::newDateObject(
1093 QDateTime dateTime, Heap::Object *parent, int index, uint flags)
1094{
1095 return memoryManager->allocate<DateObject>(
1096 dateTime, parent, index, Heap::ReferenceObject::Flags(flags));
1097}
1098
1099Heap::RegExpObject *ExecutionEngine::newRegExpObject(const QString &pattern, int flags)
1100{
1101 Scope scope(this);
1102 Scoped<RegExp> re(scope, RegExp::create(this, pattern, static_cast<CompiledData::RegExp::Flags>(flags)));
1103 return newRegExpObject(re);
1104}
1105
1106Heap::RegExpObject *ExecutionEngine::newRegExpObject(RegExp *re)
1107{
1108 return memoryManager->allocate<RegExpObject>(re);
1109}
1110
1111#if QT_CONFIG(regularexpression)
1112Heap::RegExpObject *ExecutionEngine::newRegExpObject(const QRegularExpression &re)
1113{
1114 return memoryManager->allocate<RegExpObject>(re);
1115}
1116#endif
1117
1118Heap::UrlObject *ExecutionEngine::newUrlObject()
1119{
1120 return memoryManager->allocate<UrlObject>();
1121}
1122
1123Heap::UrlObject *ExecutionEngine::newUrlObject(const QUrl &url)
1124{
1125 Scope scope(this);
1126 Scoped<UrlObject> urlObject(scope, newUrlObject());
1127 urlObject->setUrl(url);
1128 return urlObject->d();
1129}
1130
1131Heap::UrlSearchParamsObject *ExecutionEngine::newUrlSearchParamsObject()
1132{
1133 return memoryManager->allocate<UrlSearchParamsObject>();
1134}
1135
1136Heap::Object *ExecutionEngine::newErrorObject(const Value &value)
1137{
1138 return ErrorObject::create<ErrorObject>(this, value, errorCtor());
1139}
1140
1141Heap::Object *ExecutionEngine::newErrorObject(const QString &message)
1142{
1143 return ErrorObject::create<ErrorObject>(this, message);
1144}
1145
1146Heap::Object *ExecutionEngine::newSyntaxErrorObject(const QString &message)
1147{
1148 return ErrorObject::create<SyntaxErrorObject>(this, message);
1149}
1150
1151Heap::Object *ExecutionEngine::newSyntaxErrorObject(const QString &message, const QString &fileName, int line, int column)
1152{
1153 return ErrorObject::create<SyntaxErrorObject>(this, message, fileName, line, column);
1154}
1155
1156
1157Heap::Object *ExecutionEngine::newReferenceErrorObject(const QString &message)
1158{
1159 return ErrorObject::create<ReferenceErrorObject>(this, message);
1160}
1161
1162Heap::Object *ExecutionEngine::newReferenceErrorObject(const QString &message, const QString &fileName, int line, int column)
1163{
1164 return ErrorObject::create<ReferenceErrorObject>(this, message, fileName, line, column);
1165}
1166
1167
1168Heap::Object *ExecutionEngine::newTypeErrorObject(const QString &message)
1169{
1170 return ErrorObject::create<TypeErrorObject>(this, message);
1171}
1172
1173Heap::Object *ExecutionEngine::newRangeErrorObject(const QString &message)
1174{
1175 return ErrorObject::create<RangeErrorObject>(this, message);
1176}
1177
1178Heap::Object *ExecutionEngine::newURIErrorObject(const Value &message)
1179{
1180 return ErrorObject::create<URIErrorObject>(this, message, uRIErrorCtor());
1181}
1182
1183Heap::PromiseObject *ExecutionEngine::newPromiseObject()
1184{
1185 if (!m_reactionHandler) {
1186 m_reactionHandler.reset(new Promise::ReactionHandler);
1187 }
1188
1189 Scope scope(this);
1190 Scoped<PromiseObject> object(scope, memoryManager->allocate<PromiseObject>(this));
1191 return object->d();
1192}
1193
1194Heap::Object *ExecutionEngine::newPromiseObject(const QV4::FunctionObject *thisObject, const QV4::PromiseCapability *capability)
1195{
1196 if (!m_reactionHandler) {
1197 m_reactionHandler.reset(new Promise::ReactionHandler);
1198 }
1199
1200 Scope scope(this);
1201 Scoped<CapabilitiesExecutorWrapper> executor(scope, memoryManager->allocate<CapabilitiesExecutorWrapper>());
1202 executor->d()->capabilities.set(this, capability->d());
1203 executor->insertMember(id_length(), Primitive::fromInt32(2), Attr_NotWritable|Attr_NotEnumerable);
1204
1205 ScopedObject object(scope, thisObject->callAsConstructor(executor, 1));
1206 return object->d();
1207}
1208
1209Promise::ReactionHandler *ExecutionEngine::getPromiseReactionHandler()
1210{
1211 Q_ASSERT(m_reactionHandler);
1212 return m_reactionHandler.data();
1213}
1214
1215Heap::Object *ExecutionEngine::newURIErrorObject(const QString &message)
1216{
1217 return ErrorObject::create<URIErrorObject>(this, message);
1218}
1219
1220Heap::Object *ExecutionEngine::newEvalErrorObject(const QString &message)
1221{
1222 return ErrorObject::create<EvalErrorObject>(this, message);
1223}
1224
1225Heap::Object *ExecutionEngine::newVariantObject(const QMetaType type, const void *data)
1226{
1227 return memoryManager->allocate<VariantObject>(type, data);
1228}
1229
1230Heap::Object *ExecutionEngine::newForInIteratorObject(Object *o)
1231{
1232 Scope scope(this);
1233 ScopedObject obj(scope, memoryManager->allocate<ForInIteratorObject>(o));
1234 return obj->d();
1235}
1236
1237Heap::Object *ExecutionEngine::newMapIteratorObject(Object *o)
1238{
1239 return memoryManager->allocate<MapIteratorObject>(o->d(), this);
1240}
1241
1242Heap::Object *ExecutionEngine::newSetIteratorObject(Object *o)
1243{
1244 return memoryManager->allocate<SetIteratorObject>(o->d(), this);
1245}
1246
1247Heap::Object *ExecutionEngine::newArrayIteratorObject(Object *o)
1248{
1249 return memoryManager->allocate<ArrayIteratorObject>(o->d(), this);
1250}
1251
1252Heap::QmlContext *ExecutionEngine::qmlContext() const
1253{
1254 return currentStackFrame
1255 ? static_cast<Heap::QmlContext *>(qmlContext(currentContext()->d()))
1256 : nullptr;
1257}
1258
1259QObject *ExecutionEngine::qmlScopeObject() const
1260{
1261 Heap::QmlContext *ctx = qmlContext();
1262 if (!ctx)
1263 return nullptr;
1264
1265 return ctx->qml()->scopeObject;
1266}
1267
1268QQmlRefPointer<QQmlContextData> ExecutionEngine::callingQmlContext() const
1269{
1270 Heap::QmlContext *ctx = qmlContext();
1271 if (!ctx)
1272 return nullptr;
1273
1274 return ctx->qml()->context;
1275}
1276
1277StackTrace ExecutionEngine::stackTrace(int frameLimit) const
1278{
1279 StackTrace stack;
1280
1281 CppStackFrame *f = currentStackFrame;
1282 while (f && frameLimit) {
1283 QV4::StackFrame frame;
1284 frame.source = f->source();
1285 frame.function = f->function();
1286 frame.line = f->lineNumber();
1287
1288 stack.append(frame);
1289 if (f->isJSTypesFrame()) {
1290 if (static_cast<JSTypesStackFrame *>(f)->isTailCalling()) {
1291 QV4::StackFrame frame;
1292 frame.function = QStringLiteral("[elided tail calls]");
1293 stack.append(frame);
1294 }
1295 }
1296 --frameLimit;
1297 f = f->parentFrame();
1298 }
1299
1300 return stack;
1301}
1302
1303/* Helper and "C" linkage exported function to format a GDBMI stacktrace for
1304 * invocation by a debugger.
1305 * Sample GDB invocation: print qt_v4StackTrace((void*)0x7fffffffb290)
1306 * Sample CDB invocation: .call Qt5Qmld!qt_v4StackTrace(0x7fffffffb290) ; gh
1307 * Note: The helper is there to suppress MSVC warning 4190 about anything
1308 * with UDT return types in a "C" linkage function. */
1309
1310static inline char *v4StackTrace(const ExecutionContext *context)
1311{
1312 QString result;
1313 QTextStream str(&result);
1314 str << "stack=[";
1315 if (context && context->engine()) {
1316 const QList<StackFrame> stackTrace = context->engine()->stackTrace(20);
1317 for (int i = 0; i < stackTrace.size(); ++i) {
1318 if (i)
1319 str << ',';
1320 const QUrl url(stackTrace.at(i).source);
1321 const QString fileName = url.isLocalFile() ? url.toLocalFile() : url.toString();
1322 str << "frame={level=\"" << i << "\",func=\"" << stackTrace.at(i).function
1323 << "\",file=\"" << fileName << "\",fullname=\"" << fileName
1324 << "\",line=\"" << qAbs(stackTrace.at(i).line) << "\",language=\"js\"}";
1325 }
1326 }
1327 str << ']';
1328 return qstrdup(result.toLocal8Bit().constData());
1329}
1330
1331extern "C" Q_QML_EXPORT char *qt_v4StackTrace(void *executionContext)
1332{
1333 return v4StackTrace(reinterpret_cast<const ExecutionContext *>(executionContext));
1334}
1335
1336extern "C" Q_QML_EXPORT char *qt_v4StackTraceForEngine(void *executionEngine)
1337{
1338 auto engine = (reinterpret_cast<const ExecutionEngine *>(executionEngine));
1339 return v4StackTrace(engine->currentContext());
1340}
1341
1342QUrl ExecutionEngine::resolvedUrl(const QString &file)
1343{
1344 QUrl src(file);
1345 if (!src.isRelative())
1346 return src;
1347
1348 QUrl base;
1349 CppStackFrame *f = currentStackFrame;
1350 while (f) {
1351 if (f->v4Function) {
1352 base = f->v4Function->finalUrl();
1353 break;
1354 }
1355 f = f->parentFrame();
1356 }
1357
1358 if (base.isEmpty() && globalCode)
1359 base = globalCode->finalUrl();
1360
1361 if (base.isEmpty())
1362 return src;
1363
1364 return base.resolved(src);
1365}
1366
1367void ExecutionEngine::markObjects(MarkStack *markStack)
1368{
1369 for (int i = 0; i < NClasses; ++i) {
1370 if (Heap::InternalClass *c = classes[i])
1371 c->mark(markStack);
1372 }
1373
1374 identifierTable->markObjects(markStack);
1375
1376 for (const auto &compilationUnit : std::as_const(m_compilationUnits))
1377 compilationUnit->markObjects(markStack);
1378}
1379
1380ReturnedValue ExecutionEngine::throwError(const Value &value)
1381{
1382 // we can get in here with an exception already set, as the runtime
1383 // doesn't check after every operation that can throw.
1384 // in this case preserve the first exception to give correct error
1385 // information
1386 if (hasException)
1387 return Encode::undefined();
1388
1389 hasException = true;
1390 *exceptionValue = value;
1391 QV4::Scope scope(this);
1392 QV4::Scoped<ErrorObject> error(scope, value);
1393 if (!!error)
1394 exceptionStackTrace = *error->d()->stackTrace;
1395 else
1396 exceptionStackTrace = stackTrace();
1397
1398 if (QV4::Debugging::Debugger *debug = debugger())
1399 debug->aboutToThrow();
1400
1401 return Encode::undefined();
1402}
1403
1404ReturnedValue ExecutionEngine::catchException(StackTrace *trace)
1405{
1406 Q_ASSERT(hasException);
1407 if (trace)
1408 *trace = exceptionStackTrace;
1409 exceptionStackTrace.clear();
1410 hasException = false;
1411 ReturnedValue res = exceptionValue->asReturnedValue();
1412 *exceptionValue = Value::emptyValue();
1413 return res;
1414}
1415
1416ReturnedValue ExecutionEngine::throwError(const QString &message)
1417{
1418 Scope scope(this);
1419 ScopedValue v(scope, newString(message));
1420 v = newErrorObject(v);
1421 return throwError(v);
1422}
1423
1424ReturnedValue ExecutionEngine::throwSyntaxError(const QString &message, const QString &fileName, int line, int column)
1425{
1426 Scope scope(this);
1427 ScopedObject error(scope, newSyntaxErrorObject(message, fileName, line, column));
1428 return throwError(error);
1429}
1430
1431ReturnedValue ExecutionEngine::throwSyntaxError(const QString &message)
1432{
1433 Scope scope(this);
1434 ScopedObject error(scope, newSyntaxErrorObject(message));
1435 return throwError(error);
1436}
1437
1438
1439ReturnedValue ExecutionEngine::throwTypeError()
1440{
1441 Scope scope(this);
1442 ScopedObject error(scope, newTypeErrorObject(QStringLiteral("Type error")));
1443 return throwError(error);
1444}
1445
1446ReturnedValue ExecutionEngine::throwTypeError(const QString &message)
1447{
1448 Scope scope(this);
1449 ScopedObject error(scope, newTypeErrorObject(message));
1450 return throwError(error);
1451}
1452
1453ReturnedValue ExecutionEngine::throwReferenceError(const QString &name)
1454{
1455 Scope scope(this);
1456 QString msg = name + QLatin1String(" is not defined");
1457 ScopedObject error(scope, newReferenceErrorObject(msg));
1458 return throwError(error);
1459}
1460
1461ReturnedValue ExecutionEngine::throwReferenceError(const Value &value)
1462{
1463 Scope scope(this);
1464 ScopedString s(scope, value.toString(this));
1465 QString msg = s->toQString() + QLatin1String(" is not defined");
1466 ScopedObject error(scope, newReferenceErrorObject(msg));
1467 return throwError(error);
1468}
1469
1470ReturnedValue ExecutionEngine::throwReferenceError(const QString &message, const QString &fileName, int line, int column)
1471{
1472 Scope scope(this);
1473 QString msg = message;
1474 ScopedObject error(scope, newReferenceErrorObject(msg, fileName, line, column));
1475 return throwError(error);
1476}
1477
1478ReturnedValue ExecutionEngine::throwRangeError(const QString &message)
1479{
1480 Scope scope(this);
1481 ScopedObject error(scope, newRangeErrorObject(message));
1482 return throwError(error);
1483}
1484
1485ReturnedValue ExecutionEngine::throwRangeError(const Value &value)
1486{
1487 Scope scope(this);
1488 ScopedString s(scope, value.toString(this));
1489 QString msg = s->toQString() + QLatin1String(" out of range");
1490 ScopedObject error(scope, newRangeErrorObject(msg));
1491 return throwError(error);
1492}
1493
1494ReturnedValue ExecutionEngine::throwURIError(const Value &msg)
1495{
1496 Scope scope(this);
1497 ScopedObject error(scope, newURIErrorObject(msg));
1498 return throwError(error);
1499}
1500
1501ReturnedValue ExecutionEngine::throwUnimplemented(const QString &message)
1502{
1503 Scope scope(this);
1504 ScopedValue v(scope, newString(QLatin1String("Unimplemented ") + message));
1505 v = newErrorObject(v);
1506 return throwError(v);
1507}
1508
1509
1510QQmlError ExecutionEngine::catchExceptionAsQmlError()
1511{
1512 QV4::StackTrace trace;
1513 QV4::Scope scope(this);
1514 QV4::ScopedValue exception(scope, catchException(&trace));
1515 QQmlError error;
1516 if (!trace.isEmpty()) {
1517 QV4::StackFrame frame = trace.constFirst();
1518 error.setUrl(QUrl(frame.source));
1519 error.setLine(qAbs(frame.line));
1520 error.setColumn(frame.column);
1521 }
1522 QV4::Scoped<QV4::ErrorObject> errorObj(scope, exception);
1523 error.setDescription(exception->toQStringNoThrow());
1524 return error;
1525}
1526
1527void ExecutionEngine::amendException()
1528{
1529 const int missingLineNumber = currentStackFrame->missingLineNumber();
1530 const int lineNumber = currentStackFrame->lineNumber();
1531 Q_ASSERT(missingLineNumber != lineNumber);
1532
1533 auto amendStackTrace = [&](QV4::StackTrace *stackTrace) {
1534 for (auto it = stackTrace->begin(), end = stackTrace->end(); it != end; ++it) {
1535 if (it->line == missingLineNumber) {
1536 it->line = lineNumber;
1537 break;
1538 }
1539 }
1540 };
1541
1542 amendStackTrace(&exceptionStackTrace);
1543
1544 QV4::Scope scope(this);
1545 QV4::Scoped<QV4::ErrorObject> error(scope, *exceptionValue);
1546 if (error) // else some other value was thrown
1547 amendStackTrace(error->d()->stackTrace);
1548}
1549
1550// Variant conversion code
1551
1554static QVariant toVariant(
1555 const QV4::Value &value, QMetaType typeHint, JSToQVariantConversionBehavior conversionBehavior,
1556 V4ObjectSet *visitedObjects);
1557static QObject *qtObjectFromJS(const QV4::Value &value);
1558static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects = nullptr,
1560static bool convertToNativeQObject(const QV4::Value &value, QMetaType targetType, void **result);
1561
1562static QVariant toVariant(const QV4::Value &value, QMetaType metaType, JSToQVariantConversionBehavior conversionBehavior,
1563 V4ObjectSet *visitedObjects)
1564{
1565 Q_ASSERT (!value.isEmpty());
1566
1567 if (const QV4::VariantObject *v = value.as<QV4::VariantObject>())
1568 return v->d()->data();
1569
1570 if (metaType == QMetaType::fromType<bool>())
1571 return QVariant(value.toBoolean());
1572
1573 if (metaType == QMetaType::fromType<double>())
1574 return QVariant(value.toNumber());
1575
1576 if (metaType == QMetaType::fromType<float>())
1577 return QVariant(float(value.toNumber()));
1578
1579 if (metaType == QMetaType::fromType<QJsonValue>())
1580 return QVariant::fromValue(QV4::JsonObject::toJsonValue(value));
1581
1582 if (metaType == QMetaType::fromType<QJSValue>())
1583 return QVariant::fromValue(QJSValuePrivate::fromReturnedValue(value.asReturnedValue()));
1584
1585 if (const QV4::Object *o = value.as<QV4::Object>()) {
1586 QV4::Scope scope(o->engine());
1587 QV4::ScopedObject object(scope, o);
1588 if (metaType == QMetaType::fromType<QJsonObject>()
1589 && !value.as<ArrayObject>() && !value.as<FunctionObject>()) {
1590 return QVariant::fromValue(QV4::JsonObject::toJsonObject(object));
1591 } else if (QV4::QObjectWrapper *wrapper = object->as<QV4::QObjectWrapper>()) {
1592 return QVariant::fromValue<QObject *>(wrapper->object());
1593 } else if (object->as<QV4::QQmlContextWrapper>()) {
1594 return QVariant();
1595 } else if (QV4::QQmlTypeWrapper *w = object->as<QV4::QQmlTypeWrapper>()) {
1596 return w->toVariant();
1597 } else if (QV4::QQmlValueTypeWrapper *v = object->as<QV4::QQmlValueTypeWrapper>()) {
1598 return v->toVariant();
1599 } else if (QV4::QmlListWrapper *l = object->as<QV4::QmlListWrapper>()) {
1600 return l->toVariant();
1601 } else if (QV4::Sequence *s = object->as<QV4::Sequence>()) {
1602 if (metaType.isValid()
1603 && metaType != QMetaType::fromType<QVariant>()
1604 && metaType != s->d()->listType()) {
1605 // If we can, produce an accurate result.
1606 const QVariant result = QV4::SequencePrototype::toVariant(value, metaType);
1607 if (result.isValid())
1608 return result;
1609 }
1610
1611 // Otherwise produce the "natural" type of the sequence.
1612 return QV4::SequencePrototype::toVariant(s);
1613 } else if (auto association = object->as<QV4::VariantAssociationObject>()) {
1614 return association->d()->toVariant();
1615 }
1616 }
1617
1618 if (const QV4::ArrayObject *o = value.as<ArrayObject>()) {
1619 QV4::Scope scope(o->engine());
1620 QV4::ScopedArrayObject a(scope, o);
1621 if (metaType == QMetaType::fromType<QList<QObject *>>()) {
1622 QList<QObject *> list;
1623 uint length = a->getLength();
1624 QV4::Scoped<QV4::QObjectWrapper> qobjectWrapper(scope);
1625 for (uint ii = 0; ii < length; ++ii) {
1626 qobjectWrapper = a->get(ii);
1627 if (!!qobjectWrapper) {
1628 list << qobjectWrapper->object();
1629 } else {
1630 list << 0;
1631 }
1632 }
1633
1634 return QVariant::fromValue<QList<QObject*> >(list);
1635 } else if (metaType == QMetaType::fromType<QJsonArray>()) {
1636 return QVariant::fromValue(QV4::JsonObject::toJsonArray(a));
1637 }
1638
1639 QVariant retn = QV4::SequencePrototype::toVariant(value, metaType);
1640 if (retn.isValid())
1641 return retn;
1642 }
1643
1644 if (value.isUndefined())
1645 return QVariant();
1646 if (value.isNull())
1647 return QVariant::fromValue(nullptr);
1648 if (value.isBoolean())
1649 return value.booleanValue();
1650 if (value.isInteger())
1651 return value.integerValue();
1652 if (value.isNumber())
1653 return value.asDouble();
1654 if (String *s = value.stringValue()) {
1655 const QString &str = s->toQString();
1656 // QChars are stored as a strings
1657 if (metaType == QMetaType::fromType<QChar>() && str.size() == 1)
1658 return str.at(0);
1659 return str;
1660 }
1661 if (const QV4::DateObject *d = value.as<DateObject>()) {
1662 // NOTE: since we convert QTime to JS Date,
1663 // round trip will change the variant type (to QDateTime)!
1664
1665 if (metaType == QMetaType::fromType<QDate>())
1666 return DateObject::dateTimeToDate(d->toQDateTime());
1667
1668 if (metaType == QMetaType::fromType<QTime>())
1669 return d->toQDateTime().time();
1670
1671 if (metaType == QMetaType::fromType<QString>())
1672 return d->toString();
1673
1674 return d->toQDateTime();
1675 }
1676 if (const QV4::UrlObject *d = value.as<UrlObject>())
1677 return d->toQUrl();
1678 if (const ArrayBuffer *d = value.as<ArrayBuffer>())
1679 return d->asByteArray();
1680 if (const Symbol *symbol = value.as<Symbol>()) {
1681 return conversionBehavior == JSToQVariantConversionBehavior::Never
1682 ? QVariant::fromValue(QJSValuePrivate::fromReturnedValue(symbol->asReturnedValue()))
1683 : symbol->descriptiveString();
1684 }
1685
1686 const QV4::Object *object = value.as<QV4::Object>();
1687 Q_ASSERT(object);
1688 QV4::Scope scope(object->engine());
1689 QV4::ScopedObject o(scope, object);
1690
1691#if QT_CONFIG(regularexpression)
1692 if (QV4::RegExpObject *re = o->as<QV4::RegExpObject>())
1693 return re->toQRegularExpression();
1694#endif
1695
1696 if (metaType.isValid() && !(metaType.flags() & QMetaType::PointerToQObject)) {
1697 const QVariant result
1698 = QQmlValueTypeProvider::createValueType(value, metaType, scope.engine);
1699 if (result.isValid())
1700 return result;
1701 }
1702
1703 if (conversionBehavior == JSToQVariantConversionBehavior::Never)
1704 return QVariant::fromValue(QJSValuePrivate::fromReturnedValue(o->asReturnedValue()));
1705
1706 return objectToVariant(o, visitedObjects, conversionBehavior);
1707}
1708
1709QVariant ExecutionEngine::toVariantLossy(const Value &value)
1710{
1711 return ::toVariant(value, QMetaType(), JSToQVariantConversionBehavior::Aggressive, nullptr);
1712}
1713
1714QVariant ExecutionEngine::toVariant(
1715 const Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols)
1716{
1717 auto behavior = createJSValueForObjectsAndSymbols ? JSToQVariantConversionBehavior::Never
1718 : JSToQVariantConversionBehavior::Safish;
1719 return ::toVariant(value, typeHint, behavior, nullptr);
1720}
1721
1722template<typename Association>
1724 const QV4::Object *o, V4ObjectSet *visitedObjects,
1725 JSToQVariantConversionBehavior conversionBehvior)
1726{
1727 Association association;
1728 QV4::Scope scope(o->engine());
1729 QV4::ObjectIterator it(scope, o, QV4::ObjectIterator::EnumerableOnly);
1730 QV4::ScopedValue name(scope);
1731 QV4::ScopedValue val(scope);
1732 while (1) {
1733 name = it.nextPropertyNameAsString(val);
1734 if (name->isNull())
1735 break;
1736
1737 QString key = name->toQStringNoThrow();
1738 association.insert(key, ::toVariant(
1739 val, /*type hint*/ QMetaType {},
1740 conversionBehvior, visitedObjects));
1741 }
1742 return association;
1743}
1744
1745static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects,
1746 JSToQVariantConversionBehavior conversionBehvior)
1747{
1748 Q_ASSERT(o);
1749
1750 V4ObjectSet recursionGuardSet;
1751 if (!visitedObjects) {
1752 visitedObjects = &recursionGuardSet;
1753 } else if (visitedObjects->contains(o->d())) {
1754 // Avoid recursion.
1755 // For compatibility with QVariant{List,Map} conversion, we return an
1756 // empty object (and no error is thrown).
1757 if (o->as<ArrayObject>())
1758 return QVariantList();
1759 return QVariantMap();
1760 }
1761 visitedObjects->insert(o->d());
1762
1763 QVariant result;
1764
1765 if (o->as<ArrayObject>()) {
1766 QV4::Scope scope(o->engine());
1767 QV4::ScopedArrayObject a(scope, o->asReturnedValue());
1768 QV4::ScopedValue v(scope);
1769 QVariantList list;
1770
1771 int length = a->getLength();
1772 for (int ii = 0; ii < length; ++ii) {
1773 v = a->get(ii);
1774 list << ::toVariant(v, QMetaType {}, conversionBehvior,
1775 visitedObjects);
1776 }
1777
1778 result = list;
1779 } else if (o->getPrototypeOf() == o->engine()->objectPrototype()->d()
1780 || (conversionBehvior == JSToQVariantConversionBehavior::Aggressive &&
1781 !o->as<QV4::FunctionObject>())) {
1782 /* FunctionObject is excluded for historical reasons, even though
1783 objects with a custom prototype risk losing information
1784 But the Aggressive path is used only in QJSValue::toVariant
1785 which is documented to be lossy
1786 */
1787 result = objectToVariantAssociation<QVariantMap>(o, visitedObjects, conversionBehvior);
1788 } else {
1789 // If it's not a plain object, we can only save it as QJSValue.
1790 result = QVariant::fromValue(QJSValuePrivate::fromReturnedValue(o->asReturnedValue()));
1791 }
1792
1793 visitedObjects->remove(o->d());
1794 return result;
1795}
1796
1797/*!
1798 \internal
1799
1800 Transform the given \a metaType and \a ptr into a JavaScript representation.
1801 */
1802QV4::ReturnedValue ExecutionEngine::fromData(
1803 QMetaType metaType, const void *ptr,
1804 QV4::Heap::Object *container, int property, uint flags)
1805{
1806 const auto createSequence = [&](const QMetaSequence metaSequence) {
1807 QV4::Scope scope(this);
1808 QV4::Scoped<Sequence> sequence(scope);
1809 if (container) {
1810 return QV4::SequencePrototype::newSequence(
1811 this, metaType, metaSequence, ptr,
1812 container, property, Heap::ReferenceObject::Flags(flags));
1813 } else {
1814 return QV4::SequencePrototype::fromData(this, metaType, metaSequence, ptr);
1815 }
1816 };
1817
1818 const int type = metaType.id();
1819 if (type < QMetaType::User) {
1820 switch (QMetaType::Type(type)) {
1821 case QMetaType::UnknownType:
1822 case QMetaType::Void:
1823 return QV4::Encode::undefined();
1824 case QMetaType::Nullptr:
1825 case QMetaType::VoidStar:
1826 return QV4::Encode::null();
1827 case QMetaType::Bool:
1828 return QV4::Encode(*reinterpret_cast<const bool*>(ptr));
1829 case QMetaType::Int:
1830 return QV4::Encode(*reinterpret_cast<const int*>(ptr));
1831 case QMetaType::UInt:
1832 return QV4::Encode(*reinterpret_cast<const uint*>(ptr));
1833 case QMetaType::Long:
1834 return QV4::Encode((double)*reinterpret_cast<const long *>(ptr));
1835 case QMetaType::ULong:
1836 return QV4::Encode((double)*reinterpret_cast<const ulong *>(ptr));
1837 case QMetaType::LongLong:
1838 return QV4::Encode((double)*reinterpret_cast<const qlonglong*>(ptr));
1839 case QMetaType::ULongLong:
1840 return QV4::Encode((double)*reinterpret_cast<const qulonglong*>(ptr));
1841 case QMetaType::Double:
1842 return QV4::Encode(*reinterpret_cast<const double*>(ptr));
1843 case QMetaType::QString:
1844 return newString(*reinterpret_cast<const QString*>(ptr))->asReturnedValue();
1845 case QMetaType::QByteArray:
1846 return newArrayBuffer(*reinterpret_cast<const QByteArray*>(ptr))->asReturnedValue();
1847 case QMetaType::Float:
1848 return QV4::Encode(*reinterpret_cast<const float*>(ptr));
1849 case QMetaType::Short:
1850 return QV4::Encode((int)*reinterpret_cast<const short*>(ptr));
1851 case QMetaType::UShort:
1852 return QV4::Encode((int)*reinterpret_cast<const unsigned short*>(ptr));
1853 case QMetaType::Char:
1854 return QV4::Encode((int)*reinterpret_cast<const char*>(ptr));
1855 case QMetaType::UChar:
1856 return QV4::Encode((int)*reinterpret_cast<const unsigned char*>(ptr));
1857 case QMetaType::SChar:
1858 return QV4::Encode((int)*reinterpret_cast<const signed char*>(ptr));
1859 case QMetaType::QChar:
1860 return newString(*reinterpret_cast<const QChar *>(ptr))->asReturnedValue();
1861 case QMetaType::Char16:
1862 return newString(QChar(*reinterpret_cast<const char16_t *>(ptr)))->asReturnedValue();
1863 case QMetaType::QDateTime:
1864 return QV4::Encode(newDateObject(
1865 *reinterpret_cast<const QDateTime *>(ptr),
1866 container, property, flags));
1867 case QMetaType::QDate:
1868 return QV4::Encode(newDateObject(
1869 *reinterpret_cast<const QDate *>(ptr),
1870 container, property, flags));
1871 case QMetaType::QTime:
1872 return QV4::Encode(newDateObject(
1873 *reinterpret_cast<const QTime *>(ptr),
1874 container, property, flags));
1875#if QT_CONFIG(regularexpression)
1876 case QMetaType::QRegularExpression:
1877 return QV4::Encode(newRegExpObject(*reinterpret_cast<const QRegularExpression *>(ptr)));
1878#endif
1879 case QMetaType::QObjectStar:
1880 return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
1881 case QMetaType::QStringList:
1882 return createSequence(QMetaSequence::fromContainer<QStringList>());
1883 case QMetaType::QVariantList:
1884 return createSequence(QMetaSequence::fromContainer<QVariantList>());
1885 case QMetaType::QVariantMap:
1886 return VariantAssociationPrototype::fromQVariantMap(
1887 this,
1888 *reinterpret_cast<const QVariantMap *>(ptr),
1889 container, property, Heap::ReferenceObject::Flags(flags));
1890 case QMetaType::QVariantHash:
1891 return VariantAssociationPrototype::fromQVariantHash(
1892 this,
1893 *reinterpret_cast<const QVariantHash *>(ptr),
1894 container, property, Heap::ReferenceObject::Flags(flags));
1895 case QMetaType::QJsonValue:
1896 return QV4::JsonObject::fromJsonValue(this, *reinterpret_cast<const QJsonValue *>(ptr));
1897 case QMetaType::QJsonObject:
1898 return QV4::JsonObject::fromJsonObject(this, *reinterpret_cast<const QJsonObject *>(ptr));
1899 case QMetaType::QJsonArray:
1900 return QV4::JsonObject::fromJsonArray(this, *reinterpret_cast<const QJsonArray *>(ptr));
1901 case QMetaType::QPixmap:
1902 case QMetaType::QImage:
1903 // Scarce value types
1904 return QV4::Encode(newVariantObject(metaType, ptr));
1905 default:
1906 break;
1907 }
1908 }
1909
1910 if (metaType.flags() & QMetaType::IsEnumeration)
1911 return fromData(metaType.underlyingType(), ptr, container, property, flags);
1912
1913 QV4::Scope scope(this);
1914 if (metaType == QMetaType::fromType<QQmlListReference>()) {
1915 typedef QQmlListReferencePrivate QDLRP;
1916 QDLRP *p = QDLRP::get((QQmlListReference*)const_cast<void *>(ptr));
1917 if (p->object)
1918 return QV4::QmlListWrapper::create(scope.engine, p->property, p->propertyType);
1919 else
1920 return QV4::Encode::null();
1921 } else if (auto flags = metaType.flags(); flags & QMetaType::IsQmlList) {
1922 // casting to QQmlListProperty<QObject> is slightly nasty, but it's the
1923 // same QQmlListReference does.
1924 const auto *p = static_cast<const QQmlListProperty<QObject> *>(ptr);
1925 if (p->object)
1926 return QV4::QmlListWrapper::create(scope.engine, *p, metaType);
1927 else
1928 return QV4::Encode::null();
1929 } else if (metaType == QMetaType::fromType<QJSValue>()) {
1930 return QJSValuePrivate::convertToReturnedValue(
1931 this, *reinterpret_cast<const QJSValue *>(ptr));
1932 } else if (metaType == QMetaType::fromType<QList<QObject *> >()) {
1933 // XXX Can this be made more by using Array as a prototype and implementing
1934 // directly against QList<QObject*>?
1935 const QList<QObject *> &list = *(const QList<QObject *>*)ptr;
1936 QV4::ScopedArrayObject a(scope, newArrayObject());
1937 a->arrayReserve(list.size());
1938 QV4::ScopedValue v(scope);
1939 for (int ii = 0; ii < list.size(); ++ii)
1940 a->arrayPut(ii, (v = QV4::QObjectWrapper::wrap(this, list.at(ii))));
1941 a->setArrayLengthUnchecked(list.size());
1942 return a.asReturnedValue();
1943 } else if (auto flags = metaType.flags(); flags & QMetaType::PointerToQObject) {
1944 if (flags.testFlag(QMetaType::IsConst))
1945 return QV4::QObjectWrapper::wrapConst(this, *reinterpret_cast<QObject* const *>(ptr));
1946 else
1947 return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
1948 } else if (metaType == QMetaType::fromType<QJSPrimitiveValue>()) {
1949 const QJSPrimitiveValue *primitive = static_cast<const QJSPrimitiveValue *>(ptr);
1950 switch (primitive->type()) {
1951 case QJSPrimitiveValue::Boolean:
1952 return Encode(primitive->asBoolean());
1953 case QJSPrimitiveValue::Integer:
1954 return Encode(primitive->asInteger());
1955 case QJSPrimitiveValue::String:
1956 return newString(primitive->asString())->asReturnedValue();
1957 case QJSPrimitiveValue::Undefined:
1958 return Encode::undefined();
1959 case QJSPrimitiveValue::Null:
1960 return Encode::null();
1961 case QJSPrimitiveValue::Double:
1962 return Encode(primitive->asDouble());
1963 }
1964 }
1965
1966 if (const QMetaObject *vtmo = QQmlMetaType::metaObjectForValueType(metaType)) {
1967 if (container) {
1968 return QV4::QQmlValueTypeWrapper::create(
1969 this, ptr, vtmo, metaType,
1970 container, property, Heap::ReferenceObject::Flags(flags));
1971 } else {
1972 return QV4::QQmlValueTypeWrapper::create(this, ptr, vtmo, metaType);
1973 }
1974 }
1975
1976 const QQmlType listType = QQmlMetaType::qmlListType(metaType);
1977 if (listType.isSequentialContainer())
1978 return createSequence(listType.listMetaSequence());
1979
1980 QMetaSequence::Iterable iterable;
1981 if (QMetaType::convert(metaType, ptr, QMetaType::fromType<QMetaSequence::Iterable>(), &iterable)) {
1982
1983 // If the resulting iterable is useful for anything, turn it into a QV4::Sequence.
1984 const QMetaSequence sequence = iterable.metaContainer();
1985 if (sequence.hasSize() && sequence.canGetValueAtIndex())
1986 return createSequence(sequence);
1987
1988 // As a last resort, try to read the contents of the container via an iterator
1989 // and build a JS array from them.
1990 if (sequence.hasConstIterator() && sequence.canGetValueAtConstIterator()) {
1991 QV4::ScopedArrayObject a(scope, newArrayObject());
1992 QV4::ScopedValue v(scope);
1993 for (auto it = iterable.constBegin(), end = iterable.constEnd(); it != end; ++it) {
1994 v = fromVariant(*it);
1995 a->push_back(v);
1996 }
1997 return a.asReturnedValue();
1998 }
1999 }
2000
2001 return QV4::Encode(newVariantObject(metaType, ptr));
2002}
2003
2004QV4::ReturnedValue QV4::ExecutionEngine::fromVariant(const QVariant &variant)
2005{
2006 return fromData(variant.metaType(), variant.constData());
2007}
2008
2009ReturnedValue ExecutionEngine::fromVariant(
2010 const QVariant &variant, Heap::Object *parent, int property, uint flags)
2011{
2012 return fromData(variant.metaType(), variant.constData(), parent, property, flags);
2013}
2014
2015QVariantMap ExecutionEngine::variantMapFromJS(const Object *o)
2016{
2017 Q_ASSERT(o);
2018 V4ObjectSet visitedObjects;
2019 visitedObjects.insert(o->d());
2020 return objectToVariantAssociation<QVariantMap>(
2021 o, &visitedObjects, JSToQVariantConversionBehavior::Safish);
2022}
2023
2024QVariantHash ExecutionEngine::variantHashFromJS(const Object *o)
2025{
2026 Q_ASSERT(o);
2027 V4ObjectSet visitedObjects;
2028 visitedObjects.insert(o->d());
2029 return objectToVariantAssociation<QVariantHash>(
2030 o, &visitedObjects, JSToQVariantConversionBehavior::Safish);
2031}
2032
2033// Converts the meta-type defined by the given type and data to JS.
2034// Returns the value if conversion succeeded, an empty handle otherwise.
2035QV4::ReturnedValue ExecutionEngine::metaTypeToJS(QMetaType type, const void *data)
2036{
2037 Q_ASSERT(data != nullptr);
2038
2039 if (type == QMetaType::fromType<QVariant>()) {
2040 // unwrap it: this is tested in QJSEngine, and makes the most sense for
2041 // end-user code too.
2042 return fromVariant(*reinterpret_cast<const QVariant*>(data));
2043 } else if (type == QMetaType::fromType<QUrl>()) {
2044 // Create a proper URL object here, rather than a variant.
2045 return newUrlObject(*reinterpret_cast<const QUrl *>(data))->asReturnedValue();
2046 }
2047
2048 return fromData(type, data);
2049}
2050
2051int ExecutionEngine::maxJSStackSize() const
2052{
2053 return s_maxJSStackSize;
2054}
2055
2056int ExecutionEngine::maxGCStackSize() const
2057{
2058 return s_maxGCStackSize;
2059}
2060
2061/*!
2062 \internal
2063 Returns \a length converted to int if its safe to
2064 pass to \c Scope::alloc.
2065 Otherwise it throws a RangeError, and returns 0.
2066 */
2067int ExecutionEngine::safeForAllocLength(qint64 len64)
2068{
2069 if (len64 < 0ll || len64 > qint64(std::numeric_limits<int>::max())) {
2070 throwRangeError(QStringLiteral("Invalid array length."));
2071 return 0;
2072 }
2073 if (len64 > qint64(this->jsStackLimit - this->jsStackTop)) {
2074 throwRangeError(QStringLiteral("Array too large for apply()."));
2075 return 0;
2076 }
2077 return len64;
2078}
2079
2080ReturnedValue ExecutionEngine::global()
2081{
2082 return globalObject->asReturnedValue();
2083}
2084
2085QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::compilationUnitForUrl(const QUrl &url) const
2086{
2087 // Gives the _most recently inserted_ CU of that URL. That's what we want.
2088 return m_compilationUnits.value(url);
2089}
2090
2091QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::executableCompilationUnit(
2092 QQmlRefPointer<CompiledData::CompilationUnit> &&unit)
2093{
2094 const QUrl url = unit->finalUrl();
2095 auto [begin, end] = std::as_const(m_compilationUnits).equal_range(url);
2096
2097 for (auto it = begin; it != end; ++it) {
2098 if ((*it)->baseCompilationUnit() == unit)
2099 return *it;
2100 }
2101
2102 auto executableUnit = m_compilationUnits.insert(
2103 url, ExecutableCompilationUnit::create(std::move(unit), this));
2104 // runtime data should not be initialized yet, so we don't need to mark the CU
2105 Q_ASSERT(!(*executableUnit)->runtimeStrings);
2106 return *executableUnit;
2107}
2108
2109QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::insertCompilationUnit(QQmlRefPointer<CompiledData::CompilationUnit> &&unit) {
2110 QUrl url = unit->finalUrl();
2111 auto executableUnit = ExecutableCompilationUnit::create(std::move(unit), this);
2112 /* Compilation Units stored in the engine are part of the gc roots,
2113 so we don't trigger any write-barrier when they are added. Use
2114 markCustom to make sure they are still marked when we insert them */
2115 QV4::WriteBarrier::markCustom(this, [&executableUnit](QV4::MarkStack *ms) {
2116 executableUnit->markObjects(ms);
2117 });
2118 return *m_compilationUnits.insert(std::move(url), std::move(executableUnit));
2119}
2120
2121void ExecutionEngine::trimCompilationUnits()
2122{
2123 for (auto it = m_compilationUnits.begin(); it != m_compilationUnits.end();) {
2124 if ((*it)->count() == 1)
2125 it = m_compilationUnits.erase(it);
2126 else
2127 ++it;
2128 }
2129}
2130
2131void ExecutionEngine::trimCompilationUnitsForUrl(const QUrl &url)
2132{
2133 auto it = m_compilationUnits.find(url);
2134 while (it != m_compilationUnits.end() && it.key() == url) {
2135 if ((*it)->count() == 1)
2136 it = m_compilationUnits.erase(it);
2137 else
2138 ++it;
2139 }
2140}
2141
2142/*!
2143 * Returns any _existing_ module for the given \a url and \a referrer. This
2144 * does not actually load anything. You must guarantee the existence of the
2145 * module by keeping a (direct or indirect) reference to it somewhere, or you
2146 * must live with a possible nullptr being returned.
2147 */
2148ExecutionEngine::Module ExecutionEngine::moduleForUrl(
2149 const QUrl &url, const ExecutableCompilationUnit *referrer)
2150{
2151 QUrl resolved = referrer
2152 ? referrer->finalUrl().resolved(QQmlMetaType::normalizedUrl(url))
2153 : QQmlMetaType::normalizedUrl(url);
2154 if (!resolved.path().endsWith(QLatin1String(".mjs")))
2155 resolved.setFragment(QLatin1String("module"));
2156
2157 // Executable compilation unit already present in engine
2158 auto existingModule = m_compilationUnits.constFind(resolved);
2159 if (existingModule != m_compilationUnits.constEnd())
2160 return *existingModule;
2161
2162 // Also try with the relative url, to support native modules.
2163 if (resolved != url) {
2164 existingModule = m_compilationUnits.constFind(url);
2165 if (existingModule != m_compilationUnits.constEnd())
2166 return *existingModule;
2167 }
2168
2169 if (const auto blob = m_typeLoader->getScript(resolved, QQmlTypeLoader::Synchronous))
2170 return executableCompilationUnit(blob->scriptData()->compilationUnit());
2171
2172 // Unavailable
2173 return Module();
2174}
2175
2176ExecutionEngine::Module ExecutionEngine::registerNativeModule(
2177 const QUrl &url, const QV4::Value &value)
2178{
2179 Q_ASSERT(!m_compilationUnits.contains(url));
2180
2181 const QV4::CompiledData::Unit *unit = Compiler::Codegen::generateNativeModuleUnitData(
2182 /*debugMode*/debugger() != nullptr, url.toString(), value);
2183 if (!unit)
2184 return Module();
2185
2186 QQmlRefPointer<CompiledData::CompilationUnit> cu;
2187
2188 // Make sure the type loader doesn't try to resolve the module anymore.
2189 // If some other code requests that same module, we need to produce the same CU.
2190 cu = typeLoader()->injectModule(url, unit);
2191
2192 QQmlRefPointer<ExecutableCompilationUnit> newModule = insertCompilationUnit(std::move(cu));
2193
2194 Q_ASSERT(m_compilationUnits.contains(url, newModule));
2195
2196 Scope scope(this);
2197 Scoped<QV4::Module> instance(scope, newModule->instantiate());
2198 Scoped<CallContext> context(scope, instance->d()->scope);
2199
2200 const CompiledData::Unit *unitData = newModule->baseCompilationUnit()->data;
2201 const CompiledData::ExportEntry *exportEntries = unitData->localExportEntryTable();
2202
2203 ScopedObject object(scope, value);
2204 for (uint i = 0, end = unitData->localExportEntryTableSize; i < end; ++i) {
2205 const CompiledData::ExportEntry *localExport = exportEntries + i;
2206 ScopedString localName(scope, newModule->runtimeStrings[localExport->localName]);
2207 const uint index
2208 = context->internalClass()->indexOfValueOrGetter(localName->toPropertyKey());
2209 QV4::Heap::CallContext *cc = context->d();
2210 Q_ASSERT(index < cc->locals.size);
2211 if (localName->toQString() == QLatin1String("default")) {
2212 cc->locals.set(this, index, value);
2213 continue;
2214 }
2215
2216 Q_ASSERT(object);
2217
2218 ScopedValue localValue(scope, object->get(localName));
2219 cc->locals.set(this, index, localValue);
2220 }
2221
2222 instance->d()->evaluated = true;
2223 return newModule;
2224}
2225
2227{
2228 using DiskCache = ExecutionEngine::DiskCache;
2229
2230 if (v == nullptr)
2231 return DiskCache::Enabled;
2232
2233 ExecutionEngine::DiskCacheOptions result = DiskCache::Disabled;
2234 const QList<QByteArray> options = QByteArray(v).split(',');
2235 for (const QByteArray &option : options) {
2236 if (option == "aot-bytecode")
2237 result |= DiskCache::AotByteCode;
2238 else if (option == "aot-native")
2239 result |= DiskCache::AotNative;
2240 else if (option == "aot")
2241 result |= DiskCache::Aot;
2242 else if (option == "qmlc-read")
2243 result |= DiskCache::QmlcRead;
2244 else if (option == "qmlc-write")
2245 result |= DiskCache::QmlcWrite;
2246 else if (option == "qmlc")
2247 result |= DiskCache::Qmlc;
2248 else
2249 qWarning() << "Ignoring unknown option to QML_DISK_CACHE:" << option;
2250 }
2251
2252 return result;
2253}
2254
2255ExecutionEngine::DiskCacheOptions ExecutionEngine::diskCacheOptions() const
2256{
2257 if (forceDiskCache())
2258 return DiskCache::Enabled;
2259 if (disableDiskCache() || debugger())
2260 return DiskCache::Disabled;
2261 static const DiskCacheOptions options = qmlGetConfigOption<
2262 DiskCacheOptions, transFormDiskCache>("QML_DISK_CACHE");
2263 return hasPreview.loadAcquire()
2264 ? (options & ~DiskCacheOptions(DiskCache::Aot)) // Disable AOT if preview enabled
2265 : options;
2266}
2267
2268void ExecutionEngine::callInContext(QV4::Function *function, QObject *self,
2269 QV4::ExecutionContext *context, int argc, void **args,
2270 QMetaType *types)
2271{
2272 if (!args) {
2273 Q_ASSERT(argc == 0);
2274 void *dummyArgs[] = { nullptr };
2275 QMetaType dummyTypes[] = { QMetaType::fromType<void>() };
2276 function->call(self, dummyArgs, dummyTypes, argc, context);
2277 return;
2278 }
2279 Q_ASSERT(types); // both args and types must be present
2280 // implicitly sets the return value, which is args[0]
2281 function->call(self, args, types, argc, context);
2282}
2283
2284QV4::ReturnedValue ExecutionEngine::callInContext(QV4::Function *function, QObject *self,
2285 QV4::ExecutionContext *context, int argc,
2286 const QV4::Value *argv)
2287{
2288 QV4::Scope scope(this);
2289 QV4::ScopedObject jsSelf(scope, QV4::QObjectWrapper::wrap(this, self));
2290 Q_ASSERT(jsSelf);
2291 return function->call(jsSelf, argv, argc, context);
2292}
2293
2294void ExecutionEngine::initQmlGlobalObject()
2295{
2296 initializeGlobal();
2297 lockObject(*globalObject);
2298}
2299
2300static bool globalNamesAreStaticallyKnown(QV4::Object *globalObject)
2301{
2302 const Heap::InternalClass *ic = globalObject->internalClass();
2303 const SharedInternalClassData<PropertyKey> &nameMap = ic->nameMap;
2304 bool clean = true;
2305 for (uint i = 0, end = ic->size; i < end; ++i) {
2306 const QV4::PropertyKey id = nameMap.at(i);
2307 if (id.isString()) {
2308 if (!Compiler::Codegen::isNameGlobal(id.toQString())) {
2309 qCritical() << id.toQString()
2310 << "is part of the JavaScript global object "
2311 "but not statically known to be global";
2312 clean = false;
2313 }
2314 }
2315 }
2316 return clean;
2317}
2318
2319#if QT_CONFIG(qml_xml_http_request)
2320void ExecutionEngine::setupXmlHttpRequestExtension()
2321{
2322 qt_add_domexceptions(this);
2323 m_xmlHttpRequestData = qt_add_qmlxmlhttprequest(this);
2324}
2325#endif
2326
2327void ExecutionEngine::initializeGlobal()
2328{
2329 createQtObject();
2330
2331 QV4::GlobalExtensions::init(globalObject, QJSEngine::AllExtensions);
2332
2333#if QT_CONFIG(qml_locale)
2334 QQmlLocale::registerStringLocaleCompare(this);
2335 QQmlDateExtension::registerExtension(this);
2336 QQmlNumberExtension::registerExtension(this);
2337#endif
2338
2339#if QT_CONFIG(qml_xml_http_request)
2340 setupXmlHttpRequestExtension();
2341#endif
2342
2343 qt_add_sqlexceptions(this);
2344
2345 Q_ASSERT(globalNamesAreStaticallyKnown(globalObject));
2346}
2347
2348void ExecutionEngine::createQtObject()
2349{
2350 QV4::Scope scope(this);
2351 QtObject *qtObject = new QtObject(this);
2352
2353 if (publicEngine) {
2354 // If we have a public engine, parent to that one so that we don't delete
2355 // the instance on QQmlEngine::clearSingletons() we still need it as property
2356 // of the global object after all.
2357 QJSEngine::setObjectOwnership(qtObject, QJSEngine::CppOwnership);
2358 qtObject->setParent(publicEngine);
2359 } else {
2360 // If we don't have a public engine, the object isn't exposed as a singleton.
2361 // We can use JavaScript ownership to have it deleted by the last GC run.
2362 QJSEngine::setObjectOwnership(qtObject, QJSEngine::JavaScriptOwnership);
2363 }
2364
2365 QV4::ScopedObject qtObjectWrapper(
2366 scope, QV4::QObjectWrapper::wrap(this, qtObject));
2367 QV4::ScopedObject qtNamespaceWrapper(
2368 scope, QV4::QMetaObjectWrapper::create(this, &Qt::staticMetaObject));
2369 QV4::ScopedObject qtObjectProtoWrapper(
2370 scope, qtObjectWrapper->getPrototypeOf());
2371
2372 qtNamespaceWrapper->setPrototypeOf(qtObjectProtoWrapper);
2373 qtObjectWrapper->setPrototypeOf(qtNamespaceWrapper);
2374
2375 globalObject->defineDefaultProperty(QStringLiteral("Qt"), qtObjectWrapper);
2376}
2377
2378void ExecutionEngine::setQmlEngine(QQmlEngine *engine)
2379{
2380 // Second stage of initialization. We're updating some more prototypes here.
2381 isInitialized = false;
2382 m_qmlEngine = engine;
2383 initQmlGlobalObject();
2384 isInitialized = true;
2385}
2386
2387static void freeze_recursive(QV4::ExecutionEngine *v4, QV4::Object *object)
2388{
2389 if (object->as<QV4::QObjectWrapper>() || object->internalClass()->isFrozen())
2390 return;
2391
2392 QV4::Scope scope(v4);
2393
2394 bool instanceOfObject = false;
2395 QV4::ScopedObject p(scope, object->getPrototypeOf());
2396 while (p) {
2397 if (p->d() == v4->objectPrototype()->d()) {
2398 instanceOfObject = true;
2399 break;
2400 }
2401 p = p->getPrototypeOf();
2402 }
2403 if (!instanceOfObject)
2404 return;
2405
2406 Heap::InternalClass *frozen = object->internalClass()->frozen();
2407 object->setInternalClass(frozen); // Immediately assign frozen to prevent it from getting GC'd
2408
2409 QV4::ScopedObject o(scope);
2410 for (uint i = 0; i < frozen->size; ++i) {
2411 if (!frozen->nameMap.at(i).isStringOrSymbol())
2412 continue;
2413 o = *object->propertyData(i);
2414 if (o)
2415 freeze_recursive(v4, o);
2416 }
2417}
2418
2419void ExecutionEngine::freezeObject(const QV4::Value &value)
2420{
2421 QV4::Scope scope(this);
2422 QV4::ScopedObject o(scope, value);
2423 freeze_recursive(this, o);
2424}
2425
2426void ExecutionEngine::lockObject(const QV4::Value &value)
2427{
2428 QV4::Scope scope(this);
2429 ScopedObject object(scope, value);
2430 if (!object)
2431 return;
2432
2433 std::vector<Heap::Object *> stack { object->d() };
2434
2435 // Methods meant to be overridden
2436 const PropertyKey writableMembers[] = {
2437 id_toString()->propertyKey(),
2438 id_toLocaleString()->propertyKey(),
2439 id_valueOf()->propertyKey(),
2440 id_constructor()->propertyKey()
2441 };
2442 const auto writableBegin = std::begin(writableMembers);
2443 const auto writableEnd = std::end(writableMembers);
2444
2445 while (!stack.empty()) {
2446 object = stack.back();
2447 stack.pop_back();
2448
2449 if (object->as<QV4::QObjectWrapper>() || object->internalClass()->isLocked())
2450 continue;
2451
2452 Scoped<InternalClass> locked(scope, object->internalClass()->locked());
2453 QV4::ScopedObject member(scope);
2454
2455 // Taking this copy is cheap. It's refcounted. This avoids keeping a reference
2456 // to the original IC.
2457 const SharedInternalClassData<PropertyKey> nameMap = locked->d()->nameMap;
2458
2459 for (uint i = 0, end = locked->d()->size; i < end; ++i) {
2460 const PropertyKey key = nameMap.at(i);
2461 if (!key.isStringOrSymbol())
2462 continue;
2463 if ((member = *object->propertyData(i))) {
2464 stack.push_back(member->d());
2465 if (std::find(writableBegin, writableEnd, key) == writableEnd) {
2466 PropertyAttributes attributes = locked->d()->find(key).attributes;
2467 attributes.setConfigurable(false);
2468 attributes.setWritable(false);
2469 locked = locked->changeMember(key, attributes);
2470 }
2471 }
2472 }
2473
2474 object->setInternalClass(locked->d());
2475 }
2476}
2477
2478void ExecutionEngine::startTimer(const QString &timerName)
2479{
2480 if (!m_time.isValid())
2481 m_time.start();
2482 m_startedTimers[timerName] = m_time.elapsed();
2483}
2484
2485qint64 ExecutionEngine::stopTimer(const QString &timerName, bool *wasRunning)
2486{
2487 if (!m_startedTimers.contains(timerName)) {
2488 *wasRunning = false;
2489 return 0;
2490 }
2491 *wasRunning = true;
2492 qint64 startedAt = m_startedTimers.take(timerName);
2493 return m_time.elapsed() - startedAt;
2494}
2495
2496int ExecutionEngine::consoleCountHelper(const QString &file, quint16 line, quint16 column)
2497{
2498 const QString key = file + QString::number(line) + QString::number(column);
2499 int number = m_consoleCount.value(key, 0);
2500 number++;
2501 m_consoleCount.insert(key, number);
2502 return number;
2503}
2504
2505void ExecutionEngine::setExtensionData(int index, Deletable *data)
2506{
2507 if (m_extensionData.size() <= index)
2508 m_extensionData.resize(index + 1);
2509
2510 if (m_extensionData.at(index))
2511 delete m_extensionData.at(index);
2512
2513 m_extensionData[index] = data;
2514}
2515
2516template<typename Source>
2517bool convertToIterable(QMetaType metaType, void *data, Source *sequence)
2518{
2519 QMetaSequence::Iterable iterable;
2520 if (!QMetaType::view(metaType, data, QMetaType::fromType<QMetaSequence::Iterable>(), &iterable))
2521 return false;
2522
2523 // Clear the sequence before appending. There may be stale data in there.
2524 metaType.destruct(data);
2525 metaType.construct(data);
2526
2527 QV4::Scope scope(sequence->engine());
2528 QV4::ScopedValue v(scope);
2529
2530 const QMetaType elementMetaType = iterable.metaContainer().valueMetaType();
2531 QVariant element;
2532 void *elementData = nullptr;
2533 if (elementMetaType == QMetaType::fromType<QVariant>()) {
2534 elementData = &element;
2535 } else {
2536 element = QVariant(elementMetaType);
2537 elementData = element.data();
2538 }
2539
2540 for (qsizetype i = 0, end = sequence->getLength(); i < end; ++i) {
2541 v = sequence->get(i);
2542 ExecutionEngine::metaTypeFromJS(v, elementMetaType, elementData);
2543 iterable.append(element);
2544 }
2545
2546 return true;
2547}
2548
2549/*!
2550 * \internal
2551 *
2552 * Converts a JS value to a meta-type.
2553 * \a data must point to a default-constructed instance of \a metaType.
2554 * Returns \c true if conversion succeeded, \c false otherwise. In the latter case,
2555 * \a data is not modified.
2556 */
2557bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, void *data)
2558{
2559 // check if it's one of the types we know
2560 switch (metaType.id()) {
2561 case QMetaType::Bool:
2562 *reinterpret_cast<bool*>(data) = value.toBoolean();
2563 return true;
2564 case QMetaType::Int:
2565 *reinterpret_cast<int*>(data) = value.toInt32();
2566 return true;
2567 case QMetaType::UInt:
2568 *reinterpret_cast<uint*>(data) = value.toUInt32();
2569 return true;
2570 case QMetaType::Long:
2571 *reinterpret_cast<long*>(data) = long(value.toInteger());
2572 return true;
2573 case QMetaType::ULong:
2574 *reinterpret_cast<ulong*>(data) = ulong(value.toInteger());
2575 return true;
2576 case QMetaType::LongLong:
2577 *reinterpret_cast<qlonglong*>(data) = qlonglong(value.toInteger());
2578 return true;
2579 case QMetaType::ULongLong:
2580 *reinterpret_cast<qulonglong*>(data) = qulonglong(value.toInteger());
2581 return true;
2582 case QMetaType::Double:
2583 *reinterpret_cast<double*>(data) = value.toNumber();
2584 return true;
2585 case QMetaType::QString:
2586 if (value.isUndefined())
2587 *reinterpret_cast<QString*>(data) = QStringLiteral("undefined");
2588 else if (value.isNull())
2589 *reinterpret_cast<QString*>(data) = QStringLiteral("null");
2590 else
2591 *reinterpret_cast<QString*>(data) = value.toQString();
2592 return true;
2593 case QMetaType::QByteArray:
2594 if (const ArrayBuffer *ab = value.as<ArrayBuffer>()) {
2595 *reinterpret_cast<QByteArray*>(data) = ab->asByteArray();
2596 } else if (const String *string = value.as<String>()) {
2597 *reinterpret_cast<QByteArray*>(data) = string->toQString().toUtf8();
2598 } else if (const ArrayObject *ao = value.as<ArrayObject>()) {
2599 // Since QByteArray is sequentially iterable, we have to construct it from a JS Array.
2600 QByteArray result;
2601 const qint64 length = ao->getLength();
2602 result.reserve(length);
2603 QV4::Scope scope(ao->engine());
2604 QV4::ScopedValue v(scope);
2605 for (qint64 i = 0; i < length; ++i) {
2606 char value = 0;
2607 v = ao->get(i);
2608 ExecutionEngine::metaTypeFromJS(v, QMetaType::fromType<char>(), &value);
2609 result.push_back(value);
2610 }
2611 *reinterpret_cast<QByteArray*>(data) = std::move(result);
2612 } else {
2613 *reinterpret_cast<QByteArray*>(data) = QByteArray();
2614 }
2615 return true;
2616 case QMetaType::Float:
2617 *reinterpret_cast<float*>(data) = value.toNumber();
2618 return true;
2619 case QMetaType::Short:
2620 *reinterpret_cast<short*>(data) = short(value.toInt32());
2621 return true;
2622 case QMetaType::UShort:
2623 *reinterpret_cast<unsigned short*>(data) = value.toUInt16();
2624 return true;
2625 case QMetaType::Char:
2626 *reinterpret_cast<char*>(data) = char(value.toInt32());
2627 return true;
2628 case QMetaType::UChar:
2629 *reinterpret_cast<unsigned char*>(data) = (unsigned char)(value.toInt32());
2630 return true;
2631 case QMetaType::SChar:
2632 *reinterpret_cast<signed char*>(data) = (signed char)(value.toInt32());
2633 return true;
2634 case QMetaType::QChar:
2635 if (String *s = value.stringValue()) {
2636 QString str = s->toQString();
2637 *reinterpret_cast<QChar*>(data) = str.isEmpty() ? QChar() : str.at(0);
2638 } else {
2639 *reinterpret_cast<QChar*>(data) = QChar(ushort(value.toUInt16()));
2640 }
2641 return true;
2642 case QMetaType::QDateTime:
2643 if (const QV4::DateObject *d = value.as<DateObject>()) {
2644 *reinterpret_cast<QDateTime *>(data) = d->toQDateTime();
2645 return true;
2646 } break;
2647 case QMetaType::QDate:
2648 if (const QV4::DateObject *d = value.as<DateObject>()) {
2649 *reinterpret_cast<QDate *>(data) = DateObject::dateTimeToDate(d->toQDateTime());
2650 return true;
2651 } break;
2652 case QMetaType::QTime:
2653 if (const QV4::DateObject *d = value.as<DateObject>()) {
2654 *reinterpret_cast<QTime *>(data) = d->toQDateTime().time();
2655 return true;
2656 } break;
2657 case QMetaType::QUrl:
2658 if (String *s = value.stringValue()) {
2659 *reinterpret_cast<QUrl *>(data) = QUrl(s->toQString());
2660 return true;
2661 } else if (const QV4::UrlObject *d = value.as<UrlObject>()) {
2662 *reinterpret_cast<QUrl *>(data) = d->toQUrl();
2663 return true;
2664 } else if (const QV4::VariantObject *d = value.as<VariantObject>()) {
2665 const QVariant *variant = &d->d()->data();
2666 if (variant->metaType() == QMetaType::fromType<QUrl>()) {
2667 *reinterpret_cast<QUrl *>(data)
2668 = *reinterpret_cast<const QUrl *>(variant->constData());
2669 return true;
2670 }
2671 }
2672 break;
2673#if QT_CONFIG(regularexpression)
2674 case QMetaType::QRegularExpression:
2675 if (const QV4::RegExpObject *r = value.as<QV4::RegExpObject>()) {
2676 *reinterpret_cast<QRegularExpression *>(data) = r->toQRegularExpression();
2677 return true;
2678 } break;
2679#endif
2680 case QMetaType::QObjectStar: {
2681 if (value.isNull()) {
2682 *reinterpret_cast<QObject* *>(data) = nullptr;
2683 return true;
2684 }
2685 if (value.as<QV4::QObjectWrapper>()) {
2686 *reinterpret_cast<QObject* *>(data) = qtObjectFromJS(value);
2687 return true;
2688 }
2689 break;
2690 }
2691 case QMetaType::QStringList: {
2692 const QV4::ArrayObject *a = value.as<QV4::ArrayObject>();
2693 if (a) {
2694 *reinterpret_cast<QStringList *>(data) = a->toQStringList();
2695 return true;
2696 }
2697 break;
2698 }
2699 case QMetaType::QVariantList: {
2700 const QV4::ArrayObject *a = value.as<QV4::ArrayObject>();
2701 if (a) {
2702 *reinterpret_cast<QVariantList *>(data) = ExecutionEngine::toVariant(
2703 *a, /*typeHint*/QMetaType{}, /*createJSValueForObjectsAndSymbols*/false)
2704 .toList();
2705 return true;
2706 }
2707 break;
2708 }
2709 case QMetaType::QVariantMap: {
2710 const QV4::Object *o = value.as<QV4::Object>();
2711 if (o) {
2712 *reinterpret_cast<QVariantMap *>(data) = o->engine()->variantMapFromJS(o);
2713 return true;
2714 }
2715 break;
2716 }
2717 case QMetaType::QVariant:
2718 if (value.as<QV4::Managed>()) {
2719 *reinterpret_cast<QVariant*>(data) = ExecutionEngine::toVariant(
2720 value, /*typeHint*/QMetaType{}, /*createJSValueForObjectsAndSymbols*/false);
2721 } else if (value.isNull()) {
2722 *reinterpret_cast<QVariant*>(data) = QVariant::fromValue(nullptr);
2723 } else if (value.isUndefined()) {
2724 *reinterpret_cast<QVariant*>(data) = QVariant();
2725 } else if (value.isBoolean()) {
2726 *reinterpret_cast<QVariant*>(data) = QVariant(value.booleanValue());
2727 } else if (value.isInteger()) {
2728 *reinterpret_cast<QVariant*>(data) = QVariant(value.integerValue());
2729 } else if (value.isDouble()) {
2730 *reinterpret_cast<QVariant*>(data) = QVariant(value.doubleValue());
2731 }
2732 return true;
2733 case QMetaType::QJsonValue:
2734 *reinterpret_cast<QJsonValue *>(data) = QV4::JsonObject::toJsonValue(value);
2735 return true;
2736 case QMetaType::QJsonObject: {
2737 *reinterpret_cast<QJsonObject *>(data) = QV4::JsonObject::toJsonObject(value.as<Object>());
2738 return true;
2739 }
2740 case QMetaType::QJsonArray: {
2741 *reinterpret_cast<QJsonArray *>(data) = QV4::JsonObject::toJsonArray(value.as<Object>());
2742 return true;
2743 }
2744 default:
2745 break;
2746 }
2747
2748 if (metaType.flags() & QMetaType::IsEnumeration) {
2749 *reinterpret_cast<int *>(data) = value.toInt32();
2750 return true;
2751 }
2752
2753 if (const QV4::QmlListWrapper *wrapper = value.as<QV4::QmlListWrapper>()) {
2754 if (metaType == QMetaType::fromType<QQmlListReference>()) {
2755 *reinterpret_cast<QQmlListReference *>(data) = wrapper->toListReference();
2756 return true;
2757 }
2758
2759 const auto wrapperPrivate = wrapper->d();
2760 if (metaType == QMetaType::fromType<QQmlListProperty<QObject> *>()
2761 || metaType == wrapperPrivate->propertyType()) {
2762 *reinterpret_cast<QQmlListProperty<QObject> *>(data) = *wrapperPrivate->property();
2763 return true;
2764 }
2765
2766 if (metaType == QMetaType::fromType<QObjectList>()) {
2767 *reinterpret_cast<QObjectList *>(data)
2768 = wrapperPrivate->property()->toList<QObjectList>();
2769 return true;
2770 }
2771
2772 if (convertToIterable(metaType, data, wrapper))
2773 return true;
2774 }
2775
2776 if (const QQmlValueTypeWrapper *vtw = value.as<QQmlValueTypeWrapper>()) {
2777 const QMetaType valueType = vtw->type();
2778 if (valueType == metaType)
2779 return vtw->toGadget(data);
2780
2781 Heap::QQmlValueTypeWrapper *d = vtw->d();
2782 if (d->isReference())
2783 d->readReference();
2784
2785 if (void *gadgetPtr = d->gadgetPtr()) {
2786 if (QQmlValueTypeProvider::populateValueType(
2787 metaType, data, valueType, gadgetPtr, vtw->engine())) {
2788 return true;
2789 }
2790 if (QMetaType::canConvert(valueType, metaType))
2791 return QMetaType::convert(valueType, gadgetPtr, metaType, data);
2792 } else {
2793 QVariant empty(valueType);
2794 if (QQmlValueTypeProvider::populateValueType(
2795 metaType, data, valueType, empty.data(), vtw->engine())) {
2796 return true;
2797 }
2798 if (QMetaType::canConvert(valueType, metaType))
2799 return QMetaType::convert(valueType, empty.data(), metaType, data);
2800 }
2801 }
2802
2803 // Try to use magic; for compatibility with qjsvalue_cast.
2804
2805 if (convertToNativeQObject(value, metaType, reinterpret_cast<void **>(data)))
2806 return true;
2807
2808 const bool isPointer = (metaType.flags() & QMetaType::IsPointer);
2809 const QV4::VariantObject *variantObject = value.as<QV4::VariantObject>();
2810 if (variantObject) {
2811 // Actually a reference, because we're poking it for its data() below and we want
2812 // the _original_ data, not some copy.
2813 QVariant &var = variantObject->d()->data();
2814
2815 if (var.metaType() == metaType) {
2816 metaType.destruct(data);
2817 metaType.construct(data, var.data());
2818 return true;
2819 }
2820
2821 if (isPointer) {
2822 const QByteArray pointedToTypeName = QByteArray(metaType.name()).chopped(1);
2823 const QMetaType valueType = QMetaType::fromName(pointedToTypeName);
2824
2825 if (valueType == var.metaType()) {
2826 // ### Qt7: Remove this. Returning pointers to potentially gc'd data is crazy.
2827 // We have T t, T* is requested, so return &t.
2828 *reinterpret_cast<const void **>(data) = var.data();
2829 return true;
2830 } else if (Object *o = value.objectValue()) {
2831 // Look in the prototype chain.
2832 QV4::Scope scope(o->engine());
2833 QV4::ScopedObject proto(scope, o->getPrototypeOf());
2834 while (proto) {
2835 bool canCast = false;
2836 if (QV4::VariantObject *vo = proto->as<QV4::VariantObject>()) {
2837 const QVariant &v = vo->d()->data();
2838 canCast = (metaType == v.metaType());
2839 }
2840 else if (proto->as<QV4::QObjectWrapper>()) {
2841 QV4::ScopedObject p(scope, proto.getPointer());
2842 if (QObject *qobject = qtObjectFromJS(p)) {
2843 if (const QMetaObject *metaObject = metaType.metaObject())
2844 canCast = metaObject->cast(qobject) != nullptr;
2845 else
2846 canCast = qobject->qt_metacast(pointedToTypeName);
2847 }
2848 }
2849 if (canCast) {
2850 const QMetaType varType = var.metaType();
2851 if (varType.flags() & QMetaType::IsPointer) {
2852 *reinterpret_cast<const void **>(data)
2853 = *reinterpret_cast<void *const *>(var.data());
2854 } else {
2855 *reinterpret_cast<const void **>(data) = var.data();
2856 }
2857 return true;
2858 }
2859 proto = proto->getPrototypeOf();
2860 }
2861 }
2862 } else if (QQmlValueTypeProvider::populateValueType(
2863 metaType, data, var.metaType(), var.data(), variantObject->engine())) {
2864 return true;
2865 }
2866 } else if (value.isNull() && isPointer) {
2867 *reinterpret_cast<void* *>(data) = nullptr;
2868 return true;
2869 } else if (metaType == QMetaType::fromType<QJSValue>()) {
2870 QJSValuePrivate::setValue(reinterpret_cast<QJSValue*>(data), value);
2871 return true;
2872 } else if (metaType == QMetaType::fromType<QJSPrimitiveValue>()) {
2873 *reinterpret_cast<QJSPrimitiveValue *>(data) = createPrimitive(&value);
2874 return true;
2875 } else if (metaType == QMetaType::fromType<QJSManagedValue>()) {
2876 QJSManagedValue *managedData = reinterpret_cast<QJSManagedValue *>(data);
2877
2878 const Managed *managedValue = value.as<Managed>();
2879 if (Value *d = QJSManagedValuePrivate::member(managedData)) {
2880 // If the QJSManagedValue has an engine already,
2881 // we can assign a primitive without risking crosstalk between engines.
2882 if (!managedValue) {
2883 *d = value;
2884 return true;
2885 }
2886
2887 // If the engines match, we don't have to allocate a new persistent value
2888 if (reinterpret_cast<const Managed *>(d)->engine() == managedValue->engine()) {
2889 *d = value;
2890 return true;
2891 }
2892 }
2893
2894 if (managedValue) {
2895 // If value is itself managed, we create a new QJSManagedValue
2896 // to make sure the engine matches.
2897 *managedData = QJSManagedValuePrivate::create(*managedValue);
2898 return true;
2899 }
2900
2901 // The default-constructed QJSManagedValue (without engine) equals undefined.
2902 // So we can get away with doing nothing in this case.
2903 if (value.isUndefined())
2904 return true;
2905
2906 // Otherwise we can't do it. We need an engine from somewhere.
2907 return false;
2908 } else if (!isPointer) {
2909 const QV4::Managed *managed = value.as<QV4::Managed>();
2910 if (QQmlValueTypeProvider::populateValueType(
2911 metaType, data, value, managed ? managed->engine() : nullptr)) {
2912 return true;
2913 }
2914 }
2915
2916 if (const QV4::Sequence *sequence = value.as<Sequence>()) {
2917 const QVariant result = QV4::SequencePrototype::toVariant(sequence);
2918 if (result.metaType() == metaType) {
2919 metaType.destruct(data);
2920 metaType.construct(data, result.constData());
2921 return true;
2922 }
2923
2924 if (convertToIterable(metaType, data, sequence))
2925 return true;
2926 }
2927
2928 if (const QV4::ArrayObject *array = value.as<ArrayObject>()) {
2929 if (convertToIterable(metaType, data, array))
2930 return true;
2931 }
2932
2933 return false;
2934}
2935
2936static bool convertToNativeQObject(const QV4::Value &value, QMetaType targetType, void **result)
2937{
2938 if (!(targetType.flags() & QMetaType::IsPointer))
2939 return false;
2940 if (QObject *qobject = qtObjectFromJS(value)) {
2941 // If the target type has a metaObject, use that for casting.
2942 if (const QMetaObject *targetMetaObject = targetType.metaObject()) {
2943 if (QObject *instance = targetMetaObject->cast(qobject)) {
2944 *result = instance;
2945 return true;
2946 }
2947 return false;
2948 }
2949
2950 // We have to call the generated qt_metacast rather than metaObject->cast() here so that
2951 // it works for types without QMetaObject, such as QStandardItem.
2952 const QByteArray targetTypeName = targetType.name();
2953 const int start = targetTypeName.startsWith("const ") ? 6 : 0;
2954 const QByteArray className = targetTypeName.mid(start, targetTypeName.size() - start - 1);
2955 if (void *instance = qobject->qt_metacast(className)) {
2956 *result = instance;
2957 return true;
2958 }
2959 }
2960 return false;
2961}
2962
2963static QObject *qtObjectFromJS(const QV4::Value &value)
2964{
2965 if (!value.isObject())
2966 return nullptr;
2967
2968 QV4::Scope scope(value.as<QV4::Managed>()->engine());
2969 QV4::Scoped<QV4::VariantObject> v(scope, value);
2970
2971 if (v) {
2972 QVariant variant = v->d()->data();
2973 int type = variant.userType();
2974 if (type == QMetaType::QObjectStar)
2975 return *reinterpret_cast<QObject* const *>(variant.constData());
2976 }
2977 QV4::Scoped<QV4::QObjectWrapper> wrapper(scope, value);
2978 if (wrapper)
2979 return wrapper->object();
2980
2981 QV4::Scoped<QV4::QQmlTypeWrapper> typeWrapper(scope, value);
2982 if (typeWrapper)
2983 return typeWrapper->object();
2984
2985 return nullptr;
2986}
2987
2996
2997QMutex *ExecutionEngine::registrationMutex()
2998{
2999 return &registrationData()->mutex;
3000}
3001
3002int ExecutionEngine::registerExtension()
3003{
3004 return registrationData()->extensionCount++;
3005}
3006
3007QT_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