aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4stackframe_p.h
blob: f24b0b243376e8528e9e765204a2bcbcde23122d (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (C) 2018 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 QV4STACKFRAME_H
#define QV4STACKFRAME_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 <private/qv4scopedvalue_p.h>
#include <private/qv4context_p.h>
#include <private/qv4enginebase_p.h>
#include <private/qv4calldata_p.h>
#include <private/qv4function_p.h>

#include <type_traits>

QT_BEGIN_NAMESPACE

namespace QV4 {

struct CppStackFrame;
struct Q_QML_EXPORT CppStackFrameBase
{
    enum class Kind : quint8 { JS, Meta };

    CppStackFrame *parent;
    Function *v4Function;
    int originalArgumentsCount;
    int instructionPointer;

    QT_WARNING_PUSH
    QT_WARNING_DISABLE_MSVC(4201) // nonstandard extension used: nameless struct/union
    union {
        struct {
            Value *savedStackTop;
            CallData *jsFrame;
            const Value *originalArguments;
            const char *yield;
            const char *unwindHandler;
            const char *unwindLabel;
            int unwindLevel;
            bool yieldIsIterator;
            bool callerCanHandleTailCall;
            bool pendingTailCall;
            bool isTailCalling;
        };
        struct {
            ExecutionContext *context;
            QObject *thisObject;
            const QMetaType *metaTypes;
            void **returnAndArgs;
            bool returnValueIsUndefined;
        };
    };
    QT_WARNING_POP

    Kind kind;
};

struct Q_QML_EXPORT CppStackFrame : protected CppStackFrameBase
{
    // We want to have those public but we can't declare them as public without making the struct
    // non-standard layout. So we have this other struct with "using" in between.
    using CppStackFrameBase::instructionPointer;
    using CppStackFrameBase::v4Function;

    void init(Function *v4Function, int argc, Kind kind) {
        this->v4Function = v4Function;
        originalArgumentsCount = argc;
        instructionPointer = 0;
        this->kind = kind;
    }

    bool isJSTypesFrame() const { return kind == Kind::JS; }
    bool isMetaTypesFrame() const { return kind == Kind::Meta; }

    QString source() const;
    QString function() const;
    int lineNumber() const;
    int statementNumber() const;

    int missingLineNumber() const;

    CppStackFrame *parentFrame() const { return parent; }
    void setParentFrame(CppStackFrame *parentFrame) { parent = parentFrame; }

    int argc() const { return originalArgumentsCount; }

    inline ExecutionContext *context() const;

    Heap::CallContext *callContext() const { return callContext(context()->d()); }
    ReturnedValue thisObject() const;

protected:
    CppStackFrame() = default;

    void push(EngineBase *engine)
    {
        Q_ASSERT(kind == Kind::JS || kind == Kind::Meta);
        parent = engine->currentStackFrame;
        engine->currentStackFrame = this;
    }

    void pop(EngineBase *engine)
    {
        engine->currentStackFrame = parent;
    }

    Heap::CallContext *callContext(Heap::ExecutionContext *ctx) const
    {
        while (ctx->type != Heap::ExecutionContext::Type_CallContext)
            ctx = ctx->outer;
        return static_cast<Heap::CallContext *>(ctx);
    }
};

struct Q_QML_EXPORT MetaTypesStackFrame : public CppStackFrame
{
    using CppStackFrame::push;
    using CppStackFrame::pop;

    void init(Function *v4Function, QObject *thisObject, ExecutionContext *context,
              void **returnAndArgs, const QMetaType *metaTypes, int argc)
    {
        CppStackFrame::init(v4Function, argc, Kind::Meta);
        CppStackFrameBase::thisObject = thisObject;
        CppStackFrameBase::context = context;
        CppStackFrameBase::metaTypes = metaTypes;
        CppStackFrameBase::returnAndArgs = returnAndArgs;
        CppStackFrameBase::returnValueIsUndefined = false;
    }

    QMetaType returnType() const { return metaTypes[0]; }
    void *returnValue() const { return returnAndArgs[0]; }

    bool isReturnValueUndefined() const { return CppStackFrameBase::returnValueIsUndefined; }
    void setReturnValueUndefined() { CppStackFrameBase::returnValueIsUndefined = true; }

    const QMetaType *argTypes() const { return metaTypes + 1; }
    void **argv() const { return returnAndArgs + 1; }

    const QMetaType *returnAndArgTypes() const { return metaTypes; }
    void **returnAndArgValues() const { return returnAndArgs; }

    QObject *thisObject() const { return CppStackFrameBase::thisObject; }

    ExecutionContext *context() const { return CppStackFrameBase::context; }
    void setContext(ExecutionContext *context) { CppStackFrameBase::context = context; }

    Heap::CallContext *callContext() const
    {
        return CppStackFrame::callContext(CppStackFrameBase::context->d());
    }
};

struct Q_QML_EXPORT JSTypesStackFrame : public CppStackFrame
{
    using CppStackFrame::jsFrame;

    // The JIT needs to poke directly into those using offsetof
    using CppStackFrame::unwindHandler;
    using CppStackFrame::unwindLabel;
    using CppStackFrame::unwindLevel;

    void init(Function *v4Function, const Value *argv, int argc,
              bool callerCanHandleTailCall = false)
    {
        CppStackFrame::init(v4Function, argc, Kind::JS);
        CppStackFrame::originalArguments = argv;
        CppStackFrame::yield = nullptr;
        CppStackFrame::unwindHandler = nullptr;
        CppStackFrame::yieldIsIterator = false;
        CppStackFrame::callerCanHandleTailCall = callerCanHandleTailCall;
        CppStackFrame::pendingTailCall = false;
        CppStackFrame::isTailCalling = false;
        CppStackFrame::unwindLabel = nullptr;
        CppStackFrame::unwindLevel = 0;
    }

    const Value *argv() const { return originalArguments; }

    static uint requiredJSStackFrameSize(uint nRegisters) {
        return CallData::HeaderSize() + nRegisters;
    }
    static uint requiredJSStackFrameSize(Function *v4Function) {
        return CallData::HeaderSize() + v4Function->compiledFunction->nRegisters;
    }
    uint requiredJSStackFrameSize() const {
        return requiredJSStackFrameSize(v4Function);
    }

    void setupJSFrame(Value *stackSpace, const Value &function, const Heap::ExecutionContext *scope,
                      const Value &thisObject, const Value &newTarget = Value::undefinedValue()) {
        setupJSFrame(stackSpace, function, scope, thisObject, newTarget,
                     v4Function->compiledFunction->nFormals,
                     v4Function->compiledFunction->nRegisters);
    }

    void setupJSFrame(
            Value *stackSpace, const Value &function, const Heap::ExecutionContext *scope,
            const Value &thisObject, const Value &newTarget, uint nFormals, uint nRegisters)
    {
        jsFrame = reinterpret_cast<CallData *>(stackSpace);
        jsFrame->function = function;
        jsFrame->context = scope->asReturnedValue();
        jsFrame->accumulator = Encode::undefined();
        jsFrame->thisObject = thisObject;
        jsFrame->newTarget = newTarget;

        uint argc = uint(originalArgumentsCount);
        if (argc > nFormals)
            argc = nFormals;
        jsFrame->setArgc(argc);

        // memcpy requires non-null ptr, even if  argc * sizeof(Value) == 0
        if (originalArguments)
            memcpy(jsFrame->args, originalArguments, argc * sizeof(Value));
        Q_STATIC_ASSERT(Encode::undefined() == 0);
        memset(jsFrame->args + argc, 0, (nRegisters - argc) * sizeof(Value));

        if (v4Function && v4Function->compiledFunction) {
            const int firstDeadZoneRegister
                    = v4Function->compiledFunction->firstTemporalDeadZoneRegister;
            const int registerDeadZoneSize
                    = v4Function->compiledFunction->sizeOfRegisterTemporalDeadZone;

            const Value * tdzEnd = stackSpace + firstDeadZoneRegister + registerDeadZoneSize;
            for (Value *v = stackSpace + firstDeadZoneRegister; v < tdzEnd; ++v)
                *v = Value::emptyValue().asReturnedValue();
        }
    }

    ExecutionContext *context() const
    {
        return static_cast<ExecutionContext *>(&jsFrame->context);
    }

    void setContext(ExecutionContext *context)
    {
        jsFrame->context = context;
    }

    Heap::CallContext *callContext() const
    {
        return CppStackFrame::callContext(static_cast<ExecutionContext &>(jsFrame->context).d());
    }

    bool isTailCalling() const { return CppStackFrame::isTailCalling; }
    void setTailCalling(bool tailCalling) { CppStackFrame::isTailCalling = tailCalling; }

    bool pendingTailCall() const { return CppStackFrame::pendingTailCall; }
    void setPendingTailCall(bool pending) { CppStackFrame::pendingTailCall = pending; }

    const char *yield() const { return CppStackFrame::yield; }
    void setYield(const char *yield) { CppStackFrame::yield = yield; }

    bool yieldIsIterator() const { return CppStackFrame::yieldIsIterator; }
    void setYieldIsIterator(bool isIter) { CppStackFrame::yieldIsIterator = isIter; }

    bool callerCanHandleTailCall() const { return CppStackFrame::callerCanHandleTailCall; }

    ReturnedValue thisObject() const
    {
        return jsFrame->thisObject.asReturnedValue();
    }

    Value *framePointer() const { return savedStackTop; }

    void push(EngineBase *engine) {
        CppStackFrame::push(engine);
        savedStackTop = engine->jsStackTop;
    }

    void pop(EngineBase *engine) {
        CppStackFrame::pop(engine);
        engine->jsStackTop = savedStackTop;
    }
};

inline ExecutionContext *CppStackFrame::context() const
{
    if (isJSTypesFrame())
        return static_cast<const JSTypesStackFrame *>(this)->context();

    Q_ASSERT(isMetaTypesFrame());
    return static_cast<const MetaTypesStackFrame *>(this)->context();
}

struct ScopedStackFrame
{
    ScopedStackFrame(const Scope &scope, ExecutionContext *context)
        : engine(scope.engine)
    {
        if (auto currentFrame = engine->currentStackFrame) {
            frame.init(currentFrame->v4Function, nullptr, context, nullptr, nullptr, 0);
            frame.instructionPointer = currentFrame->instructionPointer;
        } else {
            frame.init(nullptr, nullptr, context, nullptr, nullptr, 0);
        }
        frame.push(engine);
    }

    ~ScopedStackFrame()
    {
        frame.pop(engine);
    }

private:
    ExecutionEngine *engine = nullptr;
    MetaTypesStackFrame frame;
};

Q_STATIC_ASSERT(sizeof(CppStackFrame) == sizeof(JSTypesStackFrame));
Q_STATIC_ASSERT(sizeof(CppStackFrame) == sizeof(MetaTypesStackFrame));
Q_STATIC_ASSERT(std::is_standard_layout_v<CppStackFrame>);
Q_STATIC_ASSERT(std::is_standard_layout_v<JSTypesStackFrame>);
Q_STATIC_ASSERT(std::is_standard_layout_v<MetaTypesStackFrame>);

}

QT_END_NAMESPACE

#endif