aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4reflect.cpp
blob: dc9dbe48aed771523d31816140ff8fe079f25c23 (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
// 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

#include "qv4reflect_p.h"
#include "qv4runtimeapi_p.h"
#include "qv4objectproto_p.h"
#include "qv4propertykey_p.h"
#include "qv4objectiterator_p.h"

using namespace QV4;

DEFINE_OBJECT_VTABLE(Reflect);

void Heap::Reflect::init()
{
    Object::init();
    Scope scope(internalClass->engine);
    ScopedObject r(scope, this);

    r->defineDefaultProperty(QStringLiteral("apply"), QV4::Reflect::method_apply, 3);
    r->defineDefaultProperty(QStringLiteral("construct"), QV4::Reflect::method_construct, 2);
    r->defineDefaultProperty(QStringLiteral("defineProperty"), QV4::Reflect::method_defineProperty, 3);
    r->defineDefaultProperty(QStringLiteral("deleteProperty"), QV4::Reflect::method_deleteProperty, 2);
    r->defineDefaultProperty(QStringLiteral("get"), QV4::Reflect::method_get, 2);
    r->defineDefaultProperty(QStringLiteral("getOwnPropertyDescriptor"), QV4::Reflect::method_getOwnPropertyDescriptor, 2);
    r->defineDefaultProperty(QStringLiteral("getPrototypeOf"), QV4::Reflect::method_getPrototypeOf, 1);
    r->defineDefaultProperty(QStringLiteral("has"), QV4::Reflect::method_has, 2);
    r->defineDefaultProperty(QStringLiteral("isExtensible"), QV4::Reflect::method_isExtensible, 1);
    r->defineDefaultProperty(QStringLiteral("ownKeys"), QV4::Reflect::method_ownKeys, 1);
    r->defineDefaultProperty(QStringLiteral("preventExtensions"), QV4::Reflect::method_preventExtensions, 1);
    r->defineDefaultProperty(QStringLiteral("set"), QV4::Reflect::method_set, 3);
    r->defineDefaultProperty(QStringLiteral("setPrototypeOf"), QV4::Reflect::method_setPrototypeOf, 2);
}

struct CallArgs {
    Value *argv;
    int argc;
};

static CallArgs createListFromArrayLike(Scope &scope, const Object *o)
{
    int len = scope.engine->safeForAllocLength(o->getLength());
    if (scope.engine->hasException)
        return {nullptr, 0};

    Value *arguments = scope.alloc(len);

    for (int i = 0; i < len; ++i) {
        arguments[i] = o->get(i);
        if (scope.hasException())
            return { nullptr, 0 };
    }
    return { arguments, len };
}

ReturnedValue Reflect::method_apply(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (argc < 3 || !argv[0].isFunctionObject() || !argv[2].isObject())
        return scope.engine->throwTypeError();

    const Object *o = static_cast<const Object *>(argv + 2);
    CallArgs arguments = createListFromArrayLike(scope, o);
    if (scope.hasException())
        return Encode::undefined();

    return checkedResult(scope.engine, static_cast<const FunctionObject &>(argv[0]).call(
                &argv[1], arguments.argv, arguments.argc));
}

ReturnedValue Reflect::method_construct(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (argc < 2 || !argv[1].isObject())
        return scope.engine->throwTypeError();
    const FunctionObject *target = argv[0].as<FunctionObject>();
    const FunctionObject *newTarget = argc == 3 ? argv[2].as<FunctionObject>() : target;
    if (!target || !target->isConstructor() || !newTarget || !newTarget->isConstructor())
        return scope.engine->throwTypeError();

    const Object *o = static_cast<const Object *>(argv + 1);
    CallArgs arguments = createListFromArrayLike(scope, o);
    if (scope.hasException())
        return Encode::undefined();

    return target->callAsConstructor(arguments.argv, arguments.argc, newTarget);
}

ReturnedValue Reflect::method_defineProperty(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (!argc || !argv[0].isObject())
        return scope.engine->throwTypeError();

    ScopedObject O(scope, argv[0]);
    ScopedPropertyKey name(scope, (argc > 1 ? argv[1] : Value::undefinedValue()).toPropertyKey(scope.engine));
    if (scope.hasException())
        return QV4::Encode::undefined();

    ScopedValue attributes(scope, argc > 2 ? argv[2] : Value::undefinedValue());
    ScopedProperty pd(scope);
    PropertyAttributes attrs;
    ObjectPrototype::toPropertyDescriptor(scope.engine, attributes, pd, &attrs);
    if (scope.hasException())
        return QV4::Encode::undefined();

    bool result = O->defineOwnProperty(name, pd, attrs);

    return Encode(result);
}

ReturnedValue Reflect::method_deleteProperty(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    ExecutionEngine *e = f->engine();
    if (!argc || !argv[0].isObject())
        return e->throwTypeError();

    bool result =  Runtime::DeleteProperty_NoThrow::call(e, argv[0], argc > 1 ? argv[1] : Value::undefinedValue());
    return Encode(result);
}

ReturnedValue Reflect::method_get(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (!argc || !argv[0].isObject())
        return scope.engine->throwTypeError();

    ScopedObject o(scope, static_cast<const Object *>(argv));
    Value undef = Value::undefinedValue();
    const Value *index = argc > 1 ? &argv[1] : &undef;
    ScopedPropertyKey name(scope, index->toPropertyKey(scope.engine));
    if (scope.hasException())
        return Encode::undefined();
    ScopedValue receiver(scope, argc > 2 ? argv[2] : *o);

    return Encode(o->get(name, receiver));
}

ReturnedValue Reflect::method_getOwnPropertyDescriptor(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
{
    if (!argc || !argv[0].isObject())
        return f->engine()->throwTypeError();

    return ObjectPrototype::method_getOwnPropertyDescriptor(f, thisObject, argv, argc);
}

ReturnedValue Reflect::method_getPrototypeOf(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    if (!argc || !argv[0].isObject())
        return f->engine()->throwTypeError();

    const Object *o = static_cast<const Object *>(argv);
    Heap::Object *p = o->getPrototypeOf();
    return (p ? p->asReturnedValue() : Encode::null());
}

ReturnedValue Reflect::method_has(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (!argc || !argv[0].isObject())
        return scope.engine->throwTypeError();

    ScopedObject o(scope, static_cast<const Object *>(argv));
    Value undef = Value::undefinedValue();
    const Value *index = argc > 1 ? &argv[1] : &undef;

    ScopedPropertyKey name(scope, index->toPropertyKey(scope.engine));
    if (scope.hasException())
        return false;

    return Encode(o->hasProperty(name));
}

ReturnedValue Reflect::method_isExtensible(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    if (!argc || !argv[0].isObject())
        return f->engine()->throwTypeError();

    const Object *o = static_cast<const Object *>(argv);
    return Encode(o->isExtensible());
}


ReturnedValue Reflect::method_ownKeys(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    if (!argc || !argv[0].isObject())
        return f->engine()->throwTypeError();

    Scope scope(f);
    if (!argc)
        return scope.engine->throwTypeError();

    ScopedObject O(scope, argv[0].toObject(scope.engine));
    if (!O)
        return Encode::undefined();

    ScopedArrayObject keys(scope, scope.engine->newArrayObject());

    ObjectIterator it(scope, O, ObjectIterator::WithSymbols);
    ScopedPropertyKey key(scope);
    ScopedValue v(scope);
    while (1) {
        key = it.next();
        if (!key->isValid())
            break;
        v = key->toStringOrSymbol(scope.engine);
        keys->push_back(v);
    }

    return keys->asReturnedValue();

}

ReturnedValue Reflect::method_preventExtensions(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (!argc || !argv[0].isObject())
        return scope.engine->throwTypeError();

    ScopedObject o(scope, static_cast<const Object *>(argv));
    return Encode(o->preventExtensions());
}

ReturnedValue Reflect::method_set(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    Scope scope(f);
    if (!argc || !argv[0].isObject())
        return scope.engine->throwTypeError();

    ScopedObject o(scope, static_cast<const Object *>(argv));
    Value undef = Value::undefinedValue();
    const Value *index = argc > 1 ? &argv[1] : &undef;
    const Value &val = argc > 2 ? argv[2] : undef;
    ScopedValue receiver(scope, argc >3 ? argv[3] : argv[0]);

    ScopedPropertyKey propertyKey(scope, index->toPropertyKey(scope.engine));
    if (scope.hasException())
        return false;
    bool result = o->put(propertyKey, val, receiver);
    return Encode(result);
}

ReturnedValue Reflect::method_setPrototypeOf(const FunctionObject *f, const Value *, const Value *argv, int argc)
{
    if (argc < 2 || !argv[0].isObject() || (!argv[1].isNull() && !argv[1].isObject()))
        return f->engine()->throwTypeError();

    Scope scope(f);
    ScopedObject o(scope, static_cast<const Object *>(argv));
    const Object *proto = argv[1].isNull() ? nullptr : static_cast<const Object *>(argv + 1);
    return Encode(o->setPrototypeOf(proto));
}