1/****************************************************************************
2**
3** Copyright (C) 2016 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#ifndef QMLJS_ENVIRONMENT_H
40#define QMLJS_ENVIRONMENT_H
41
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include "qv4global_p.h"
54#include "qv4managed_p.h"
55
56QT_BEGIN_NAMESPACE
57
58namespace QV4 {
59
60
61namespace Heap {
62
63#define ExecutionContextMembers(class, Member) \
64 Member(class, Pointer, ExecutionContext *, outer) \
65 Member(class, Pointer, Object *, activation)
66
67DECLARE_HEAP_OBJECT(ExecutionContext, Base) {
68 DECLARE_MARKOBJECTS(ExecutionContext);
69
70 enum ContextType {
71 Type_GlobalContext = 0x1,
72 Type_WithContext = 0x2,
73 Type_QmlContext = 0x3,
74 Type_BlockContext = 0x4,
75 Type_CallContext = 0x5
76 };
77
78 void init(ContextType t)
79 {
80 Base::init();
81
82 type = t;
83 }
84
85 const VTable *vtable() const {
86 return internalClass->vtable;
87 }
88
89 quint32 type : 8;
90 quint32 nArgs : 24;
91#if QT_POINTER_SIZE == 8
92 quint8 padding_[4];
93#endif
94};
95Q_STATIC_ASSERT(std::is_trivial< ExecutionContext >::value);
96Q_STATIC_ASSERT(sizeof(ExecutionContext) == sizeof(Base) + sizeof(ExecutionContextData) + QT_POINTER_SIZE);
97
98Q_STATIC_ASSERT(std::is_standard_layout<ExecutionContextData>::value);
99Q_STATIC_ASSERT(offsetof(ExecutionContextData, outer) == 0);
100Q_STATIC_ASSERT(offsetof(ExecutionContextData, activation) == offsetof(ExecutionContextData, outer) + QT_POINTER_SIZE);
101
102#define CallContextMembers(class, Member) \
103 Member(class, Pointer, FunctionObject *, function) \
104 Member(class, ValueArray, ValueArray, locals)
105
106DECLARE_HEAP_OBJECT(CallContext, ExecutionContext) {
107 DECLARE_MARKOBJECTS(CallContext);
108
109 void init()
110 {
111 ExecutionContext::init(Type_CallContext);
112 }
113
114 int argc() const {
115 return static_cast<int>(nArgs);
116 }
117 const Value *args() const {
118 return locals.data() + locals.size;
119 }
120 void setArg(uint index, Value v);
121
122 template <typename BlockOrFunction>
123 void setupLocalTemporalDeadZone(BlockOrFunction *bof) {
124 for (uint i = bof->nLocals - bof->sizeOfLocalTemporalDeadZone; i < bof->nLocals; ++i)
125 locals.values[i] = Value::emptyValue();
126 }
127};
128Q_STATIC_ASSERT(std::is_trivial< CallContext >::value);
129Q_STATIC_ASSERT(std::is_standard_layout<CallContextData>::value);
130Q_STATIC_ASSERT(offsetof(CallContextData, function) == 0);
131//### The following size check fails on Win8. With the ValueArray at the end of the
132// CallContextMembers, it doesn't look very useful.
133//#if defined(Q_PROCESSOR_ARM_32) && !defined(Q_OS_IOS)
134//Q_STATIC_ASSERT(sizeof(CallContext) == sizeof(ExecutionContext) + sizeof(CallContextData) + QT_POINTER_SIZE);
135//#else
136//Q_STATIC_ASSERT(sizeof(CallContext) == sizeof(ExecutionContext) + sizeof(CallContextData));
137//#endif
138
139
140}
141
142struct Q_QML_EXPORT ExecutionContext : public Managed
143{
144 enum {
145 IsExecutionContext = true
146 };
147
148 V4_MANAGED(ExecutionContext, Managed)
149 Q_MANAGED_TYPE(ExecutionContext)
150 V4_INTERNALCLASS(ExecutionContext)
151
152 static Heap::CallContext *newBlockContext(QV4::CppStackFrame *frame, int blockIndex);
153 static Heap::CallContext *cloneBlockContext(ExecutionEngine *engine,
154 Heap::CallContext *callContext);
155 static Heap::CallContext *newCallContext(QV4::CppStackFrame *frame);
156 Heap::ExecutionContext *newWithContext(Heap::Object *with) const;
157 static Heap::ExecutionContext *newCatchContext(CppStackFrame *frame, int blockIndex, Heap::String *exceptionVarName);
158
159 void createMutableBinding(String *name, bool deletable);
160
161 enum Error {
162 NoError,
163 TypeError,
164 RangeError
165 };
166
167 Error setProperty(String *name, const Value &value);
168
169 ReturnedValue getProperty(String *name);
170 ReturnedValue getPropertyAndBase(String *name, Value *base);
171 bool deleteProperty(String *name);
172
173 inline CallContext *asCallContext();
174 inline const CallContext *asCallContext() const;
175
176protected:
177 // vtable method required for compilation
178 static bool virtualDeleteProperty(Managed *, PropertyKey) {
179 Q_UNREACHABLE();
180 }
181};
182
183struct Q_QML_EXPORT CallContext : public ExecutionContext
184{
185 V4_MANAGED(CallContext, ExecutionContext)
186 V4_INTERNALCLASS(CallContext)
187
188 int argc() const {
189 return d()->argc();
190 }
191 const Value *args() const {
192 return d()->args();
193 }
194};
195
196inline CallContext *ExecutionContext::asCallContext()
197{
198 return d()->type == Heap::ExecutionContext::Type_CallContext ? static_cast<CallContext *>(this) : nullptr;
199}
200
201inline const CallContext *ExecutionContext::asCallContext() const
202{
203 return d()->type == Heap::ExecutionContext::Type_CallContext ? static_cast<const CallContext *>(this) : nullptr;
204}
205
206} // namespace QV4
207
208QT_END_NAMESPACE
209
210#endif
211