Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmlscriptdata.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <private/qqmlscriptdata_p.h>
5#include <private/qqmlcontext_p.h>
6#include <private/qqmlengine_p.h>
7#include <private/qqmlscriptblob_p.h>
8#include <private/qv4engine_p.h>
9#include <private/qv4scopedvalue_p.h>
10#include <private/qv4object_p.h>
11#include <private/qv4qmlcontext_p.h>
12#include <private/qv4module_p.h>
13
15
16QQmlRefPointer<QQmlContextData> QQmlScriptData::qmlContextDataForContext(
17 const QQmlRefPointer<QQmlContextData> &parentQmlContextData)
18{
19 Q_ASSERT(parentQmlContextData && parentQmlContextData->engine());
20
21 if (!m_precompiledScript || m_precompiledScript->isESModule())
22 return nullptr;
23
24 QQmlRefPointer<QQmlContextData> qmlContextData = m_precompiledScript->isSharedLibrary()
25 ? QQmlContextData::createRefCounted(QQmlRefPointer<QQmlContextData>())
26 : QQmlContextData::createRefCounted(parentQmlContextData);
27
28 qmlContextData->setInternal(true);
29 qmlContextData->setJSContext(true);
30 if (m_precompiledScript->isSharedLibrary())
31 qmlContextData->setPragmaLibraryContext(true);
32 else
33 qmlContextData->setPragmaLibraryContext(parentQmlContextData->isPragmaLibraryContext());
34 qmlContextData->setBaseUrl(url);
35 qmlContextData->setBaseUrlString(urlString);
36
37 // For backward compatibility, if there are no imports, we need to use the
38 // imports from the parent context. See QTBUG-17518.
39 if (!typeNameCache->isEmpty()) {
40 qmlContextData->setImports(typeNameCache);
41 } else if (!m_precompiledScript->isSharedLibrary()) {
42 qmlContextData->setImports(parentQmlContextData->imports());
43 qmlContextData->setImportedScripts(parentQmlContextData->importedScripts());
44 }
45
46 if (m_precompiledScript->isSharedLibrary())
47 qmlContextData->setEngine(parentQmlContextData->engine()); // Fix for QTBUG-21620
48
49 QV4::ExecutionEngine *v4 = parentQmlContextData->engine()->handle();
50 QV4::Scope scope(v4);
51 QV4::ScopedObject scriptsArray(scope);
52 if (qmlContextData->importedScripts().isNullOrUndefined()) {
53 scriptsArray = v4->newArrayObject(scripts.size());
54 qmlContextData->setImportedScripts(
55 QV4::PersistentValue(v4, scriptsArray.asReturnedValue()));
56 } else {
57 scriptsArray = qmlContextData->importedScripts().valueRef();
58 }
59 QV4::ScopedValue v(scope);
60 for (int ii = 0; ii < scripts.size(); ++ii) {
61 v = scripts.at(ii)->scriptData()->scriptValueForContext(qmlContextData);
62 scriptsArray->put(ii, v);
63 }
64
65 return qmlContextData;
66}
67
69 const QQmlRefPointer<QQmlContextData> &parentQmlContextData)
70{
71 if (m_loaded)
72 return m_value.value();
73
74 Q_ASSERT(parentQmlContextData && parentQmlContextData->engine());
75 QV4::ExecutionEngine *v4 = parentQmlContextData->engine()->handle();
76 QV4::Scope scope(v4);
77
78 QV4::Scoped<QV4::QmlContext> qmlExecutionContext(scope);
79 if (auto qmlContextData = qmlContextDataForContext(parentQmlContextData)) {
80 qmlExecutionContext = QV4::QmlContext::create(v4->rootContext(), std::move(qmlContextData),
81 /* scopeObject: */ nullptr);
82 }
83
85 scope,
86 v4->executableCompilationUnit(QQmlRefPointer<QV4::CompiledData::CompilationUnit>(
87 m_precompiledScript))->instantiate());
88
89 if (module) {
90 if (qmlExecutionContext) {
91 module->d()->scope->outer.set(v4, qmlExecutionContext->d());
92 qmlExecutionContext->d()->qml()->module.set(v4, module->d());
93 }
94
95 module->evaluate();
96 }
97
98 if (v4->hasException) {
100 if (error.isValid())
101 QQmlEnginePrivate::get(v4)->warning(error);
102 }
103
104 QV4::ScopedValue value(scope);
105 if (qmlExecutionContext)
106 value = qmlExecutionContext->d()->qml();
107 else if (module)
108 value = module->d();
109
110 if (m_precompiledScript->isSharedLibrary() || m_precompiledScript->isESModule()) {
111 m_loaded = true;
112 m_value.set(v4, value);
113 }
114
115 return value->asReturnedValue();
116}
117
void setInternal(bool isInternal)
static QQmlRefPointer< QQmlContextData > createRefCounted(const QQmlRefPointer< QQmlContextData > &parent)
static QQmlEnginePrivate * get(QQmlEngine *e)
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
QVector< QQmlRefPointer< QQmlScriptBlob > > scripts
QV4::ReturnedValue scriptValueForContext(const QQmlRefPointer< QQmlContextData > &parentCtxt)
QQmlRefPointer< QQmlTypeNameCache > typeNameCache
ReturnedValue value() const
void set(ExecutionEngine *engine, const Value &value)
Combined button and popup list for selecting options.
quint64 ReturnedValue
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLsizei const GLfloat * v
[13]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ExecutionContext * rootContext() const
QQmlError catchExceptionAsQmlError()
Heap::ArrayObject * newArrayObject(int count=0)
QQmlRefPointer< ExecutableCompilationUnit > executableCompilationUnit(QQmlRefPointer< QV4::CompiledData::CompilationUnit > &&unit)
static Heap::QmlContext * create(QV4::ExecutionContext *parent, QQmlRefPointer< QQmlContextData > context, QObject *scopeObject)