1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <private/qqmlscriptdata_p.h>
41#include <private/qqmlcontext_p.h>
42#include <private/qqmlengine_p.h>
43#include <private/qqmlscriptblob_p.h>
44#include <private/qv4engine_p.h>
45#include <private/qv4scopedvalue_p.h>
46#include <private/qv4object_p.h>
47#include <private/qv4qmlcontext_p.h>
48#include <private/qv4module_p.h>
49
50QT_BEGIN_NAMESPACE
51
52QQmlScriptData::QQmlScriptData()
53 : typeNameCache(nullptr)
54 , m_loaded(false)
55{
56}
57
58QQmlContextData *QQmlScriptData::qmlContextDataForContext(QQmlContextData *parentQmlContextData)
59{
60 Q_ASSERT(parentQmlContextData && parentQmlContextData->engine);
61
62 if (m_precompiledScript->isESModule())
63 return nullptr;
64
65 auto qmlContextData = new QQmlContextData();
66
67 qmlContextData->isInternal = true;
68 qmlContextData->isJSContext = true;
69 if (m_precompiledScript->isSharedLibrary())
70 qmlContextData->isPragmaLibraryContext = true;
71 else
72 qmlContextData->isPragmaLibraryContext = parentQmlContextData->isPragmaLibraryContext;
73 qmlContextData->baseUrl = url;
74 qmlContextData->baseUrlString = urlString;
75
76 // For backward compatibility, if there are no imports, we need to use the
77 // imports from the parent context. See QTBUG-17518.
78 if (!typeNameCache->isEmpty()) {
79 qmlContextData->imports = typeNameCache;
80 } else if (!m_precompiledScript->isSharedLibrary()) {
81 qmlContextData->imports = parentQmlContextData->imports;
82 qmlContextData->importedScripts = parentQmlContextData->importedScripts;
83 }
84
85 if (!m_precompiledScript->isSharedLibrary()) {
86 qmlContextData->setParent(parentQmlContextData);
87 } else {
88 qmlContextData->engine = parentQmlContextData->engine; // Fix for QTBUG-21620
89 }
90
91 QV4::ExecutionEngine *v4 = parentQmlContextData->engine->handle();
92 QV4::Scope scope(v4);
93 QV4::ScopedObject scriptsArray(scope);
94 if (qmlContextData->importedScripts.isNullOrUndefined()) {
95 scriptsArray = v4->newArrayObject(scripts.count());
96 qmlContextData->importedScripts.set(v4, scriptsArray);
97 } else {
98 scriptsArray = qmlContextData->importedScripts.valueRef();
99 }
100 QV4::ScopedValue v(scope);
101 for (int ii = 0; ii < scripts.count(); ++ii)
102 scriptsArray->put(ii, (v = scripts.at(ii)->scriptData()->scriptValueForContext(qmlContextData)));
103
104 return qmlContextData;
105}
106
107QV4::ReturnedValue QQmlScriptData::scriptValueForContext(QQmlContextData *parentQmlContextData)
108{
109 if (m_loaded)
110 return m_value.value();
111
112 Q_ASSERT(parentQmlContextData && parentQmlContextData->engine);
113 QV4::ExecutionEngine *v4 = parentQmlContextData->engine->handle();
114 QV4::Scope scope(v4);
115
116 if (!hasEngine()) {
117 addToEngine(parentQmlContextData->engine);
118 addref();
119 }
120
121 QQmlContextDataRef qmlContextData = qmlContextDataForContext(parentQmlContextData);
122 QV4::Scoped<QV4::QmlContext> qmlExecutionContext(scope);
123 if (qmlContextData)
124 qmlExecutionContext =
125 QV4::QmlContext::create(v4->rootContext(), qmlContextData, /* scopeObject: */ nullptr);
126
127 QV4::Scoped<QV4::Module> module(scope, m_precompiledScript->instantiate(v4));
128 if (module) {
129 if (qmlContextData) {
130 module->d()->scope->outer.set(v4, qmlExecutionContext->d());
131 qmlExecutionContext->d()->qml()->module.set(v4, module->d());
132 }
133
134 module->evaluate();
135 }
136
137 if (v4->hasException) {
138 QQmlError error = v4->catchExceptionAsQmlError();
139 if (error.isValid())
140 QQmlEnginePrivate::get(v4)->warning(error);
141 }
142
143 QV4::ScopedValue value(scope);
144 if (qmlContextData)
145 value = qmlExecutionContext->d()->qml();
146 else if (module)
147 value = module->d();
148
149 if (m_precompiledScript->isSharedLibrary() || m_precompiledScript->isESModule()) {
150 m_loaded = true;
151 m_value.set(v4, value);
152 }
153
154 return value->asReturnedValue();
155}
156
157void QQmlScriptData::clear()
158{
159 if (typeNameCache) {
160 typeNameCache->release();
161 typeNameCache = nullptr;
162 }
163
164 scripts.clear();
165
166 // An addref() was made when the QQmlCleanup was added to the engine.
167 release();
168}
169
170QT_END_NAMESPACE
171