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 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | QQmlScriptData::QQmlScriptData() |
53 | : typeNameCache(nullptr) |
54 | , m_loaded(false) |
55 | { |
56 | } |
57 | |
58 | QQmlContextData *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(count: scripts.count()); |
96 | qmlContextData->importedScripts.set(engine: v4, value: 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(idx: ii, v: (v = scripts.at(i: ii)->scriptData()->scriptValueForContext(parentCtxt: qmlContextData))); |
103 | |
104 | return qmlContextData; |
105 | } |
106 | |
107 | QV4::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 | |
117 | QQmlContextDataRef qmlContextData = qmlContextDataForContext(parentQmlContextData); |
118 | QV4::Scoped<QV4::QmlContext> qmlExecutionContext(scope); |
119 | if (qmlContextData) |
120 | qmlExecutionContext = |
121 | QV4::QmlContext::create(parent: v4->rootContext(), context: qmlContextData, /* scopeObject: */ scopeObject: nullptr); |
122 | |
123 | QV4::Scoped<QV4::Module> module(scope, m_precompiledScript->instantiate(engine: v4)); |
124 | if (module) { |
125 | if (qmlContextData) { |
126 | module->d()->scope->outer.set(e: v4, newVal: qmlExecutionContext->d()); |
127 | qmlExecutionContext->d()->qml()->module.set(e: v4, newVal: module->d()); |
128 | } |
129 | |
130 | module->evaluate(); |
131 | } |
132 | |
133 | if (v4->hasException) { |
134 | QQmlError error = v4->catchExceptionAsQmlError(); |
135 | if (error.isValid()) |
136 | QQmlEnginePrivate::get(e: v4)->warning(error); |
137 | } |
138 | |
139 | QV4::ScopedValue value(scope); |
140 | if (qmlContextData) |
141 | value = qmlExecutionContext->d()->qml(); |
142 | else if (module) |
143 | value = module->d(); |
144 | |
145 | if (m_precompiledScript->isSharedLibrary() || m_precompiledScript->isESModule()) { |
146 | m_loaded = true; |
147 | m_value.set(engine: v4, value); |
148 | } |
149 | |
150 | return value->asReturnedValue(); |
151 | } |
152 | |
153 | QT_END_NAMESPACE |
154 | |