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