Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4reflect.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qv4reflect_p.h"
5#include "qv4runtimeapi_p.h"
6#include "qv4objectproto_p.h"
7#include "qv4propertykey_p.h"
9
10using namespace QV4;
11
13
15{
16 Object::init();
18 ScopedObject r(scope, this);
19
20 r->defineDefaultProperty(QStringLiteral("apply"), QV4::Reflect::method_apply, 3);
21 r->defineDefaultProperty(QStringLiteral("construct"), QV4::Reflect::method_construct, 2);
22 r->defineDefaultProperty(QStringLiteral("defineProperty"), QV4::Reflect::method_defineProperty, 3);
23 r->defineDefaultProperty(QStringLiteral("deleteProperty"), QV4::Reflect::method_deleteProperty, 2);
24 r->defineDefaultProperty(QStringLiteral("get"), QV4::Reflect::method_get, 2);
25 r->defineDefaultProperty(QStringLiteral("getOwnPropertyDescriptor"), QV4::Reflect::method_getOwnPropertyDescriptor, 2);
26 r->defineDefaultProperty(QStringLiteral("getPrototypeOf"), QV4::Reflect::method_getPrototypeOf, 1);
27 r->defineDefaultProperty(QStringLiteral("has"), QV4::Reflect::method_has, 2);
28 r->defineDefaultProperty(QStringLiteral("isExtensible"), QV4::Reflect::method_isExtensible, 1);
29 r->defineDefaultProperty(QStringLiteral("ownKeys"), QV4::Reflect::method_ownKeys, 1);
30 r->defineDefaultProperty(QStringLiteral("preventExtensions"), QV4::Reflect::method_preventExtensions, 1);
31 r->defineDefaultProperty(QStringLiteral("set"), QV4::Reflect::method_set, 3);
32 r->defineDefaultProperty(QStringLiteral("setPrototypeOf"), QV4::Reflect::method_setPrototypeOf, 2);
33}
34
35struct CallArgs {
37 int argc;
38};
39
41{
42 int len = scope.engine->safeForAllocLength(o->getLength());
43 if (scope.engine->hasException)
44 return {nullptr, 0};
45
46 Value *arguments = scope.alloc(len);
47
48 for (int i = 0; i < len; ++i) {
49 arguments[i] = o->get(i);
50 if (scope.hasException())
51 return { nullptr, 0 };
52 }
53 return { arguments, len };
54}
55
56ReturnedValue Reflect::method_apply(const FunctionObject *f, const Value *, const Value *argv, int argc)
57{
58 Scope scope(f);
59 if (argc < 3 || !argv[0].isFunctionObject() || !argv[2].isObject())
60 return scope.engine->throwTypeError();
61
62 const Object *o = static_cast<const Object *>(argv + 2);
64 if (scope.hasException())
65 return Encode::undefined();
66
67 return checkedResult(scope.engine, static_cast<const FunctionObject &>(argv[0]).call(
68 &argv[1], arguments.argv, arguments.argc));
69}
70
71ReturnedValue Reflect::method_construct(const FunctionObject *f, const Value *, const Value *argv, int argc)
72{
73 Scope scope(f);
74 if (argc < 2 || !argv[1].isObject())
75 return scope.engine->throwTypeError();
76 const FunctionObject *target = argv[0].as<FunctionObject>();
77 const FunctionObject *newTarget = argc == 3 ? argv[2].as<FunctionObject>() : target;
78 if (!target || !target->isConstructor() || !newTarget || !newTarget->isConstructor())
79 return scope.engine->throwTypeError();
80
81 const Object *o = static_cast<const Object *>(argv + 1);
83 if (scope.hasException())
84 return Encode::undefined();
85
86 return target->callAsConstructor(arguments.argv, arguments.argc, newTarget);
87}
88
90{
91 Scope scope(f);
92 if (!argc || !argv[0].isObject())
93 return scope.engine->throwTypeError();
94
95 ScopedObject O(scope, argv[0]);
96 ScopedPropertyKey name(scope, (argc > 1 ? argv[1] : Value::undefinedValue()).toPropertyKey(scope.engine));
97 if (scope.hasException())
99
100 ScopedValue attributes(scope, argc > 2 ? argv[2] : Value::undefinedValue());
101 ScopedProperty pd(scope);
103 ObjectPrototype::toPropertyDescriptor(scope.engine, attributes, pd, &attrs);
104 if (scope.hasException())
105 return QV4::Encode::undefined();
106
107 bool result = O->defineOwnProperty(name, pd, attrs);
108
109 return Encode(result);
110}
111
113{
114 ExecutionEngine *e = f->engine();
115 if (!argc || !argv[0].isObject())
116 return e->throwTypeError();
117
118 bool result = Runtime::DeleteProperty_NoThrow::call(e, argv[0], argc > 1 ? argv[1] : Value::undefinedValue());
119 return Encode(result);
120}
121
122ReturnedValue Reflect::method_get(const FunctionObject *f, const Value *, const Value *argv, int argc)
123{
124 Scope scope(f);
125 if (!argc || !argv[0].isObject())
126 return scope.engine->throwTypeError();
127
128 ScopedObject o(scope, static_cast<const Object *>(argv));
130 const Value *index = argc > 1 ? &argv[1] : &undef;
131 ScopedPropertyKey name(scope, index->toPropertyKey(scope.engine));
132 if (scope.hasException())
133 return Encode::undefined();
134 ScopedValue receiver(scope, argc > 2 ? argv[2] : *o);
135
136 return Encode(o->get(name, receiver));
137}
138
139ReturnedValue Reflect::method_getOwnPropertyDescriptor(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
140{
141 if (!argc || !argv[0].isObject())
142 return f->engine()->throwTypeError();
143
144 return ObjectPrototype::method_getOwnPropertyDescriptor(f, thisObject, argv, argc);
145}
146
148{
149 if (!argc || !argv[0].isObject())
150 return f->engine()->throwTypeError();
151
152 const Object *o = static_cast<const Object *>(argv);
153 Heap::Object *p = o->getPrototypeOf();
154 return (p ? p->asReturnedValue() : Encode::null());
155}
156
157ReturnedValue Reflect::method_has(const FunctionObject *f, const Value *, const Value *argv, int argc)
158{
159 Scope scope(f);
160 if (!argc || !argv[0].isObject())
161 return scope.engine->throwTypeError();
162
163 ScopedObject o(scope, static_cast<const Object *>(argv));
165 const Value *index = argc > 1 ? &argv[1] : &undef;
166
167 ScopedPropertyKey name(scope, index->toPropertyKey(scope.engine));
168 if (scope.hasException())
169 return false;
170
171 return Encode(o->hasProperty(name));
172}
173
175{
176 if (!argc || !argv[0].isObject())
177 return f->engine()->throwTypeError();
178
179 const Object *o = static_cast<const Object *>(argv);
180 return Encode(o->isExtensible());
181}
182
183
184ReturnedValue Reflect::method_ownKeys(const FunctionObject *f, const Value *, const Value *argv, int argc)
185{
186 if (!argc || !argv[0].isObject())
187 return f->engine()->throwTypeError();
188
189 Scope scope(f);
190 if (!argc)
191 return scope.engine->throwTypeError();
192
193 ScopedObject O(scope, argv[0].toObject(scope.engine));
194 if (!O)
195 return Encode::undefined();
196
198
200 ScopedPropertyKey key(scope);
201 ScopedValue v(scope);
202 while (1) {
203 key = it.next();
204 if (!key->isValid())
205 break;
206 v = key->toStringOrSymbol(scope.engine);
207 keys->push_back(v);
208 }
209
210 return keys->asReturnedValue();
211
212}
213
215{
216 Scope scope(f);
217 if (!argc || !argv[0].isObject())
218 return scope.engine->throwTypeError();
219
220 ScopedObject o(scope, static_cast<const Object *>(argv));
221 return Encode(o->preventExtensions());
222}
223
224ReturnedValue Reflect::method_set(const FunctionObject *f, const Value *, const Value *argv, int argc)
225{
226 Scope scope(f);
227 if (!argc || !argv[0].isObject())
228 return scope.engine->throwTypeError();
229
230 ScopedObject o(scope, static_cast<const Object *>(argv));
232 const Value *index = argc > 1 ? &argv[1] : &undef;
233 const Value &val = argc > 2 ? argv[2] : undef;
234 ScopedValue receiver(scope, argc >3 ? argv[3] : argv[0]);
235
236 ScopedPropertyKey propertyKey(scope, index->toPropertyKey(scope.engine));
237 if (scope.hasException())
238 return false;
239 bool result = o->put(propertyKey, val, receiver);
240 return Encode(result);
241}
242
244{
245 if (argc < 2 || !argv[0].isObject() || (!argv[1].isNull() && !argv[1].isObject()))
246 return f->engine()->throwTypeError();
247
248 Scope scope(f);
249 ScopedObject o(scope, static_cast<const Object *>(argv));
250 const Object *proto = argv[1].isNull() ? nullptr : static_cast<const Object *>(argv + 1);
251 return Encode(o->setPrototypeOf(proto));
252}
Definition main.cpp:8
QSet< QString >::iterator it
QList< QVariant > arguments
quint64 ReturnedValue
ReturnedValue checkedResult(QV4::ExecutionEngine *v4, ReturnedValue result)
static struct AttrInfo attrs[]
GLsizei const GLfloat * v
[13]
GLuint64 key
GLuint index
[2]
GLboolean r
[2]
GLfloat GLfloat f
GLenum target
GLuint name
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
GLenum GLsizei len
#define QStringLiteral(str)
static CallArgs createListFromArrayLike(Scope &scope, const Object *o)
#define DEFINE_OBJECT_VTABLE(classname)
QStringList keys
QObject::connect nullptr
Value * argv
static constexpr ReturnedValue undefined()
static constexpr ReturnedValue null()
int safeForAllocLength(qint64 len64)
Heap::ArrayObject * newArrayObject(int count=0)
ReturnedValue throwTypeError()
ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const
Heap::InternalClass * internalClass() const
static ReturnedValue method_getOwnPropertyDescriptor(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static void toPropertyDescriptor(ExecutionEngine *engine, const Value &v, Property *desc, PropertyAttributes *attrs)
static ReturnedValue method_isExtensible(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_get(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_defineProperty(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_setPrototypeOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_getOwnPropertyDescriptor(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_has(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_ownKeys(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_construct(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_apply(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_getPrototypeOf(const FunctionObject *, const Value *, const Value *argv, int argc)
static ReturnedValue method_preventExtensions(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_deleteProperty(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static Bool call(ExecutionEngine *, const Value &, const Value &)
Value * alloc(qint64 nValues) const =delete
bool hasException() const
ExecutionEngine * engine
bool isFunctionObject() const
Definition qv4value_p.h:309
QV4::PropertyKey toPropertyKey(ExecutionEngine *e) const
Definition qv4value.cpp:207
static constexpr Value undefinedValue()
Definition qv4value_p.h:191
const T * as() const
Definition qv4value_p.h:132
Heap::Object * toObject(ExecutionEngine *e) const
Definition qv4value_p.h:122
bool isObject() const
Definition qv4value_p.h:302