Qt 4.8
qscriptdeclarativeobject.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file. Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
19 **
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23 
24 #include "config.h"
26 
27 #include "../api/qscriptengine.h"
28 #include "../api/qscriptengine_p.h"
29 #include "../api/qscriptcontext.h"
30 #include "../api/qscriptcontext_p.h"
31 #include "../api/qscriptclass.h"
32 #include "../api/qscriptclasspropertyiterator.h"
33 
34 #include "Error.h"
35 #include "PropertyNameArray.h"
36 
37 #include <QtCore/qstringlist.h>
38 
42 
44 
45 namespace QScript
46 {
47 
50 : m_class(c), m_object(o)
51 {
52 }
53 
55 {
56  delete m_object;
57 }
58 
60 {
62 }
63 
65  JSC::ExecState *exec,
66  const JSC::Identifier &propertyName,
67  JSC::PropertySlot &slot)
68 {
69  QScriptDeclarativeClass::Identifier identifier = (void *)propertyName.ustring().rep();
70 
72  p->context = reinterpret_cast<QScriptContext *>(exec);
73  QScriptClass::QueryFlags flags =
75  if (flags & QScriptClass::HandlesReadAccess) {
77  p->context = 0;
78  slot.setValue((const JSC::JSValue &)val);
79  return true;
80  }
81  p->context = 0;
82 
83  return QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot);
84 }
85 
86 void DeclarativeObjectDelegate::put(QScriptObject* object, JSC::ExecState *exec,
87  const JSC::Identifier &propertyName,
88  JSC::JSValue value, JSC::PutPropertySlot &slot)
89 {
91  QScript::SaveFrameHelper saveFrame(engine, exec);
92  QScriptDeclarativeClass::Identifier identifier = (void *)propertyName.ustring().rep();
93 
95  p->context = reinterpret_cast<QScriptContext *>(exec);
96  QScriptClass::QueryFlags flags =
99  m_class->setProperty(m_object, identifier, engine->scriptValueFromJSCValue(value));
100  p->context = 0;
101  return;
102  }
103  p->context = 0;
104 
105  QScriptObjectDelegate::put(object, exec, propertyName, value, slot);
106 }
107 
108 bool DeclarativeObjectDelegate::deleteProperty(QScriptObject* object, JSC::ExecState *exec,
109  const JSC::Identifier &propertyName)
110 {
111  return QScriptObjectDelegate::deleteProperty(object, exec, propertyName);
112 }
113 
115  JSC::PropertyNameArray &propertyNames,
116  JSC::EnumerationMode mode)
117 {
119  for (int ii = 0; ii < properties.count(); ++ii) {
120  const QString &name = properties.at(ii);
121  propertyNames.add(JSC::Identifier(exec, name));
122  }
123 
124  QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, mode);
125 }
126 
127 JSC::CallType DeclarativeObjectDelegate::getCallData(QScriptObject *object, JSC::CallData &callData)
128 {
129  if (!QScriptDeclarativeClassPrivate::get(m_class)->supportsCall)
130  return JSC::CallTypeNone;
131  callData.native.function = call;
132  return JSC::CallTypeHost;
133 }
134 
135 JSC::JSValue DeclarativeObjectDelegate::call(JSC::ExecState *exec, JSC::JSObject *callee,
136  JSC::JSValue thisValue, const JSC::ArgList &args)
137 {
138  if (!callee->inherits(&QScriptObject::info))
139  return JSC::throwError(exec, JSC::TypeError, "callee is not a DeclarativeObject object");
140  QScriptObject *obj = static_cast<QScriptObject*>(callee);
141  QScriptObjectDelegate *delegate = obj->delegate();
142  if (!delegate || (delegate->type() != QScriptObjectDelegate::DeclarativeClassObject))
143  return JSC::throwError(exec, JSC::TypeError, "callee is not a DeclarativeObject object");
144 
147 
148  QScript::SaveFrameHelper saveFrame(eng_p, exec);
149  eng_p->pushContext(exec, thisValue, args, callee);
150  QScriptContext *ctxt = eng_p->contextForFrame(eng_p->currentFrame);
151 
152  QScriptValue scriptObject = eng_p->scriptValueFromJSCValue(obj);
154  scriptClass->call(static_cast<DeclarativeObjectDelegate*>(delegate)->m_object, ctxt);
155 
156  eng_p->popContext();
157  return (JSC::JSValue &)(result);
158 }
159 
160 JSC::ConstructType DeclarativeObjectDelegate::getConstructData(QScriptObject* object, JSC::ConstructData &constructData)
161 {
162  return QScriptObjectDelegate::getConstructData(object, constructData);
163 }
164 
165 bool DeclarativeObjectDelegate::hasInstance(QScriptObject* object, JSC::ExecState *exec,
166  JSC::JSValue value, JSC::JSValue proto)
167 {
168  return QScriptObjectDelegate::hasInstance(object, exec, value, proto);
169 }
170 
171 bool DeclarativeObjectDelegate::compareToObject(QScriptObject *o, JSC::ExecState *exec, JSC::JSObject *o2)
172 {
173  if (!o2->inherits(&QScriptObject::info))
174  return false;
175 
176  QScriptObject *scriptObject = static_cast<QScriptObject*>(o2);
177  QScriptObjectDelegate *delegate = scriptObject->delegate();
178  if (!delegate || (delegate->type() != QScriptObjectDelegate::DeclarativeClassObject))
179  return false;
180 
181  DeclarativeObjectDelegate *other = static_cast<DeclarativeObjectDelegate*>(delegate);
182  if (m_class != other->m_class)
183  return false;
184  else
185  return m_class->compare(m_object, other->m_object);
186 }
187 
188 } // namespace QScript
189 
The QScriptContext class represents a Qt Script function invocation.
virtual Value call(Object *, QScriptContext *)
virtual void getOwnPropertyNames(QScriptObject *, JSC::ExecState *, JSC::PropertyNameArray &, JSC::EnumerationMode mode=JSC::ExcludeDontEnumProperties)
virtual JSC::ConstructType getConstructData(QScriptObject *, JSC::ConstructData &)
virtual bool deleteProperty(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
virtual void getOwnPropertyNames(QScriptObject *, JSC::ExecState *, JSC::PropertyNameArray &, JSC::EnumerationMode mode=JSC::ExcludeDontEnumProperties)
unsigned char c[8]
Definition: qnumeric_p.h:62
virtual Type type() const =0
virtual bool hasInstance(QScriptObject *, JSC::ExecState *, JSC::JSValue value, JSC::JSValue proto)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool compareToObject(QScriptObject *, JSC::ExecState *, JSC::JSObject *)
JSC::CallFrame * pushContext(JSC::CallFrame *exec, JSC::JSValue thisObject, const JSC::ArgList &args, JSC::JSObject *callee, bool calledAsConstructor=false, bool clearScopeChain=false)
JSC native function doesn&#39;t have different stackframe or context.
QScriptObjectDelegate * delegate() const
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static const JSC::ClassInfo info
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void put(QScriptObject *, JSC::ExecState *exec, const JSC::Identifier &propertyName, JSC::JSValue, JSC::PutPropertySlot &)
virtual void put(QScriptObject *, JSC::ExecState *exec, const JSC::Identifier &propertyName, JSC::JSValue, JSC::PutPropertySlot &)
virtual JSC::ConstructType getConstructData(QScriptObject *, JSC::ConstructData &)
The QScriptDeclarativeClass::Value class acts as a container for JavaScript data types.
virtual void setProperty(Object *, const Identifier &name, const QScriptValue &)
JSC::ExecState * currentFrame
virtual bool compare(Object *, Object *)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags)
static QScriptDeclarativeClassPrivate * get(QScriptDeclarativeClass *c)
static QScriptContext * contextForFrame(JSC::ExecState *frame)
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
virtual bool hasInstance(QScriptObject *, JSC::ExecState *, JSC::JSValue value, JSC::JSValue proto)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual bool deleteProperty(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName)
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
virtual bool getOwnPropertySlot(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName, JSC::PropertySlot &)
virtual bool getOwnPropertySlot(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName, JSC::PropertySlot &)
static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState *, JSC::JSObject *, JSC::JSValue, const JSC::ArgList &)
static const QCssKnownValue properties[NumProperties - 1]
Definition: qcssparser.cpp:67
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
QScriptDeclarativeClass * scriptClass() const
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
DeclarativeObjectDelegate(QScriptDeclarativeClass *c, QScriptDeclarativeClass::Object *o)
static QScriptValue property(const QScriptValue &, const Identifier &)
virtual JSC::CallType getCallData(QScriptObject *, JSC::CallData &)
virtual QStringList propertyNames(Object *)
QScriptDeclarativeClass::Object * m_object