|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtScript module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "config.h" |
|
43 #include "qscriptclassobject_p.h" |
|
44 |
|
45 #include "../api/qscriptengine.h" |
|
46 #include "../api/qscriptengine_p.h" |
|
47 #include "../api/qscriptcontext.h" |
|
48 #include "../api/qscriptcontext_p.h" |
|
49 #include "../api/qscriptclass.h" |
|
50 #include "../api/qscriptclasspropertyiterator.h" |
|
51 |
|
52 #include "Error.h" |
|
53 #include "PropertyNameArray.h" |
|
54 |
|
55 Q_DECLARE_METATYPE(QScriptContext*) |
|
56 Q_DECLARE_METATYPE(QScriptValue) |
|
57 Q_DECLARE_METATYPE(QScriptValueList) |
|
58 |
|
59 QT_BEGIN_NAMESPACE |
|
60 |
|
61 namespace QScript |
|
62 { |
|
63 |
|
64 ClassObjectDelegate::ClassObjectDelegate(QScriptClass *scriptClass) |
|
65 : m_scriptClass(scriptClass) |
|
66 { |
|
67 } |
|
68 |
|
69 ClassObjectDelegate::~ClassObjectDelegate() |
|
70 { |
|
71 } |
|
72 |
|
73 QScriptObjectDelegate::Type ClassObjectDelegate::type() const |
|
74 { |
|
75 return ClassObject; |
|
76 } |
|
77 |
|
78 bool ClassObjectDelegate::getOwnPropertySlot(QScriptObject* object, |
|
79 JSC::ExecState *exec, |
|
80 const JSC::Identifier &propertyName, |
|
81 JSC::PropertySlot &slot) |
|
82 { |
|
83 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
84 QScript::SaveFrameHelper saveFrame(engine, exec); |
|
85 // for compatibility with the old back-end, normal JS properties |
|
86 // are queried first. |
|
87 if (QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot)) |
|
88 return true; |
|
89 |
|
90 QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); |
|
91 QScriptString scriptName; |
|
92 QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
|
93 QScriptStringPrivate::init(scriptName, &scriptName_d); |
|
94 uint id = 0; |
|
95 QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
|
96 scriptObject, scriptName, QScriptClass::HandlesReadAccess, &id); |
|
97 if (flags & QScriptClass::HandlesReadAccess) { |
|
98 QScriptValue value = m_scriptClass->property(scriptObject, scriptName, id); |
|
99 slot.setValue(engine->scriptValueToJSCValue(value)); |
|
100 return true; |
|
101 } |
|
102 return false; |
|
103 } |
|
104 |
|
105 void ClassObjectDelegate::put(QScriptObject* object, JSC::ExecState *exec, |
|
106 const JSC::Identifier &propertyName, |
|
107 JSC::JSValue value, JSC::PutPropertySlot &slot) |
|
108 { |
|
109 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
110 QScript::SaveFrameHelper saveFrame(engine, exec); |
|
111 QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); |
|
112 QScriptString scriptName; |
|
113 QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
|
114 QScriptStringPrivate::init(scriptName, &scriptName_d); |
|
115 uint id = 0; |
|
116 QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
|
117 scriptObject, scriptName, QScriptClass::HandlesWriteAccess, &id); |
|
118 if (flags & QScriptClass::HandlesWriteAccess) { |
|
119 m_scriptClass->setProperty(scriptObject, scriptName, id, engine->scriptValueFromJSCValue(value)); |
|
120 return; |
|
121 } |
|
122 QScriptObjectDelegate::put(object, exec, propertyName, value, slot); |
|
123 } |
|
124 |
|
125 bool ClassObjectDelegate::deleteProperty(QScriptObject* object, JSC::ExecState *exec, |
|
126 const JSC::Identifier &propertyName, |
|
127 bool checkDontDelete) |
|
128 { |
|
129 // ### avoid duplication of put() |
|
130 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
131 QScript::SaveFrameHelper saveFrame(engine, exec); |
|
132 QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); |
|
133 QScriptString scriptName; |
|
134 QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
|
135 QScriptStringPrivate::init(scriptName, &scriptName_d); |
|
136 uint id = 0; |
|
137 QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
|
138 scriptObject, scriptName, QScriptClass::HandlesWriteAccess, &id); |
|
139 if (flags & QScriptClass::HandlesWriteAccess) { |
|
140 if (m_scriptClass->propertyFlags(scriptObject, scriptName, id) & QScriptValue::Undeletable) |
|
141 return false; |
|
142 m_scriptClass->setProperty(scriptObject, scriptName, id, QScriptValue()); |
|
143 return true; |
|
144 } |
|
145 return QScriptObjectDelegate::deleteProperty(object, exec, propertyName, checkDontDelete); |
|
146 } |
|
147 |
|
148 bool ClassObjectDelegate::getPropertyAttributes(const QScriptObject* object, JSC::ExecState *exec, |
|
149 const JSC::Identifier &propertyName, |
|
150 unsigned &attribs) const |
|
151 { |
|
152 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
153 QScript::SaveFrameHelper saveFrame(engine, exec); |
|
154 QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); |
|
155 QScriptString scriptName; |
|
156 QScriptStringPrivate scriptName_d(engine, propertyName, QScriptStringPrivate::StackAllocated); |
|
157 QScriptStringPrivate::init(scriptName, &scriptName_d); |
|
158 uint id = 0; |
|
159 QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( |
|
160 scriptObject, scriptName, QScriptClass::HandlesReadAccess, &id); |
|
161 if (flags & QScriptClass::HandlesReadAccess) { |
|
162 QScriptValue::PropertyFlags flags = m_scriptClass->propertyFlags(scriptObject, scriptName, id); |
|
163 attribs = 0; |
|
164 if (flags & QScriptValue::ReadOnly) |
|
165 attribs |= JSC::ReadOnly; |
|
166 if (flags & QScriptValue::SkipInEnumeration) |
|
167 attribs |= JSC::DontEnum; |
|
168 if (flags & QScriptValue::Undeletable) |
|
169 attribs |= JSC::DontDelete; |
|
170 if (flags & QScriptValue::PropertyGetter) |
|
171 attribs |= JSC::Getter; |
|
172 if (flags & QScriptValue::PropertySetter) |
|
173 attribs |= JSC::Setter; |
|
174 attribs |= flags & QScriptValue::UserRange; |
|
175 return true; |
|
176 } |
|
177 return QScriptObjectDelegate::getPropertyAttributes(object, exec, propertyName, attribs); |
|
178 } |
|
179 |
|
180 void ClassObjectDelegate::getOwnPropertyNames(QScriptObject* object, JSC::ExecState *exec, |
|
181 JSC::PropertyNameArray &propertyNames, |
|
182 bool includeNonEnumerable) |
|
183 { |
|
184 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
185 QScript::SaveFrameHelper saveFrame(engine, exec); |
|
186 QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); |
|
187 QScriptClassPropertyIterator *it = m_scriptClass->newIterator(scriptObject); |
|
188 if (it != 0) { |
|
189 while (it->hasNext()) { |
|
190 it->next(); |
|
191 QString name = it->name().toString(); |
|
192 propertyNames.add(JSC::Identifier(exec, name)); |
|
193 } |
|
194 delete it; |
|
195 } |
|
196 QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, includeNonEnumerable); |
|
197 } |
|
198 |
|
199 JSC::CallType ClassObjectDelegate::getCallData(QScriptObject*, JSC::CallData &callData) |
|
200 { |
|
201 if (!m_scriptClass->supportsExtension(QScriptClass::Callable)) |
|
202 return JSC::CallTypeNone; |
|
203 callData.native.function = call; |
|
204 return JSC::CallTypeHost; |
|
205 } |
|
206 |
|
207 JSC::JSValue JSC_HOST_CALL ClassObjectDelegate::call(JSC::ExecState *exec, JSC::JSObject *callee, |
|
208 JSC::JSValue thisValue, const JSC::ArgList &args) |
|
209 { |
|
210 if (!callee->inherits(&QScriptObject::info)) |
|
211 return JSC::throwError(exec, JSC::TypeError, "callee is not a ClassObject object"); |
|
212 QScriptObject *obj = static_cast<QScriptObject*>(callee); |
|
213 QScriptObjectDelegate *delegate = obj->delegate(); |
|
214 if (!delegate || (delegate->type() != QScriptObjectDelegate::ClassObject)) |
|
215 return JSC::throwError(exec, JSC::TypeError, "callee is not a ClassObject object"); |
|
216 |
|
217 QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass(); |
|
218 QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec); |
|
219 |
|
220 JSC::ExecState *oldFrame = eng_p->currentFrame; |
|
221 eng_p->pushContext(exec, thisValue, args, callee); |
|
222 QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame); |
|
223 QScriptValue scriptObject = eng_p->scriptValueFromJSCValue(obj); |
|
224 QVariant result = scriptClass->extension(QScriptClass::Callable, qVariantFromValue(ctx)); |
|
225 eng_p->popContext(); |
|
226 eng_p->currentFrame = oldFrame; |
|
227 return eng_p->jscValueFromVariant(result); |
|
228 } |
|
229 |
|
230 JSC::ConstructType ClassObjectDelegate::getConstructData(QScriptObject*, JSC::ConstructData &constructData) |
|
231 { |
|
232 if (!m_scriptClass->supportsExtension(QScriptClass::Callable)) |
|
233 return JSC::ConstructTypeNone; |
|
234 constructData.native.function = construct; |
|
235 return JSC::ConstructTypeHost; |
|
236 } |
|
237 |
|
238 JSC::JSObject* ClassObjectDelegate::construct(JSC::ExecState *exec, JSC::JSObject *callee, |
|
239 const JSC::ArgList &args) |
|
240 { |
|
241 Q_ASSERT(callee->inherits(&QScriptObject::info)); |
|
242 QScriptObject *obj = static_cast<QScriptObject*>(callee); |
|
243 QScriptObjectDelegate *delegate = obj->delegate(); |
|
244 QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass(); |
|
245 |
|
246 QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec); |
|
247 //JSC::ExecState *oldFrame = eng_p->currentFrame; |
|
248 eng_p->pushContext(exec, JSC::JSValue(), args, callee, true); |
|
249 QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame); |
|
250 |
|
251 QScriptValue defaultObject = ctx->thisObject(); |
|
252 QScriptValue result = qvariant_cast<QScriptValue>(scriptClass->extension(QScriptClass::Callable, qVariantFromValue(ctx))); |
|
253 if (!result.isObject()) |
|
254 result = defaultObject; |
|
255 return JSC::asObject(eng_p->scriptValueToJSCValue(result)); |
|
256 } |
|
257 |
|
258 bool ClassObjectDelegate::hasInstance(QScriptObject* object, JSC::ExecState *exec, |
|
259 JSC::JSValue value, JSC::JSValue proto) |
|
260 { |
|
261 if (!scriptClass()->supportsExtension(QScriptClass::HasInstance)) |
|
262 return QScriptObjectDelegate::hasInstance(object, exec, value, proto); |
|
263 QScriptValueList args; |
|
264 QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec); |
|
265 QScript::SaveFrameHelper saveFrame(eng_p, exec); |
|
266 args << eng_p->scriptValueFromJSCValue(object) << eng_p->scriptValueFromJSCValue(value); |
|
267 QVariant result = scriptClass()->extension(QScriptClass::HasInstance, qVariantFromValue(args)); |
|
268 return result.toBool(); |
|
269 } |
|
270 |
|
271 } // namespace QScript |
|
272 |
|
273 QT_END_NAMESPACE |