1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtScript module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "config.h" |
41 | #include "qscriptclassobject_p.h" |
42 | |
43 | #include "../api/qscriptengine.h" |
44 | #include "../api/qscriptengine_p.h" |
45 | #include "../api/qscriptcontext.h" |
46 | #include "../api/qscriptcontext_p.h" |
47 | #include "../api/qscriptclass.h" |
48 | #include "../api/qscriptclasspropertyiterator.h" |
49 | |
50 | #include "Error.h" |
51 | #include "PropertyNameArray.h" |
52 | |
53 | Q_DECLARE_METATYPE(QScriptContext*) |
54 | Q_DECLARE_METATYPE(QScriptValue) |
55 | Q_DECLARE_METATYPE(QScriptValueList) |
56 | |
57 | QT_BEGIN_NAMESPACE |
58 | |
59 | namespace QScript |
60 | { |
61 | |
62 | ClassObjectDelegate::ClassObjectDelegate(QScriptClass *scriptClass) |
63 | : m_scriptClass(scriptClass) |
64 | { |
65 | } |
66 | |
67 | ClassObjectDelegate::~ClassObjectDelegate() |
68 | { |
69 | } |
70 | |
71 | QScriptObjectDelegate::Type ClassObjectDelegate::type() const |
72 | { |
73 | return ClassObject; |
74 | } |
75 | |
76 | bool ClassObjectDelegate::getOwnPropertySlot(QScriptObject* object, |
77 | JSC::ExecState *exec, |
78 | const JSC::Identifier &propertyName, |
79 | JSC::PropertySlot &slot) |
80 | { |
81 | QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
82 | QScript::SaveFrameHelper saveFrame(engine, exec); |
83 | // for compatibility with the old back-end, normal JS properties |
84 | // are queried first. |
85 | if (QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot)) |
86 | return true; |
87 | |
88 | QScriptValue scriptObject = engine->scriptValueFromJSCValue(value: object); |
89 | QScriptString scriptName; |
90 | QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
91 | QScriptStringPrivate::init(q&: scriptName, d: &scriptName_d); |
92 | uint id = 0; |
93 | QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
94 | object: scriptObject, name: scriptName, flags: QScriptClass::HandlesReadAccess, id: &id); |
95 | if (flags & QScriptClass::HandlesReadAccess) { |
96 | QScriptValue value = m_scriptClass->property(object: scriptObject, name: scriptName, id); |
97 | if (!value.isValid()) { |
98 | // The class claims to have the property, but returned an invalid |
99 | // value. Silently convert to undefined to avoid the invalid value |
100 | // "escaping" into JS. |
101 | value = QScriptValue(QScriptValue::UndefinedValue); |
102 | } |
103 | slot.setValue(engine->scriptValueToJSCValue(value)); |
104 | return true; |
105 | } |
106 | return false; |
107 | } |
108 | |
109 | bool ClassObjectDelegate::getOwnPropertyDescriptor(QScriptObject *object, |
110 | JSC::ExecState *exec, |
111 | const JSC::Identifier &propertyName, |
112 | JSC::PropertyDescriptor &descriptor) |
113 | { |
114 | QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
115 | QScript::SaveFrameHelper saveFrame(engine, exec); |
116 | // for compatibility with the old back-end, normal JS properties |
117 | // are queried first. |
118 | if (QScriptObjectDelegate::getOwnPropertyDescriptor(object, exec, propertyName, descriptor)) |
119 | return true; |
120 | |
121 | QScriptValue scriptObject = engine->scriptValueFromJSCValue(value: object); |
122 | QScriptString scriptName; |
123 | QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
124 | QScriptStringPrivate::init(q&: scriptName, d: &scriptName_d); |
125 | uint id = 0; |
126 | QScriptClass::QueryFlags qflags = m_scriptClass->queryProperty( |
127 | object: scriptObject, name: scriptName, flags: QScriptClass::HandlesReadAccess, id: &id); |
128 | if (qflags & QScriptClass::HandlesReadAccess) { |
129 | QScriptValue::PropertyFlags pflags = m_scriptClass->propertyFlags(object: scriptObject, name: scriptName, id); |
130 | unsigned attribs = 0; |
131 | if (pflags & QScriptValue::ReadOnly) |
132 | attribs |= JSC::ReadOnly; |
133 | if (pflags & QScriptValue::SkipInEnumeration) |
134 | attribs |= JSC::DontEnum; |
135 | if (pflags & QScriptValue::Undeletable) |
136 | attribs |= JSC::DontDelete; |
137 | if (pflags & QScriptValue::PropertyGetter) |
138 | attribs |= JSC::Getter; |
139 | if (pflags & QScriptValue::PropertySetter) |
140 | attribs |= JSC::Setter; |
141 | attribs |= pflags & QScriptValue::UserRange; |
142 | // Rather than calling the getter, we could return an access descriptor here. |
143 | QScriptValue value = m_scriptClass->property(object: scriptObject, name: scriptName, id); |
144 | if (!value.isValid()) { |
145 | // The class claims to have the property, but returned an invalid |
146 | // value. Silently convert to undefined to avoid the invalid value |
147 | // "escaping" into JS. |
148 | value = QScriptValue(QScriptValue::UndefinedValue); |
149 | } |
150 | descriptor.setDescriptor(value: engine->scriptValueToJSCValue(value), attributes: attribs); |
151 | return true; |
152 | } |
153 | return false; |
154 | } |
155 | |
156 | void ClassObjectDelegate::put(QScriptObject* object, JSC::ExecState *exec, |
157 | const JSC::Identifier &propertyName, |
158 | JSC::JSValue value, JSC::PutPropertySlot &slot) |
159 | { |
160 | QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
161 | QScript::SaveFrameHelper saveFrame(engine, exec); |
162 | QScriptValue scriptObject = engine->scriptValueFromJSCValue(value: object); |
163 | QScriptString scriptName; |
164 | QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
165 | QScriptStringPrivate::init(q&: scriptName, d: &scriptName_d); |
166 | uint id = 0; |
167 | QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
168 | object: scriptObject, name: scriptName, flags: QScriptClass::HandlesWriteAccess, id: &id); |
169 | if (flags & QScriptClass::HandlesWriteAccess) { |
170 | m_scriptClass->setProperty(object&: scriptObject, name: scriptName, id, value: engine->scriptValueFromJSCValue(value)); |
171 | return; |
172 | } |
173 | QScriptObjectDelegate::put(object, exec, propertyName, value, slot); |
174 | } |
175 | |
176 | bool ClassObjectDelegate::deleteProperty(QScriptObject* object, JSC::ExecState *exec, |
177 | const JSC::Identifier &propertyName) |
178 | { |
179 | // ### avoid duplication of put() |
180 | QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
181 | QScript::SaveFrameHelper saveFrame(engine, exec); |
182 | QScriptValue scriptObject = engine->scriptValueFromJSCValue(value: object); |
183 | QScriptString scriptName; |
184 | QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
185 | QScriptStringPrivate::init(q&: scriptName, d: &scriptName_d); |
186 | uint id = 0; |
187 | QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
188 | object: scriptObject, name: scriptName, flags: QScriptClass::HandlesWriteAccess, id: &id); |
189 | if (flags & QScriptClass::HandlesWriteAccess) { |
190 | if (m_scriptClass->propertyFlags(object: scriptObject, name: scriptName, id) & QScriptValue::Undeletable) |
191 | return false; |
192 | m_scriptClass->setProperty(object&: scriptObject, name: scriptName, id, value: QScriptValue()); |
193 | return true; |
194 | } |
195 | return QScriptObjectDelegate::deleteProperty(object, exec, propertyName); |
196 | } |
197 | |
198 | void ClassObjectDelegate::getOwnPropertyNames(QScriptObject* object, JSC::ExecState *exec, |
199 | JSC::PropertyNameArray &propertyNames, |
200 | JSC::EnumerationMode mode) |
201 | { |
202 | // For compatibility with the old back-end, normal JS properties |
203 | // are added first. |
204 | QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, mode); |
205 | |
206 | QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
207 | QScript::SaveFrameHelper saveFrame(engine, exec); |
208 | QScriptValue scriptObject = engine->scriptValueFromJSCValue(value: object); |
209 | QScriptClassPropertyIterator *it = m_scriptClass->newIterator(object: scriptObject); |
210 | if (it != 0) { |
211 | while (it->hasNext()) { |
212 | it->next(); |
213 | QString name = it->name().toString(); |
214 | propertyNames.add(JSC::Identifier(exec, name)); |
215 | } |
216 | delete it; |
217 | } |
218 | } |
219 | |
220 | JSC::CallType ClassObjectDelegate::getCallData(QScriptObject*, JSC::CallData &callData) |
221 | { |
222 | if (!m_scriptClass->supportsExtension(extension: QScriptClass::Callable)) |
223 | return JSC::CallTypeNone; |
224 | callData.native.function = call; |
225 | return JSC::CallTypeHost; |
226 | } |
227 | |
228 | JSC::JSValue JSC_HOST_CALL ClassObjectDelegate::call(JSC::ExecState *exec, JSC::JSObject *callee, |
229 | JSC::JSValue thisValue, const JSC::ArgList &args) |
230 | { |
231 | if (!callee->inherits(info: &QScriptObject::info)) |
232 | return JSC::throwError(exec, JSC::TypeError, message: "callee is not a ClassObject object" ); |
233 | QScriptObject *obj = static_cast<QScriptObject*>(callee); |
234 | QScriptObjectDelegate *delegate = obj->delegate(); |
235 | if (!delegate || (delegate->type() != QScriptObjectDelegate::ClassObject)) |
236 | return JSC::throwError(exec, JSC::TypeError, message: "callee is not a ClassObject object" ); |
237 | |
238 | QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass(); |
239 | QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec); |
240 | |
241 | JSC::ExecState *oldFrame = eng_p->currentFrame; |
242 | eng_p->pushContext(exec, thisObject: thisValue, args, callee); |
243 | QScriptContext *ctx = eng_p->contextForFrame(frame: eng_p->currentFrame); |
244 | QScriptValue scriptObject = eng_p->scriptValueFromJSCValue(value: obj); |
245 | QVariant result = scriptClass->extension(extension: QScriptClass::Callable, argument: QVariant::fromValue(value: ctx)); |
246 | eng_p->popContext(); |
247 | eng_p->currentFrame = oldFrame; |
248 | return QScriptEnginePrivate::jscValueFromVariant(exec, v: result); |
249 | } |
250 | |
251 | JSC::ConstructType ClassObjectDelegate::getConstructData(QScriptObject*, JSC::ConstructData &constructData) |
252 | { |
253 | if (!m_scriptClass->supportsExtension(extension: QScriptClass::Callable)) |
254 | return JSC::ConstructTypeNone; |
255 | constructData.native.function = construct; |
256 | return JSC::ConstructTypeHost; |
257 | } |
258 | |
259 | JSC::JSObject* ClassObjectDelegate::construct(JSC::ExecState *exec, JSC::JSObject *callee, |
260 | const JSC::ArgList &args) |
261 | { |
262 | Q_ASSERT(callee->inherits(&QScriptObject::info)); |
263 | QScriptObject *obj = static_cast<QScriptObject*>(callee); |
264 | QScriptObjectDelegate *delegate = obj->delegate(); |
265 | QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass(); |
266 | |
267 | QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec); |
268 | JSC::ExecState *oldFrame = eng_p->currentFrame; |
269 | eng_p->pushContext(exec, JSC::JSValue(), args, callee, calledAsConstructor: true); |
270 | QScriptContext *ctx = eng_p->contextForFrame(frame: eng_p->currentFrame); |
271 | |
272 | QScriptValue defaultObject = ctx->thisObject(); |
273 | QScriptValue result = qvariant_cast<QScriptValue>(v: scriptClass->extension(extension: QScriptClass::Callable, argument: QVariant::fromValue(value: ctx))); |
274 | if (!result.isObject()) |
275 | result = defaultObject; |
276 | eng_p->popContext(); |
277 | eng_p->currentFrame = oldFrame; |
278 | return JSC::asObject(value: eng_p->scriptValueToJSCValue(value: result)); |
279 | } |
280 | |
281 | bool ClassObjectDelegate::hasInstance(QScriptObject* object, JSC::ExecState *exec, |
282 | JSC::JSValue value, JSC::JSValue proto) |
283 | { |
284 | if (!scriptClass()->supportsExtension(extension: QScriptClass::HasInstance)) |
285 | return QScriptObjectDelegate::hasInstance(object, exec, value, proto); |
286 | QScriptValueList args; |
287 | QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec); |
288 | QScript::SaveFrameHelper saveFrame(eng_p, exec); |
289 | args << eng_p->scriptValueFromJSCValue(value: object) << eng_p->scriptValueFromJSCValue(value); |
290 | QVariant result = scriptClass()->extension(extension: QScriptClass::HasInstance, argument: QVariant::fromValue(value: args)); |
291 | return result.toBool(); |
292 | } |
293 | |
294 | } // namespace QScript |
295 | |
296 | QT_END_NAMESPACE |
297 | |