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-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file.  Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt.io/contact-us/.
19 **
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23 
24 #include "qscriptextensionplugin.h"
25 
26 #include "qscriptvalue.h"
27 #include "qscriptengine.h"
28 
29 QT_BEGIN_NAMESPACE
30 
31 /*!
32     \since 4.3
33     \class QScriptExtensionPlugin
34     \brief The QScriptExtensionPlugin class provides an abstract base for custom QScript extension plugins.
35 
36     \ingroup plugins
37 
38     QScriptExtensionPlugin is a plugin interface that makes it
39     possible to offer extensions that can be loaded dynamically into
40     applications using the QScriptEngine class.
41 
42     Writing a script extension plugin is achieved by subclassing this
43     base class, reimplementing the pure virtual keys() and initialize()
44     functions, and exporting the class using the Q_EXPORT_PLUGIN2()
45     macro. See \l {How to Create Qt Plugins} for details.
46 
47     \sa QScriptEngine::importExtension(), {Creating QtScript Extensions}
48 */
49 
50 /*!
51     \fn QStringList QScriptExtensionPlugin::keys() const
52 
53     Returns the list of keys this plugin supports.
54 
55     These keys are usually the names of the "modules" or "packages"
56     that are implemented in the plugin (e.g. \c{com.mycompany.MyProduct}).
57 
58     \sa initialize()
59 */
60 
61 /*!
62     \fn void QScriptExtensionPlugin::initialize(const QString& key, QScriptEngine *engine)
63 
64     Initializes the extension specified by \a key in the given \a engine.
65     The key must come from the set of keys().
66 
67     \sa keys()
68 */
69 
70 /*!
71     Constructs a script extension plugin with the given \a parent.
72 
73     Note that this constructor is invoked automatically by the
74     Q_EXPORT_PLUGIN2() macro, so there is no need for calling it
75     explicitly.
76 */
QScriptExtensionPlugin(QObject * parent)77 QScriptExtensionPlugin::QScriptExtensionPlugin(QObject *parent)
78     : QObject(parent)
79 {
80 }
81 
82 /*!
83     Destroys the script extension plugin.
84 
85     Note that Qt destroys a plugin automatically when it is no longer
86     used, so there is no need for calling the destructor explicitly.
87 */
~QScriptExtensionPlugin()88 QScriptExtensionPlugin::~QScriptExtensionPlugin()
89 {
90 }
91 
92 /*!
93 
94   This function is provided for convenience when reimplementing
95   initialize(). It splits the given \a key on \c{'.'} (dot), and
96   ensures that there's a corresponding path of objects in the
97   environment of the given \a engine, creating new objects to complete
98   the path if necessary.  E.g. if the key is "com.trolltech", after
99   the call to setupPackage() the script expression \c{com.trolltech}
100   will evaluate to an object. More specifically, the engine's Global
101   Object will have a property called "com", which in turn has a
102   property called "trolltech".
103 
104   Use this function to avoid global namespace pollution when installing
105   your extensions in the engine.
106 
107   \sa initialize()
108 */
setupPackage(const QString & key,QScriptEngine * engine) const109 QScriptValue QScriptExtensionPlugin::setupPackage(
110     const QString &key, QScriptEngine *engine) const
111 {
112     QStringList components = key.split(QLatin1Char('.'));
113     QScriptValue o = engine->globalObject();
114     for (int i = 0; i < components.count(); ++i) {
115         QScriptValue oo = o.property(components.at(i));
116         if (!oo.isValid()) {
117             oo = engine->newObject();
118             o.setProperty(components.at(i), oo);
119         }
120         o = oo;
121     }
122     return o;
123 }
124 
125 QT_END_NAMESPACE
126