1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtScript 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 "qscriptextensionplugin.h"
41
42#include "qscriptvalue.h"
43#include "qscriptengine.h"
44
45QT_BEGIN_NAMESPACE
46
47/*!
48 \since 4.3
49 \class QScriptExtensionPlugin
50 \brief The QScriptExtensionPlugin class provides an abstract base for custom QScript extension plugins.
51 \inmodule QtScript
52 \ingroup plugins
53
54 QScriptExtensionPlugin is a plugin interface that makes it
55 possible to offer extensions that can be loaded dynamically into
56 applications using the QScriptEngine class.
57
58 Writing a script extension plugin is achieved by subclassing this
59 base class, reimplementing the pure virtual keys() and initialize()
60 functions, and exporting the class using the Q_PLUGIN_METADATA()
61 macro. See \l {How to Create Qt Plugins} for details.
62
63 \sa QScriptEngine::importExtension(), {Creating Qt Script Extensions}
64*/
65
66/*!
67 \fn QStringList QScriptExtensionPlugin::keys() const
68
69 Returns the list of keys this plugin supports.
70
71 These keys are usually the names of the "modules" or "packages"
72 that are implemented in the plugin (e.g. \c{com.mycompany.MyProduct}).
73
74 \sa initialize()
75*/
76
77/*!
78 \fn void QScriptExtensionPlugin::initialize(const QString& key, QScriptEngine *engine)
79
80 Initializes the extension specified by \a key in the given \a engine.
81 The key must come from the set of keys().
82
83 \sa keys()
84*/
85
86/*!
87 Constructs a script extension plugin with the given \a parent.
88
89 Note that this constructor is invoked automatically by the
90 Q_PLUGIN_METADATA() macro, so there is no need for calling it
91 explicitly.
92*/
93QScriptExtensionPlugin::QScriptExtensionPlugin(QObject *parent)
94 : QObject(parent)
95{
96}
97
98/*!
99 Destroys the script extension plugin.
100
101 Note that Qt destroys a plugin automatically when it is no longer
102 used, so there is no need for calling the destructor explicitly.
103*/
104QScriptExtensionPlugin::~QScriptExtensionPlugin()
105{
106}
107
108/*!
109
110 This function is provided for convenience when reimplementing
111 initialize(). It splits the given \a key on \c{'.'} (dot), and
112 ensures that there's a corresponding path of objects in the
113 environment of the given \a engine, creating new objects to complete
114 the path if necessary. E.g. if the key is "com.trolltech", after
115 the call to setupPackage() the script expression \c{com.trolltech}
116 will evaluate to an object. More specifically, the engine's Global
117 Object will have a property called "com", which in turn has a
118 property called "trolltech".
119
120 Use this function to avoid global namespace pollution when installing
121 your extensions in the engine.
122
123 \sa initialize()
124*/
125QScriptValue QScriptExtensionPlugin::setupPackage(
126 const QString &key, QScriptEngine *engine) const
127{
128 QStringList components = key.split(sep: QLatin1Char('.'));
129 QScriptValue o = engine->globalObject();
130 for (int i = 0; i < components.count(); ++i) {
131 QScriptValue oo = o.property(name: components.at(i));
132 if (!oo.isValid()) {
133 oo = engine->newObject();
134 o.setProperty(name: components.at(i), value: oo);
135 }
136 o = oo;
137 }
138 return o;
139}
140
141QT_END_NAMESPACE
142

source code of qtscript/src/script/api/qscriptextensionplugin.cpp