[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL-ONLY$
|
---|
| 10 | ** GNU Lesser General Public License Usage
|
---|
| 11 | ** This file may be used under the terms of the GNU Lesser
|
---|
| 12 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 13 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 14 | ** packaging of this file. Please review the following information to
|
---|
| 15 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 17 | **
|
---|
| 18 | ** If you have questions regarding the use of this file, please contact
|
---|
| 19 | ** Nokia at qt-info@nokia.com.
|
---|
| 20 | ** $QT_END_LICENSE$
|
---|
| 21 | **
|
---|
| 22 | ****************************************************************************/
|
---|
| 23 |
|
---|
| 24 | #include "config.h"
|
---|
| 25 | #include "qscriptdeclarativeobject_p.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 |
|
---|
| 39 | Q_DECLARE_METATYPE(QScriptContext*)
|
---|
| 40 | Q_DECLARE_METATYPE(QScriptValue)
|
---|
| 41 | Q_DECLARE_METATYPE(QScriptValueList)
|
---|
| 42 |
|
---|
| 43 | QT_BEGIN_NAMESPACE
|
---|
| 44 |
|
---|
| 45 | namespace QScript
|
---|
| 46 | {
|
---|
| 47 |
|
---|
| 48 | DeclarativeObjectDelegate::DeclarativeObjectDelegate(QScriptDeclarativeClass *c,
|
---|
| 49 | QScriptDeclarativeClass::Object *o)
|
---|
| 50 | : m_class(c), m_object(o)
|
---|
| 51 | {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | DeclarativeObjectDelegate::~DeclarativeObjectDelegate()
|
---|
| 55 | {
|
---|
| 56 | delete m_object;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | QScriptObjectDelegate::Type DeclarativeObjectDelegate::type() const
|
---|
| 60 | {
|
---|
| 61 | return DeclarativeClassObject;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | bool DeclarativeObjectDelegate::getOwnPropertySlot(QScriptObject* object,
|
---|
| 65 | JSC::ExecState *exec,
|
---|
| 66 | const JSC::Identifier &propertyName,
|
---|
| 67 | JSC::PropertySlot &slot)
|
---|
| 68 | {
|
---|
| 69 | QScriptDeclarativeClass::Identifier identifier = (void *)propertyName.ustring().rep();
|
---|
| 70 |
|
---|
| 71 | QScriptDeclarativeClassPrivate *p = QScriptDeclarativeClassPrivate::get(m_class);
|
---|
| 72 | p->context = reinterpret_cast<QScriptContext *>(exec);
|
---|
| 73 | QScriptClass::QueryFlags flags =
|
---|
| 74 | m_class->queryProperty(m_object, identifier, QScriptClass::HandlesReadAccess);
|
---|
| 75 | if (flags & QScriptClass::HandlesReadAccess) {
|
---|
[846] | 76 | QScriptDeclarativeClass::Value val = m_class->property(m_object, identifier);
|
---|
[556] | 77 | p->context = 0;
|
---|
[846] | 78 | slot.setValue((const JSC::JSValue &)val);
|
---|
[556] | 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 | {
|
---|
| 90 | QScriptEnginePrivate *engine = scriptEngineFromExec(exec);
|
---|
[846] | 91 | QScript::SaveFrameHelper saveFrame(engine, exec);
|
---|
[556] | 92 | QScriptDeclarativeClass::Identifier identifier = (void *)propertyName.ustring().rep();
|
---|
| 93 |
|
---|
| 94 | QScriptDeclarativeClassPrivate *p = QScriptDeclarativeClassPrivate::get(m_class);
|
---|
| 95 | p->context = reinterpret_cast<QScriptContext *>(exec);
|
---|
| 96 | QScriptClass::QueryFlags flags =
|
---|
| 97 | m_class->queryProperty(m_object, identifier, QScriptClass::HandlesWriteAccess);
|
---|
| 98 | if (flags & QScriptClass::HandlesWriteAccess) {
|
---|
| 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,
|
---|
[846] | 109 | const JSC::Identifier &propertyName)
|
---|
[556] | 110 | {
|
---|
[846] | 111 | return QScriptObjectDelegate::deleteProperty(object, exec, propertyName);
|
---|
[556] | 112 | }
|
---|
| 113 |
|
---|
| 114 | void DeclarativeObjectDelegate::getOwnPropertyNames(QScriptObject* object, JSC::ExecState *exec,
|
---|
| 115 | JSC::PropertyNameArray &propertyNames,
|
---|
[846] | 116 | JSC::EnumerationMode mode)
|
---|
[556] | 117 | {
|
---|
| 118 | QStringList properties = m_class->propertyNames(m_object);
|
---|
| 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 |
|
---|
[846] | 124 | QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, mode);
|
---|
[556] | 125 | }
|
---|
| 126 |
|
---|
| 127 | JSC::CallType DeclarativeObjectDelegate::getCallData(QScriptObject *object, JSC::CallData &callData)
|
---|
| 128 | {
|
---|
[846] | 129 | if (!QScriptDeclarativeClassPrivate::get(m_class)->supportsCall)
|
---|
| 130 | return JSC::CallTypeNone;
|
---|
| 131 | callData.native.function = call;
|
---|
| 132 | return JSC::CallTypeHost;
|
---|
[556] | 133 | }
|
---|
| 134 |
|
---|
[846] | 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 |
|
---|
| 145 | QScriptDeclarativeClass *scriptClass = static_cast<DeclarativeObjectDelegate*>(delegate)->m_class;
|
---|
| 146 | QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec);
|
---|
| 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);
|
---|
| 153 | QScriptDeclarativeClass::Value result =
|
---|
| 154 | scriptClass->call(static_cast<DeclarativeObjectDelegate*>(delegate)->m_object, ctxt);
|
---|
| 155 |
|
---|
| 156 | eng_p->popContext();
|
---|
| 157 | return (JSC::JSValue &)(result);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[556] | 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 |
|
---|
[846] | 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 |
|
---|
[556] | 188 | } // namespace QScript
|
---|
| 189 |
|
---|
| 190 | QT_END_NAMESPACE
|
---|