6a22e19d8fbb0587eb10ceda2ac7b438b9c9cba3
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qscriptisolate_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtScript module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file.  Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** If you have questions regarding the use of this file, please contact
19 ** us via http://www.qt-project.org/.
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23
24 #ifndef APIPREAMBLE_P_H
25 #define APIPREAMBLE_P_H
26
27 #include <private/qv8_p.h>
28 #include "qv8engine_p.h"
29
30 QT_BEGIN_NAMESPACE
31
32 /**
33   \internal
34   Class used to switch to the right isolate. It does the same thing as v8::Isolate::Scope but
35   it checks for a null engine.
36   \attention We decided to put context switching "up" which means that it should be as high
37   as possible on call stack. And it should be switched at most once per public API function call.
38 */
39 class QScriptIsolate {
40 public:
41     // OperationMode was introduced to reduce number of checking for a null engine pointer. If we
42     // know that given pointer is not null than we should pass NotNullEngine as constructor argument
43     // that would nicely remove checking on compilation time.
44     enum OperationMode {Default, NotNullEngine};
45     inline QScriptIsolate(const QV8Engine *engine, const OperationMode mode = Default)
46         : m_engine(engine)
47         , m_mode(mode)
48     {
49         if (m_mode == NotNullEngine || m_engine) {
50             Q_ASSERT(m_engine);
51             m_engine->context()->Enter();
52         }
53     }
54
55     inline ~QScriptIsolate()
56     {
57         if (m_mode == NotNullEngine || m_engine) {
58             m_engine->context()->Exit();
59         }
60     }
61
62 private:
63     Q_DISABLE_COPY(QScriptIsolate);
64     const QV8Engine *m_engine;
65     const OperationMode m_mode;
66 };
67
68
69 QT_END_NAMESPACE
70
71 #endif // APIPREAMBLE_P_H