summaryrefslogtreecommitdiff
path: root/src/qml/jsruntime/qv4argumentsobject.cpp
blob: 94f418cae147c619f392cbaf0be38ae1a2eec315 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qv4argumentsobject_p.h>
#include <qv4alloca_p.h>
#include <qv4scopedvalue_p.h>
#include "qv4string_p.h"

using namespace QV4;

DEFINE_OBJECT_VTABLE(ArgumentsObject);

Heap::ArgumentsObject::ArgumentsObject(QV4::CallContext *context)
    : context(context->d())
    , fullyCreated(false)
{
    Q_ASSERT(vtable() == QV4::ArgumentsObject::staticVTable());

    ExecutionEngine *v4 = context->d()->engine;
    Scope scope(v4);
    Scoped<QV4::ArgumentsObject> args(scope, this);

    args->setArrayType(Heap::ArrayData::Complex);

    if (context->d()->strictMode) {
        Q_ASSERT(CalleePropertyIndex == args->internalClass()->find(context->d()->engine->id_callee()));
        Q_ASSERT(CallerPropertyIndex == args->internalClass()->find(context->d()->engine->id_caller()));
        *args->propertyData(CalleePropertyIndex + QV4::Object::GetterOffset) = v4->thrower();
        *args->propertyData(CalleePropertyIndex + QV4::Object::SetterOffset) = v4->thrower();
        *args->propertyData(CallerPropertyIndex + QV4::Object::GetterOffset) = v4->thrower();
        *args->propertyData(CallerPropertyIndex + QV4::Object::SetterOffset) = v4->thrower();

        args->arrayReserve(context->argc());
        args->arrayPut(0, context->args(), context->argc());
        args->d()->fullyCreated = true;
    } else {
        Q_ASSERT(CalleePropertyIndex == args->internalClass()->find(context->d()->engine->id_callee()));
        *args->propertyData(CalleePropertyIndex) = context->d()->function->asReturnedValue();
    }
    Q_ASSERT(LengthPropertyIndex == args->internalClass()->find(context->d()->engine->id_length()));
    *args->propertyData(LengthPropertyIndex) = Primitive::fromInt32(context->d()->callData->argc);
}

void ArgumentsObject::fullyCreate()
{
    if (fullyCreated())
        return;

    uint argCount = context()->callData->argc;
    uint numAccessors = qMin(context()->function->formalParameterCount(), argCount);
    ArrayData::realloc(this, Heap::ArrayData::Sparse, argCount, true);
    context()->engine->requireArgumentsAccessors(numAccessors);

    Scope scope(engine());
    Scoped<MemberData> md(scope, d()->mappedArguments);
    d()->mappedArguments = md->allocate(engine(), numAccessors);
    for (uint i = 0; i < numAccessors; ++i) {
        d()->mappedArguments->data[i] = context()->callData->args[i];
        arraySet(i, context()->engine->argumentsAccessors + i, Attr_Accessor);
    }
    arrayPut(numAccessors, context()->callData->args + numAccessors, argCount - numAccessors);
    for (uint i = numAccessors; i < argCount; ++i)
        setArrayAttributes(i, Attr_Data);

    d()->fullyCreated = true;
}

bool ArgumentsObject::defineOwnProperty(ExecutionEngine *engine, uint index, const Property *desc, PropertyAttributes attrs)
{
    fullyCreate();

    Scope scope(engine);
    Property *pd = arrayData() ? arrayData()->getProperty(index) : 0;
    ScopedProperty map(scope);
    PropertyAttributes mapAttrs;
    bool isMapped = false;
    uint numAccessors = qMin((int)context()->function->formalParameterCount(), context()->callData->argc);
    if (pd && index < (uint)numAccessors)
        isMapped = arrayData()->attributes(index).isAccessor() &&
                pd->getter() == context()->engine->argumentsAccessors[index].getter();

    if (isMapped) {
        Q_ASSERT(arrayData());
        mapAttrs = arrayData()->attributes(index);
        map->copy(pd, mapAttrs);
        setArrayAttributes(index, Attr_Data);
        pd = arrayData()->getProperty(index);
        pd->value = d()->mappedArguments->data[index];
    }

    bool strict = engine->current->strictMode;
    engine->current->strictMode = false;
    bool result = Object::defineOwnProperty2(scope.engine, index, desc, attrs);
    engine->current->strictMode = strict;

    if (isMapped && attrs.isData()) {
        Q_ASSERT(arrayData());
        ScopedFunctionObject setter(scope, map->setter());
        ScopedCallData callData(scope, 1);
        callData->thisObject = this->asReturnedValue();
        callData->args[0] = desc->value;
        setter->call(callData);

        if (attrs.isWritable()) {
            setArrayAttributes(index, mapAttrs);
            pd = arrayData()->getProperty(index);
            pd->copy(map, mapAttrs);
        }
    }

    if (engine->current->strictMode && !result)
        return engine->throwTypeError();
    return result;
}

ReturnedValue ArgumentsObject::getIndexed(const Managed *m, uint index, bool *hasProperty)
{
    const ArgumentsObject *args = static_cast<const ArgumentsObject *>(m);
    if (args->fullyCreated())
        return Object::getIndexed(m, index, hasProperty);

    if (index < static_cast<uint>(args->context()->callData->argc)) {
        if (hasProperty)
            *hasProperty = true;
        return args->context()->callData->args[index].asReturnedValue();
    }
    if (hasProperty)
        *hasProperty = false;
    return Encode::undefined();
}

void ArgumentsObject::putIndexed(Managed *m, uint index, const Value &value)
{
    ArgumentsObject *args = static_cast<ArgumentsObject *>(m);
    if (!args->fullyCreated() && index >= static_cast<uint>(args->context()->callData->argc))
        args->fullyCreate();

    if (args->fullyCreated()) {
        Object::putIndexed(m, index, value);
        return;
    }

    args->context()->callData->args[index] = value;
}

bool ArgumentsObject::deleteIndexedProperty(Managed *m, uint index)
{
    ArgumentsObject *args = static_cast<ArgumentsObject *>(m);
    if (!args->fullyCreated())
        args->fullyCreate();
    return Object::deleteIndexedProperty(m, index);
}

PropertyAttributes ArgumentsObject::queryIndexed(const Managed *m, uint index)
{
    const ArgumentsObject *args = static_cast<const ArgumentsObject *>(m);
    if (args->fullyCreated())
        return Object::queryIndexed(m, index);

    uint numAccessors = qMin((int)args->context()->function->formalParameterCount(), args->context()->callData->argc);
    uint argCount = args->context()->callData->argc;
    if (index >= argCount)
        return PropertyAttributes();
    if (index >= numAccessors)
        return Attr_Data;
    return Attr_Accessor;
}

DEFINE_OBJECT_VTABLE(ArgumentsGetterFunction);

ReturnedValue ArgumentsGetterFunction::call(const Managed *getter, CallData *callData)
{
    ExecutionEngine *v4 = static_cast<const ArgumentsGetterFunction *>(getter)->engine();
    Scope scope(v4);
    Scoped<ArgumentsGetterFunction> g(scope, static_cast<const ArgumentsGetterFunction *>(getter));
    Scoped<ArgumentsObject> o(scope, callData->thisObject.as<ArgumentsObject>());
    if (!o)
        return v4->throwTypeError();

    Q_ASSERT(g->index() < static_cast<unsigned>(o->context()->callData->argc));
    return o->context()->callData->args[g->index()].asReturnedValue();
}

DEFINE_OBJECT_VTABLE(ArgumentsSetterFunction);

ReturnedValue ArgumentsSetterFunction::call(const Managed *setter, CallData *callData)
{
    ExecutionEngine *v4 = static_cast<const ArgumentsSetterFunction *>(setter)->engine();
    Scope scope(v4);
    Scoped<ArgumentsSetterFunction> s(scope, static_cast<const ArgumentsSetterFunction *>(setter));
    Scoped<ArgumentsObject> o(scope, callData->thisObject.as<ArgumentsObject>());
    if (!o)
        return v4->throwTypeError();

    Q_ASSERT(s->index() < static_cast<unsigned>(o->context()->callData->argc));
    o->context()->callData->args[s->index()] = callData->argc ? callData->args[0].asReturnedValue() : Encode::undefined();
    return Encode::undefined();
}

void ArgumentsObject::markObjects(Heap::Base *that, ExecutionEngine *e)
{
    ArgumentsObject::Data *o = static_cast<ArgumentsObject::Data *>(that);
    if (o->context)
        o->context->mark(e);
    if (o->mappedArguments)
        o->mappedArguments->mark(e);

    Object::markObjects(that, e);
}