aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4context_p.h
blob: 82a94722239374fd4a9f199fd41c16512f65cb92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QMLJS_ENVIRONMENT_H
#define QMLJS_ENVIRONMENT_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qv4global_p.h"
#include "qv4managed_p.h"

QT_BEGIN_NAMESPACE

namespace QV4 {


namespace Heap {

#define ExecutionContextMembers(class, Member) \
    Member(class, Pointer, ExecutionContext *, outer) \
    Member(class, Pointer, Object *, activation)

DECLARE_HEAP_OBJECT(ExecutionContext, Base) {
    DECLARE_MARKOBJECTS(ExecutionContext)

    enum ContextType {
        Type_GlobalContext = 0x1,
        Type_WithContext = 0x2,
        Type_QmlContext = 0x3,
        Type_BlockContext = 0x4,
        Type_CallContext = 0x5
    };

    void init(ContextType t)
    {
        Base::init();

        type = t;
    }

    const VTable *vtable() const {
        return internalClass->vtable;
    }

    quint32 type : 8;
    quint32 nArgs : 24;
#if QT_POINTER_SIZE == 8
    quint8 padding_[4];
#endif
};
Q_STATIC_ASSERT(std::is_trivial_v<ExecutionContext>);
Q_STATIC_ASSERT(sizeof(ExecutionContext) == sizeof(Base) + sizeof(ExecutionContextData) + QT_POINTER_SIZE);

Q_STATIC_ASSERT(std::is_standard_layout<ExecutionContextData>::value);
Q_STATIC_ASSERT(offsetof(ExecutionContextData, outer) == 0);
Q_STATIC_ASSERT(offsetof(ExecutionContextData, activation) == offsetof(ExecutionContextData, outer) + QT_POINTER_SIZE);

#define CallContextMembers(class, Member) \
    Member(class, Pointer, FunctionObject *, function) \
    Member(class, ValueArray, ValueArray, locals)

DECLARE_HEAP_OBJECT(CallContext, ExecutionContext) {
    DECLARE_MARKOBJECTS(CallContext)

    void init()
    {
        ExecutionContext::init(Type_CallContext);
    }

    int argc() const {
        return static_cast<int>(nArgs);
    }
    const Value *args() const {
        return locals.data() + locals.size;
    }
    void setArg(uint index, Value v);

    template <typename BlockOrFunction>
    void setupLocalTemporalDeadZone(BlockOrFunction *bof) {
        for (uint i = bof->nLocals - bof->sizeOfLocalTemporalDeadZone; i < bof->nLocals; ++i)
            locals.values[i] = Value::emptyValue();
    }
};
Q_STATIC_ASSERT(std::is_trivial_v<CallContext>);
Q_STATIC_ASSERT(std::is_standard_layout<CallContextData>::value);
Q_STATIC_ASSERT(offsetof(CallContextData, function) == 0);
//### The following size check fails on Win8. With the ValueArray at the end of the
// CallContextMembers, it doesn't look very useful.
//#if defined(Q_PROCESSOR_ARM_32) && !defined(Q_OS_IOS)
//Q_STATIC_ASSERT(sizeof(CallContext) == sizeof(ExecutionContext) + sizeof(CallContextData) + QT_POINTER_SIZE);
//#else
//Q_STATIC_ASSERT(sizeof(CallContext) == sizeof(ExecutionContext) + sizeof(CallContextData));
//#endif


}

struct Q_QML_EXPORT ExecutionContext : public Managed
{
    enum {
        IsExecutionContext = true
    };

    V4_MANAGED(ExecutionContext, Managed)
    Q_MANAGED_TYPE(ExecutionContext)
    V4_INTERNALCLASS(ExecutionContext)

    static Heap::CallContext *newBlockContext(QV4::CppStackFrame *frame, int blockIndex);
    static Heap::CallContext *cloneBlockContext(ExecutionEngine *engine,
                                                Heap::CallContext *callContext);
    static Heap::CallContext *newCallContext(JSTypesStackFrame *frame);
    Heap::ExecutionContext *newWithContext(Heap::Object *with) const;
    static Heap::ExecutionContext *newCatchContext(CppStackFrame *frame, int blockIndex, Heap::String *exceptionVarName);

    void createMutableBinding(String *name, bool deletable);

    enum Error {
        NoError,
        TypeError,
        RangeError
    };

    Error setProperty(String *name, const Value &value);

    ReturnedValue getProperty(String *name);
    ReturnedValue getPropertyAndBase(String *name, Value *base);
    bool deleteProperty(String *name);

    inline CallContext *asCallContext();
    inline const CallContext *asCallContext() const;

protected:
    // vtable method required for compilation
    static bool virtualDeleteProperty(Managed *, PropertyKey) {
        Q_UNREACHABLE();
    }
};

struct Q_QML_EXPORT CallContext : public ExecutionContext
{
    V4_MANAGED(CallContext, ExecutionContext)
    V4_INTERNALCLASS(CallContext)

    int argc() const {
        return d()->argc();
    }
    const Value *args() const {
        return d()->args();
    }
};

inline CallContext *ExecutionContext::asCallContext()
{
    return d()->type == Heap::ExecutionContext::Type_CallContext ? static_cast<CallContext *>(this) : nullptr;
}

inline const CallContext *ExecutionContext::asCallContext() const
{
    return d()->type == Heap::ExecutionContext::Type_CallContext ? static_cast<const CallContext *>(this) : nullptr;
}

} // namespace QV4

QT_END_NAMESPACE

#endif