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 "qscriptable.h"
41#include "qscriptable_p.h"
42#include "qscriptengine.h"
43
44QT_BEGIN_NAMESPACE
45
46/*!
47 \since 4.3
48 \class QScriptable
49 \inmodule QtScript
50
51 \brief The QScriptable class provides access to the Qt Script environment from Qt C++ member functions.
52
53 \ingroup script
54
55
56 With QScriptEngine::newQObject(), you can expose the signals and
57 slots and properties of any QObject (or subclass) to script
58 code. QScriptable augments this functionality by giving your C++
59 members access to the Qt Script environment they are invoked in;
60 conceptually, it is similar to QObject::sender().
61
62 By subclassing QScriptable, you get the following functions in your
63 class: thisObject(), argumentCount(), argument(), context() and
64 engine(). With these functions, you have full access to the Qt
65 Script environment from the slots and property access functions of
66 your class, when they are invoked from script code.
67
68 For example, you can throw a Qt Script exception from a slot;
69 manipulate the `this' object associated with the function call;
70 inspect the arguments stored in the QScriptContext to know the
71 "real" arguments passed to the function from script code; and call
72 script functions from your slot.
73
74 A typical use case of QScriptable is to implement prototype objects
75 for custom C++ types. You define the scriptable interface of your
76 custom type in a QScriptable subclass using properties and slots;
77 then you wrap an instance of your class using
78 QScriptEngine::newQObject(), and finally pass the result to
79 QScriptEngine::setDefaultPrototype(). See the \l{Default Prototypes Example}
80 to see how this can be done.
81
82 The following is what subclassing QScriptable typically looks
83 like:
84
85 \snippet code/src_script_qscriptable.cpp 0
86
87 The only difference from regular QObject subclassing is that you
88 also inherit from QScriptable.
89
90 In the implementation of your slots, you can then use the functions
91 inherited from QScriptable:
92
93 \snippet code/src_script_qscriptable.cpp 1
94
95 \sa {Default Prototypes Example}, QScriptEngine::newFunction()
96*/
97
98/*!
99 \internal
100*/
101QScriptable::QScriptable()
102 : d_ptr(new QScriptablePrivate())
103{
104 d_ptr->q_ptr = this;
105}
106
107/*!
108 \internal
109*/
110QScriptable::~QScriptable()
111{
112}
113
114/*!
115 Returns a pointer to the QScriptEngine associated with the current
116 Qt function call, or 0 if the Qt function was not invoked from
117 script code.
118*/
119QScriptEngine *QScriptable::engine() const
120{
121 Q_D(const QScriptable);
122 return d->engine;
123}
124
125/*!
126 Returns a pointer to the QScriptContext associated with the current
127 Qt function call, or 0 if the Qt function was not invoked from
128 script code.
129*/
130QScriptContext *QScriptable::context() const
131{
132 if (QScriptEngine *e = engine())
133 return e->currentContext();
134
135 return 0;
136}
137
138/*!
139 Returns the `this' object associated with the current Qt function
140 call, or an invalid QScriptValue if the Qt function was not invoked
141 from script code.
142*/
143
144QScriptValue QScriptable::thisObject() const
145{
146 if (QScriptContext *c = context())
147 return c->thisObject();
148
149 return QScriptValue();
150}
151
152/*!
153 Returns the number of arguments passed to the function in this
154 invocation, or -1 if the Qt function was not invoked from script
155 code.
156
157 \sa argument()
158*/
159int QScriptable::argumentCount() const
160{
161 if (QScriptContext *c = context())
162 return c->argumentCount();
163
164 return -1;
165}
166
167/*!
168 Returns the function argument at the given \a index, or an invalid
169 QScriptValue if the Qt function was not invoked from script code.
170
171 \sa argumentCount()
172*/
173QScriptValue QScriptable::argument(int index) const
174{
175 if (QScriptContext *c = context())
176 return c->argument(index);
177
178 return QScriptValue();
179}
180
181QT_END_NAMESPACE
182

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