diff options
author | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2009-07-13 08:11:58 (GMT) |
---|---|---|
committer | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2009-07-13 08:11:58 (GMT) |
commit | 777762fa72de048b8ace97e21192bb659accb9f9 (patch) | |
tree | ef516cb06c1078d7945fe0d9aabf51395390eab7 /src/script/bridge | |
parent | f6304c909b074a951dc33c39bc2b9d75536b1c22 (diff) | |
parent | 7f2fadda40fa79a35e9c77ef4f9613940c23f382 (diff) | |
download | Qt-777762fa72de048b8ace97e21192bb659accb9f9.zip Qt-777762fa72de048b8ace97e21192bb659accb9f9.tar.gz Qt-777762fa72de048b8ace97e21192bb659accb9f9.tar.bz2 |
Merge branch 'qtscript-jsc-backend' of git@scm.dev.nokia.troll.no:qt/qt-webkit into qtscript-jsc-backend
Diffstat (limited to 'src/script/bridge')
-rw-r--r-- | src/script/bridge/bridge.pri | 4 | ||||
-rw-r--r-- | src/script/bridge/qscriptclassobject.cpp | 225 | ||||
-rw-r--r-- | src/script/bridge/qscriptclassobject_p.h | 84 | ||||
-rw-r--r-- | src/script/bridge/qscriptobject.cpp | 225 | ||||
-rw-r--r-- | src/script/bridge/qscriptobject_p.h | 132 | ||||
-rw-r--r-- | src/script/bridge/qscriptqobject.cpp | 132 | ||||
-rw-r--r-- | src/script/bridge/qscriptqobject_p.h | 45 | ||||
-rw-r--r-- | src/script/bridge/qscriptvariant.cpp | 45 | ||||
-rw-r--r-- | src/script/bridge/qscriptvariant_p.h | 25 |
9 files changed, 800 insertions, 117 deletions
diff --git a/src/script/bridge/bridge.pri b/src/script/bridge/bridge.pri index 585ccc0..cf8395a 100644 --- a/src/script/bridge/bridge.pri +++ b/src/script/bridge/bridge.pri @@ -1,9 +1,13 @@ SOURCES += \ $$PWD/qscriptfunction.cpp \ + $$PWD/qscriptobject.cpp \ + $$PWD/qscriptclassobject.cpp \ $$PWD/qscriptvariant.cpp \ $$PWD/qscriptqobject.cpp HEADERS += \ $$PWD/qscriptfunction_p.h \ + $$PWD/qscriptobject_p.h \ + $$PWD/qscriptclassobject_p.h \ $$PWD/qscriptvariant_p.h \ $$PWD/qscriptqobject_p.h diff --git a/src/script/bridge/qscriptclassobject.cpp b/src/script/bridge/qscriptclassobject.cpp new file mode 100644 index 0000000..62364f3 --- /dev/null +++ b/src/script/bridge/qscriptclassobject.cpp @@ -0,0 +1,225 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +#include "qscriptclassobject_p.h" + +#ifndef QT_NO_SCRIPT + +#include "../api/qscriptengine.h" +#include "../api/qscriptengine_p.h" +#include "../api/qscriptclass.h" + +#include "Error.h" + +Q_DECLARE_METATYPE(QScriptContext*) +Q_DECLARE_METATYPE(QScriptValue) +Q_DECLARE_METATYPE(QScriptValueList) + +QT_BEGIN_NAMESPACE + +namespace QScript +{ + +QString qtStringFromJSCUString(const JSC::UString &str); + +ClassObjectDelegate::ClassObjectDelegate(QScriptClass *scriptClass) + : m_scriptClass(scriptClass) +{ +} + +ClassObjectDelegate::~ClassObjectDelegate() +{ +} + +QScriptClass *ClassObjectDelegate::scriptClass() const +{ + return m_scriptClass; +} + +void ClassObjectDelegate::setScriptClass(QScriptClass *scriptClass) +{ + m_scriptClass = scriptClass; +} + +QScriptObjectDelegate::Type ClassObjectDelegate::type() const +{ + return ClassObject; +} + +bool ClassObjectDelegate::getOwnPropertySlot(QScriptObject* object, + JSC::ExecState *exec, + const JSC::Identifier &propertyName, + JSC::PropertySlot &slot) +{ + QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); + QString name = qtStringFromJSCUString(propertyName.ustring()); + QScriptString scriptName = QScriptEnginePrivate::get(engine)->toStringHandle(name); + uint id = 0; + QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( + scriptObject, scriptName, QScriptClass::HandlesReadAccess, &id); + if (flags & QScriptClass::HandlesReadAccess) { + QScriptValue value = m_scriptClass->property(scriptObject, scriptName, id); + slot.setValue(engine->scriptValueToJSCValue(value)); + return true; + } + return QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot); +} + +void ClassObjectDelegate::put(QScriptObject* object, JSC::ExecState *exec, + const JSC::Identifier &propertyName, + JSC::JSValue value, JSC::PutPropertySlot &slot) +{ + QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); + QString name = qtStringFromJSCUString(propertyName.ustring()); + QScriptString scriptName = QScriptEnginePrivate::get(engine)->toStringHandle(name); + uint id = 0; + QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( + scriptObject, scriptName, QScriptClass::HandlesWriteAccess, &id); + if (flags & QScriptClass::HandlesWriteAccess) { + m_scriptClass->setProperty(scriptObject, scriptName, id, engine->scriptValueFromJSCValue(value)); + return; + } + QScriptObjectDelegate::put(object, exec, propertyName, value, slot); +} + +bool ClassObjectDelegate::deleteProperty(QScriptObject* object, JSC::ExecState *exec, + const JSC::Identifier &propertyName) +{ + // ### avoid duplication of put() + QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); + QString name = qtStringFromJSCUString(propertyName.ustring()); + QScriptString scriptName = QScriptEnginePrivate::get(engine)->toStringHandle(name); + uint id = 0; + QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( + scriptObject, scriptName, QScriptClass::HandlesWriteAccess, &id); + if (flags & QScriptClass::HandlesWriteAccess) { + if (m_scriptClass->propertyFlags(scriptObject, scriptName, id) & QScriptValue::Undeletable) + return false; + m_scriptClass->setProperty(scriptObject, scriptName, id, QScriptValue()); + return true; + } + return QScriptObjectDelegate::deleteProperty(object, exec, propertyName); +} + +bool ClassObjectDelegate::getPropertyAttributes(const QScriptObject* object, JSC::ExecState *exec, + const JSC::Identifier &propertyName, + unsigned &attribs) const +{ + QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + QScriptValue scriptObject = engine->scriptValueFromJSCValue(object); + QString name = qtStringFromJSCUString(propertyName.ustring()); + QScriptString scriptName = QScriptEnginePrivate::get(engine)->toStringHandle(name); + uint id = 0; + QScriptClass::QueryFlags flags = m_scriptClass->queryProperty( + scriptObject, scriptName, QScriptClass::HandlesReadAccess, &id); + if (flags & QScriptClass::HandlesReadAccess) { + QScriptValue::PropertyFlags flags = m_scriptClass->propertyFlags(scriptObject, scriptName, id); + attribs = 0; + if (flags & QScriptValue::ReadOnly) + attribs |= JSC::ReadOnly; + if (flags & QScriptValue::SkipInEnumeration) + attribs |= JSC::DontEnum; + if (flags & QScriptValue::Undeletable) + attribs |= JSC::DontDelete; + if (flags & QScriptValue::PropertyGetter) + attribs |= JSC::Getter; + if (flags & QScriptValue::PropertySetter) + attribs |= JSC::Setter; + attribs |= flags & QScriptValue::UserRange; + return true; + } + return QScriptObjectDelegate::getPropertyAttributes(object, exec, propertyName, attribs); +} + +void ClassObjectDelegate::getPropertyNames(QScriptObject* object, JSC::ExecState *exec, + JSC::PropertyNameArray &propertyNames) +{ + qWarning("Enumeration of custom script objects not implemented"); + QScriptObjectDelegate::getPropertyNames(object, exec, propertyNames); +} + +JSC::CallType ClassObjectDelegate::getCallData(QScriptObject*, JSC::CallData &callData) +{ + if (!m_scriptClass->supportsExtension(QScriptClass::Callable)) + return JSC::CallTypeNone; + callData.native.function = call; + return JSC::CallTypeHost; +} + +JSC::JSValue JSC_HOST_CALL ClassObjectDelegate::call(JSC::ExecState *exec, JSC::JSObject *callee, + JSC::JSValue thisValue, const JSC::ArgList &args) +{ + if (!callee->isObject(&QScriptObject::info)) + return JSC::throwError(exec, JSC::TypeError, "callee is not a ClassObject object"); + QScriptObject *obj = static_cast<QScriptObject*>(callee); + QScriptObjectDelegate *delegate = obj->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::ClassObject)) + return JSC::throwError(exec, JSC::TypeError, "callee is not a ClassObject object"); + QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass(); + QScriptEnginePrivate *eng_p = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + JSC::ExecState *previousFrame = eng_p->currentFrame; + QScriptContext *ctx = eng_p->contextForFrame(exec); + eng_p->currentFrame = exec; + QScriptValue scriptObject = eng_p->scriptValueFromJSCValue(obj); + QVariant result = scriptClass->extension(QScriptClass::Callable, qVariantFromValue(ctx)); + eng_p->currentFrame = previousFrame; + eng_p->releaseContextForFrame(exec); + return eng_p->jscValueFromVariant(result); +} + +JSC::ConstructType ClassObjectDelegate::getConstructData(QScriptObject*, JSC::ConstructData &constructData) +{ + if (!m_scriptClass->supportsExtension(QScriptClass::Callable)) + return JSC::ConstructTypeNone; + constructData.native.function = construct; + return JSC::ConstructTypeHost; +} + +JSC::JSObject* ClassObjectDelegate::construct(JSC::ExecState *exec, JSC::JSObject *callee, + const JSC::ArgList &args) +{ + Q_ASSERT(callee->isObject(&QScriptObject::info)); + QScriptObject *obj = static_cast<QScriptObject*>(callee); + QScriptObjectDelegate *delegate = obj->delegate(); + QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass(); + QScriptEnginePrivate *eng_p = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + JSC::ExecState *previousFrame = eng_p->currentFrame; + QScriptContext *ctx = eng_p->contextForFrame(exec); + eng_p->currentFrame = exec; + QScriptValue defaultObject = ctx->thisObject(); + QScriptValue result = qvariant_cast<QScriptValue>(scriptClass->extension(QScriptClass::Callable, qVariantFromValue(ctx))); + if (!result.isObject()) + result = defaultObject; + eng_p->currentFrame = previousFrame; + eng_p->releaseContextForFrame(exec); + return JSC::asObject(eng_p->scriptValueToJSCValue(result)); +} + +bool ClassObjectDelegate::hasInstance(QScriptObject* object, JSC::ExecState *exec, + JSC::JSValue value, JSC::JSValue proto) +{ + if (!scriptClass()->supportsExtension(QScriptClass::HasInstance)) + return QScriptObjectDelegate::hasInstance(object, exec, value, proto); + QScriptValueList args; + QScriptEnginePrivate *eng_p = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; + args << eng_p->scriptValueFromJSCValue(object) << eng_p->scriptValueFromJSCValue(value); + QVariant result = scriptClass()->extension(QScriptClass::HasInstance, qVariantFromValue(args)); + return result.toBool(); +} + +} // namespace QScript + +QT_END_NAMESPACE + +#endif // QT_NO_SCRIPT diff --git a/src/script/bridge/qscriptclassobject_p.h b/src/script/bridge/qscriptclassobject_p.h new file mode 100644 index 0000000..b2d6d7f --- /dev/null +++ b/src/script/bridge/qscriptclassobject_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +#ifndef QSCRIPTCLASSOBJECT_P_H +#define QSCRIPTCLASSOBJECT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qobjectdefs.h> + +#ifndef QT_NO_SCRIPT + +#include "qscriptobject_p.h" + +QT_BEGIN_NAMESPACE + +class QScriptClass; + +namespace QScript +{ + +class ClassObjectDelegate : public QScriptObjectDelegate +{ +public: + ClassObjectDelegate(QScriptClass *scriptClass); + ~ClassObjectDelegate(); + + QScriptClass *scriptClass() const; + void setScriptClass(QScriptClass *scriptClass); + + virtual Type type() const; + + virtual bool getOwnPropertySlot(QScriptObject*, JSC::ExecState*, + const JSC::Identifier& propertyName, + JSC::PropertySlot&); + virtual void put(QScriptObject*, JSC::ExecState* exec, + const JSC::Identifier& propertyName, + JSC::JSValue, JSC::PutPropertySlot&); + virtual bool deleteProperty(QScriptObject*, JSC::ExecState*, + const JSC::Identifier& propertyName); + virtual bool getPropertyAttributes(const QScriptObject*, JSC::ExecState*, + const JSC::Identifier&, + unsigned&) const; + virtual void getPropertyNames(QScriptObject*, JSC::ExecState*, + JSC::PropertyNameArray&); + + virtual JSC::CallType getCallData(QScriptObject*, JSC::CallData&); + static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*, + JSC::JSValue, const JSC::ArgList&); + virtual JSC::ConstructType getConstructData(QScriptObject*, JSC::ConstructData&); + static JSC::JSObject* construct(JSC::ExecState*, JSC::JSObject*, + const JSC::ArgList&); + + virtual bool hasInstance(QScriptObject*, JSC::ExecState*, + JSC::JSValue value, JSC::JSValue proto); + +private: + QScriptClass *m_scriptClass; +}; + +} // namespace QScript + +QT_END_NAMESPACE + +#endif // QT_NO_SCRIPT + +#endif diff --git a/src/script/bridge/qscriptobject.cpp b/src/script/bridge/qscriptobject.cpp new file mode 100644 index 0000000..d490959 --- /dev/null +++ b/src/script/bridge/qscriptobject.cpp @@ -0,0 +1,225 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +#include "qscriptobject_p.h" + +#ifndef QT_NO_SCRIPT + +QT_BEGIN_NAMESPACE + +namespace JSC +{ +ASSERT_CLASS_FITS_IN_CELL(QScriptObject); +ASSERT_CLASS_FITS_IN_CELL(QScriptObjectPrototype); +} + +// masquerading as JSC::JSObject +const JSC::ClassInfo QScriptObject::info = { "Object", 0, 0, 0 }; + +QScriptObject::Data::~Data() +{ + delete delegate; +} + +QScriptObject::QScriptObject(WTF::PassRefPtr<JSC::Structure> sid) + : JSC::JSObject(sid), d(0) +{ +} + +QScriptObject::~QScriptObject() +{ + delete d; +} + +JSC::JSValue QScriptObject::data() const +{ + if (!d) + return JSC::JSValue(); + return d->data; +} + +void QScriptObject::setData(JSC::JSValue data) +{ + if (!d) + d = new Data(); + d->data = data; +} + +QScriptObjectDelegate *QScriptObject::delegate() const +{ + if (!d) + return 0; + return d->delegate; +} + +void QScriptObject::setDelegate(QScriptObjectDelegate *delegate) +{ + if (!d) + d = new Data(); + d->delegate = delegate; +} + +bool QScriptObject::getOwnPropertySlot(JSC::ExecState* exec, + const JSC::Identifier& propertyName, + JSC::PropertySlot& slot) +{ + if (!d || !d->delegate) + return JSC::JSObject::getOwnPropertySlot(exec, propertyName, slot); + return d->delegate->getOwnPropertySlot(this, exec, propertyName, slot); +} + +void QScriptObject::put(JSC::ExecState* exec, const JSC::Identifier& propertyName, + JSC::JSValue value, JSC::PutPropertySlot& slot) +{ + if (!d || !d->delegate) { + JSC::JSObject::put(exec, propertyName, value, slot); + return; + } + d->delegate->put(this, exec, propertyName, value, slot); +} + +bool QScriptObject::deleteProperty(JSC::ExecState* exec, + const JSC::Identifier& propertyName) +{ + if (!d || !d->delegate) + return JSC::JSObject::deleteProperty(exec, propertyName); + return d->delegate->deleteProperty(this, exec, propertyName); +} + +bool QScriptObject::getPropertyAttributes(JSC::ExecState* exec, const JSC::Identifier& propertyName, + unsigned& attributes) const +{ + if (!d || !d->delegate) + return JSC::JSObject::getPropertyAttributes(exec, propertyName, attributes); + return d->delegate->getPropertyAttributes(this, exec, propertyName, attributes); +} + +void QScriptObject::getPropertyNames(JSC::ExecState* exec, JSC::PropertyNameArray& propertyNames) +{ + if (!d || !d->delegate) { + JSC::JSObject::getPropertyNames(exec, propertyNames); + return; + } + d->delegate->getPropertyNames(this, exec, propertyNames); +} + +void QScriptObject::mark() +{ + if (!d || !d->delegate) { + JSC::JSObject::mark(); + return; + } + d->delegate->mark(this); +} + +JSC::CallType QScriptObject::getCallData(JSC::CallData &data) +{ + if (!d || !d->delegate) + return JSC::JSObject::getCallData(data); + return d->delegate->getCallData(this, data); +} + +JSC::ConstructType QScriptObject::getConstructData(JSC::ConstructData &data) +{ + if (!d || !d->delegate) + return JSC::JSObject::getConstructData(data); + return d->delegate->getConstructData(this, data); +} + +bool QScriptObject::hasInstance(JSC::ExecState* exec, JSC::JSValue value, JSC::JSValue proto) +{ + if (!d || !d->delegate) + return JSC::JSObject::hasInstance(exec, value, proto); + return d->delegate->hasInstance(this, exec, value, proto); +} + +JSC::JSValue QScriptObject::lookupGetter(JSC::ExecState*, const JSC::Identifier& propertyName) +{ + Q_ASSERT_X(false, Q_FUNC_INFO, "implement me"); +} + +JSC::JSValue QScriptObject::lookupSetter(JSC::ExecState*, const JSC::Identifier& propertyName) +{ + Q_ASSERT_X(false, Q_FUNC_INFO, "implement me"); +} + +QScriptObjectPrototype::QScriptObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure> structure, + JSC::Structure* /*prototypeFunctionStructure*/) + : QScriptObject(structure) +{ +} + +QScriptObjectDelegate::QScriptObjectDelegate() +{ +} + +QScriptObjectDelegate::~QScriptObjectDelegate() +{ +} + +bool QScriptObjectDelegate::getOwnPropertySlot(QScriptObject* object, JSC::ExecState* exec, + const JSC::Identifier& propertyName, + JSC::PropertySlot& slot) +{ + return object->JSC::JSObject::getOwnPropertySlot(exec, propertyName, slot); +} + +void QScriptObjectDelegate::put(QScriptObject* object, JSC::ExecState* exec, + const JSC::Identifier& propertyName, + JSC::JSValue value, JSC::PutPropertySlot& slot) +{ + object->JSC::JSObject::put(exec, propertyName, value, slot); +} + +bool QScriptObjectDelegate::deleteProperty(QScriptObject* object, JSC::ExecState* exec, + const JSC::Identifier& propertyName) +{ + return object->JSC::JSObject::deleteProperty(exec, propertyName); +} + +bool QScriptObjectDelegate::getPropertyAttributes(const QScriptObject* object, + JSC::ExecState* exec, + const JSC::Identifier& propertyName, + unsigned& attributes) const +{ + return object->JSC::JSObject::getPropertyAttributes(exec, propertyName, attributes); +} + +void QScriptObjectDelegate::getPropertyNames(QScriptObject* object, JSC::ExecState* exec, + JSC::PropertyNameArray& propertyNames) +{ + object->JSC::JSObject::getPropertyNames(exec, propertyNames); +} + +void QScriptObjectDelegate::mark(QScriptObject* object) +{ + object->JSC::JSObject::mark(); +} + +JSC::CallType QScriptObjectDelegate::getCallData(QScriptObject* object, JSC::CallData& data) +{ + return object->JSC::JSObject::getCallData(data); +} + +JSC::ConstructType QScriptObjectDelegate::getConstructData(QScriptObject* object, JSC::ConstructData& data) +{ + return object->JSC::JSObject::getConstructData(data); +} + +bool QScriptObjectDelegate::hasInstance(QScriptObject* object, JSC::ExecState* exec, + JSC::JSValue value, JSC::JSValue proto) +{ + return object->JSC::JSObject::hasInstance(exec, value, proto); +} + +QT_END_NAMESPACE + +#endif // QT_NO_SCRIPT diff --git a/src/script/bridge/qscriptobject_p.h b/src/script/bridge/qscriptobject_p.h new file mode 100644 index 0000000..a4ed94c --- /dev/null +++ b/src/script/bridge/qscriptobject_p.h @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +#ifndef QSCRIPTOBJECT_P_H +#define QSCRIPTOBJECT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qobjectdefs.h> + +#ifndef QT_NO_SCRIPT + +#include "JSObject.h" + +QT_BEGIN_NAMESPACE + +class QScriptObjectDelegate; + +class QScriptObject : public JSC::JSObject +{ +public: + // work around CELL_SIZE limitation + struct Data + { + JSC::JSValue data; // QScriptValue::data + QScriptObjectDelegate *delegate; + + Data() : delegate(0) {} + ~Data(); + }; + + explicit QScriptObject(WTF::PassRefPtr<JSC::Structure> sid); + virtual ~QScriptObject(); + + virtual bool getOwnPropertySlot(JSC::ExecState*, + const JSC::Identifier& propertyName, + JSC::PropertySlot&); + virtual void put(JSC::ExecState* exec, const JSC::Identifier& propertyName, + JSC::JSValue, JSC::PutPropertySlot&); + virtual bool deleteProperty(JSC::ExecState*, + const JSC::Identifier& propertyName); + virtual bool getPropertyAttributes(JSC::ExecState*, const JSC::Identifier&, + unsigned&) const; + virtual void getPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&); + virtual JSC::JSValue lookupGetter(JSC::ExecState*, const JSC::Identifier& propertyName); + virtual JSC::JSValue lookupSetter(JSC::ExecState*, const JSC::Identifier& propertyName); + virtual void mark(); + virtual JSC::CallType getCallData(JSC::CallData&); + virtual JSC::ConstructType getConstructData(JSC::ConstructData&); + virtual bool hasInstance(JSC::ExecState*, JSC::JSValue value, JSC::JSValue proto); + + virtual const JSC::ClassInfo* classInfo() const { return &info; } + static const JSC::ClassInfo info; + + static WTF::PassRefPtr<JSC::Structure> createStructure(JSC::JSValue prototype) + { + return JSC::Structure::create(prototype, JSC::TypeInfo(JSC::ObjectType, JSC::ImplementsHasInstance)); + } + + JSC::JSValue data() const; + void setData(JSC::JSValue data); + + QScriptObjectDelegate *delegate() const; + void setDelegate(QScriptObjectDelegate *delegate); + +protected: + Data *d; +}; + +class QScriptObjectPrototype : public QScriptObject +{ +public: + QScriptObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>, + JSC::Structure* prototypeFunctionStructure); +}; + +class QScriptObjectDelegate +{ +public: + enum Type { + QtObject, + Variant, + ClassObject + }; + + QScriptObjectDelegate(); + virtual ~QScriptObjectDelegate(); + + virtual Type type() const = 0; + + virtual bool getOwnPropertySlot(QScriptObject*, JSC::ExecState*, + const JSC::Identifier& propertyName, + JSC::PropertySlot&); + virtual void put(QScriptObject*, JSC::ExecState* exec, const JSC::Identifier& propertyName, + JSC::JSValue, JSC::PutPropertySlot&); + virtual bool deleteProperty(QScriptObject*, JSC::ExecState*, + const JSC::Identifier& propertyName); + virtual bool getPropertyAttributes(const QScriptObject*, JSC::ExecState*, + const JSC::Identifier&, unsigned&) const; + virtual void getPropertyNames(QScriptObject*, JSC::ExecState*, JSC::PropertyNameArray&); + virtual void mark(QScriptObject*); + virtual JSC::CallType getCallData(QScriptObject*, JSC::CallData&); + virtual JSC::ConstructType getConstructData(QScriptObject*, JSC::ConstructData&); + virtual bool hasInstance(QScriptObject*, JSC::ExecState*, + JSC::JSValue value, JSC::JSValue proto); + +private: + Q_DISABLE_COPY(QScriptObjectDelegate) +}; + +QT_END_NAMESPACE + +#endif // QT_NO_SCRIPT + +#endif diff --git a/src/script/bridge/qscriptqobject.cpp b/src/script/bridge/qscriptqobject.cpp index 8d9b5e4..a17daea 100644 --- a/src/script/bridge/qscriptqobject.cpp +++ b/src/script/bridge/qscriptqobject.cpp @@ -32,7 +32,6 @@ QT_BEGIN_NAMESPACE namespace JSC { -ASSERT_CLASS_FITS_IN_CELL(QScript::QObjectWrapperObject); ASSERT_CLASS_FITS_IN_CELL(QScript::QObjectPrototype); ASSERT_CLASS_FITS_IN_CELL(QScript::QMetaObjectWrapperObject); ASSERT_CLASS_FITS_IN_CELL(QScript::QMetaObjectPrototype); @@ -70,6 +69,8 @@ struct QObjectConnection { if (senderWrapper && !senderWrapper.marked()) { // see if the sender should be marked or not + Q_ASSERT_X(false, Q_FUNC_INFO, "implement me"); +#if 0 Q_ASSERT(JSC::asObject(senderWrapper)->inherits(&QObjectWrapperObject::info)); QObjectWrapperObject *inst = static_cast<QObjectWrapperObject*>(JSC::asObject(senderWrapper)); if ((inst->ownership() == QScriptEngine::ScriptOwnership) @@ -79,6 +80,7 @@ struct QObjectConnection } else { senderWrapper.mark(); } +#endif } if (receiver) receiver.mark(); @@ -235,15 +237,18 @@ void QtFunction::mark() data->object.mark(); } -QObjectWrapperObject *QtFunction::wrapperObject() const +QScriptObject *QtFunction::wrapperObject() const { - Q_ASSERT(JSC::asObject(data->object)->inherits(&QObjectWrapperObject::info)); - return static_cast<QObjectWrapperObject*>(JSC::asObject(data->object)); + Q_ASSERT(JSC::asObject(data->object)->inherits(&QScriptObject::info)); + return static_cast<QScriptObject*>(JSC::asObject(data->object)); } QObject *QtFunction::qobject() const { - return wrapperObject()->value(); + QScriptObject *scriptObject = wrapperObject(); + QScriptObjectDelegate *delegate = scriptObject->delegate(); + Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::QtObject)); + return static_cast<QScript::QObjectDelegate*>(delegate)->value(); } const QMetaObject *QtFunction::metaObject() const @@ -462,17 +467,22 @@ struct QScriptMetaArguments JSC::JSValue QtFunction::execute(JSC::ExecState *exec, JSC::JSValue thisValue, const JSC::ArgList &scriptArgs) { - Q_ASSERT(data->object.isObject(&QObjectWrapperObject::info)); - QObjectWrapperObject *wrapper = static_cast<QObjectWrapperObject*>(JSC::asObject(data->object)); - QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; - QObject *qobj = wrapper->value(); + Q_ASSERT(data->object.isObject(&QScriptObject::info)); + QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(data->object)); + QScriptObjectDelegate *delegate = scriptObject->delegate(); + Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::QtObject)); + QObject *qobj = static_cast<QScript::QObjectDelegate*>(delegate)->value(); Q_ASSERT_X(qobj != 0, "QtFunction::call", "handle the case when QObject has been deleted"); + QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine; const QMetaObject *meta = qobj->metaObject(); - QObject *thisQObject; - if (thisValue.isObject(&QObjectWrapperObject::info)) - thisQObject = static_cast<QObjectWrapperObject*>(JSC::asObject(thisValue))->value(); - else + QObject *thisQObject = 0; + if (thisValue.isObject(&QScriptObject::info)) { + delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); + if (delegate && (delegate->type() == QScriptObjectDelegate::QtObject)) + thisQObject = static_cast<QScript::QObjectDelegate*>(delegate)->value(); + } + if (!thisQObject) thisQObject = qobj; // ### TypeError if (!meta->cast(thisQObject)) { @@ -1077,17 +1087,15 @@ int QtPropertyFunction::propertyIndex() const return data->index; } -const JSC::ClassInfo QObjectWrapperObject::info = { "QObject", 0, 0, 0 }; -QObjectWrapperObject::QObjectWrapperObject( +QObjectDelegate::QObjectDelegate( QObject *object, QScriptEngine::ValueOwnership ownership, - const QScriptEngine::QObjectWrapOptions &options, - WTF::PassRefPtr<JSC::Structure> sid) - : JSC::JSObject(sid), data(new Data(object, ownership, options)) + const QScriptEngine::QObjectWrapOptions &options) + : data(new Data(object, ownership, options)) { } -QObjectWrapperObject::~QObjectWrapperObject() +QObjectDelegate::~QObjectDelegate() { switch (data->ownership) { case QScriptEngine::QtOwnership: @@ -1106,9 +1114,14 @@ QObjectWrapperObject::~QObjectWrapperObject() delete data; } -bool QObjectWrapperObject::getOwnPropertySlot(JSC::ExecState *exec, - const JSC::Identifier &propertyName, - JSC::PropertySlot &slot) +QScriptObjectDelegate::Type QObjectDelegate::type() const +{ + return QtObject; +} + +bool QObjectDelegate::getOwnPropertySlot(QScriptObject *object, JSC::ExecState *exec, + const JSC::Identifier &propertyName, + JSC::PropertySlot &slot) { QByteArray name = qtStringFromJSCUString(propertyName.ustring()).toLatin1(); QObject *qobject = data->value; @@ -1142,7 +1155,7 @@ bool QObjectWrapperObject::getOwnPropertySlot(JSC::ExecState *exec, if (!(opt & QScriptEngine::ExcludeSuperClassMethods) || (index >= meta->methodOffset())) { QtFunction *fun = new (exec)QtFunction( - this, index, /*maybeOverloaded=*/false, + object, index, /*maybeOverloaded=*/false, &exec->globalData(), exec->lexicalGlobalObject()->functionStructure(), propertyName); slot.setValue(fun); @@ -1193,7 +1206,7 @@ bool QObjectWrapperObject::getOwnPropertySlot(JSC::ExecState *exec, if (hasMethodAccess(method, index, opt) && (methodName(method) == name)) { QtFunction *fun = new (exec)QtFunction( - this, index, /*maybeOverloaded=*/true, + object, index, /*maybeOverloaded=*/true, &exec->globalData(), exec->lexicalGlobalObject()->functionStructure(), propertyName); slot.setValue(fun); @@ -1215,11 +1228,12 @@ bool QObjectWrapperObject::getOwnPropertySlot(JSC::ExecState *exec, } } - return JSC::JSObject::getOwnPropertySlot(exec, propertyName, slot); + return QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot); } -void QObjectWrapperObject::put(JSC::ExecState* exec, const JSC::Identifier& propertyName, - JSC::JSValue value, JSC::PutPropertySlot &slot) +void QObjectDelegate::put(QScriptObject *object, JSC::ExecState* exec, + const JSC::Identifier& propertyName, + JSC::JSValue value, JSC::PutPropertySlot &slot) { QByteArray name = qtStringFromJSCUString(propertyName.ustring()).toLatin1(); QObject *qobject = data->value; @@ -1274,7 +1288,7 @@ void QObjectWrapperObject::put(JSC::ExecState* exec, const JSC::Identifier& prop JSC::CallType callType = fun.getCallData(callData); JSC::JSValue argv[1] = { value }; JSC::ArgList args(argv, 1); - (void)JSC::call(exec, fun, callType, callData, this, args); + (void)JSC::call(exec, fun, callType, callData, object, args); } else { QVariant v; if (prop.isEnumType() && value.isString() @@ -1310,11 +1324,11 @@ void QObjectWrapperObject::put(JSC::ExecState* exec, const JSC::Identifier& prop } } - JSC::JSObject::put(exec, propertyName, value, slot); + QScriptObjectDelegate::put(object, exec, propertyName, value, slot); } -bool QObjectWrapperObject::deleteProperty(JSC::ExecState *exec, - const JSC::Identifier& propertyName) +bool QObjectDelegate::deleteProperty(QScriptObject *object, JSC::ExecState *exec, + const JSC::Identifier& propertyName) { QByteArray name = qtStringFromJSCUString(propertyName.ustring()).toLatin1(); QObject *qobject = data->value; @@ -1353,12 +1367,13 @@ bool QObjectWrapperObject::deleteProperty(JSC::ExecState *exec, return true; } - return JSC::JSObject::deleteProperty(exec, propertyName); + return QScriptObjectDelegate::deleteProperty(object, exec, propertyName); } -bool QObjectWrapperObject::getPropertyAttributes(JSC::ExecState *exec, - const JSC::Identifier &propertyName, - unsigned &attributes) const +bool QObjectDelegate::getPropertyAttributes(const QScriptObject *object, + JSC::ExecState *exec, + const JSC::Identifier &propertyName, + unsigned &attributes) const { // ### try to avoid duplicating logic from getOwnPropertySlot() QByteArray name = qtStringFromJSCUString(propertyName.ustring()).toLatin1(); @@ -1427,22 +1442,11 @@ bool QObjectWrapperObject::getPropertyAttributes(JSC::ExecState *exec, } } - return JSC::JSObject::getPropertyAttributes(exec, propertyName, attributes); -} - -JSC::JSValue QObjectWrapperObject::lookupGetter(JSC::ExecState* exec, const JSC::Identifier& propertyName) -{ - Q_ASSERT_X(false, Q_FUNC_INFO, "implement me"); - return JSC::JSObject::lookupGetter(exec, propertyName); -} - -JSC::JSValue QObjectWrapperObject::lookupSetter(JSC::ExecState* exec, const JSC::Identifier& propertyName) -{ - Q_ASSERT_X(false, Q_FUNC_INFO, "implement me"); - return JSC::JSObject::lookupSetter(exec, propertyName); + return QScriptObjectDelegate::getPropertyAttributes(object, exec, propertyName, attributes); } -void QObjectWrapperObject::getPropertyNames(JSC::ExecState *exec, JSC::PropertyNameArray &propertyNames) +void QObjectDelegate::getPropertyNames(QScriptObject *object, JSC::ExecState *exec, + JSC::PropertyNameArray &propertyNames) { QObject *qobject = data->value; if (!qobject) { @@ -1488,24 +1492,28 @@ void QObjectWrapperObject::getPropertyNames(JSC::ExecState *exec, JSC::PropertyN } } - JSC::JSObject::getPropertyNames(exec, propertyNames); + QScriptObjectDelegate::getPropertyNames(object, exec, propertyNames); } -void QObjectWrapperObject::mark() +void QObjectDelegate::mark(QScriptObject *object) { QHash<QByteArray, JSC::JSValue>::const_iterator it; for (it = data->cachedMembers.constBegin(); it != data->cachedMembers.constEnd(); ++it) JSC::asObject(it.value())->mark(); - JSC::JSObject::mark(); + object->JSC::JSObject::mark(); } static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncFindChild(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList &args) { - if (!thisValue.isObject(&QObjectWrapperObject::info)) + if (!thisValue.isObject(&QScriptObject::info)) + return throwError(exec, JSC::TypeError, "this object is not a QObject"); + QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(thisValue)); + QScriptObjectDelegate *delegate = scriptObject->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::QtObject)) return throwError(exec, JSC::TypeError, "this object is not a QObject"); - QObject *obj = static_cast<QObjectWrapperObject*>(JSC::asObject(thisValue))->value(); + QObject *obj = static_cast<QObjectDelegate*>(delegate)->value(); QString name; if (args.size() != 0) name = QScript::qtStringFromJSCUString(args.at(0).toString(exec)); @@ -1518,17 +1526,19 @@ static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncFindChild(JSC::ExecState *exec static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncFindChildren(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList &args) { - if (!thisValue.isObject(&QObjectWrapperObject::info)) - return throwError(exec, JSC::TypeError, "this object is not a QObject"); return throwError(exec, JSC::GeneralError, "QObject.prototype.findChildren not implemented"); } static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncToString(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList&) { - if (!thisValue.isObject(&QObjectWrapperObject::info)) + if (!thisValue.isObject(&QScriptObject::info)) + return JSC::jsUndefined(); + QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(thisValue)); + QScriptObjectDelegate *delegate = scriptObject->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::QtObject)) return JSC::jsUndefined(); - QObject *obj = static_cast<QObjectWrapperObject*>(JSC::asObject(thisValue))->value(); + QObject *obj = static_cast<QObjectDelegate*>(delegate)->value(); const QMetaObject *meta = obj ? obj->metaObject() : &QObject::staticMetaObject; QString name = obj ? obj->objectName() : QString::fromUtf8("unnamed"); QString str = QString::fromUtf8("%0(name = \"%1\")") @@ -1538,8 +1548,10 @@ static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncToString(JSC::ExecState *exec, QObjectPrototype::QObjectPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure, JSC::Structure* prototypeFunctionStructure) - : QObjectWrapperObject(new QObject(), QScriptEngine::AutoOwnership, /*options=*/0, structure) + : QScriptObject(structure) { + setDelegate(new QObjectDelegate(new QObject(), QScriptEngine::AutoOwnership, /*options=*/0)); + putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, /*length=*/0, exec->propertyNames().toString, qobjectProtoFuncToString), JSC::DontEnum); putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, /*length=*/1, JSC::Identifier(exec, "findChild"), qobjectProtoFuncFindChild), JSC::DontEnum); putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, /*length=*/1, JSC::Identifier(exec, "findChildren"), qobjectProtoFuncFindChildren), JSC::DontEnum); @@ -1797,7 +1809,7 @@ void QObjectConnectionManager::execute(int slotIndex, void **argv) JSC::ArgList jscArgs(argsVector.data(), argsVector.size()); JSC::JSValue senderObject; - if (senderWrapper && senderWrapper.isObject(&QObjectWrapperObject::info)) + if (senderWrapper && senderWrapper.isObject(&QScriptObject::info)) // ### check if it's actually a QObject wrapper senderObject = senderWrapper; else { QScriptEngine::QObjectWrapOptions opt = QScriptEngine::PreferExistingWrapperObject; diff --git a/src/script/bridge/qscriptqobject_p.h b/src/script/bridge/qscriptqobject_p.h index e5b65b6..986ea0f 100644 --- a/src/script/bridge/qscriptqobject_p.h +++ b/src/script/bridge/qscriptqobject_p.h @@ -23,14 +23,13 @@ // We mean it. // -#include <QtCore/qobject.h> +#include "qscriptobject_p.h" #ifndef QT_NO_SCRIPT #include "qscriptengine.h" #include <QtCore/qpointer.h> -#include "JSObject.h" #include "InternalFunction.h" QT_BEGIN_NAMESPACE @@ -43,10 +42,9 @@ enum AttributeExtension { QObjectMemberAttribute = 1 << 12 }; -class QObjectWrapperObject : public JSC::JSObject +class QObjectDelegate : public QScriptObjectDelegate { public: - // work around CELL_SIZE limitation struct Data { QPointer<QObject> value; @@ -60,28 +58,26 @@ public: : value(o), ownership(own), options(opt) {} }; - explicit QObjectWrapperObject( + QObjectDelegate( QObject *object, QScriptEngine::ValueOwnership ownership, - const QScriptEngine::QObjectWrapOptions &options, - WTF::PassRefPtr<JSC::Structure> sid); - ~QObjectWrapperObject(); - - virtual bool getOwnPropertySlot(JSC::ExecState*, + const QScriptEngine::QObjectWrapOptions &options); + ~QObjectDelegate(); + + virtual Type type() const; + + virtual bool getOwnPropertySlot(QScriptObject*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&); - virtual void put(JSC::ExecState* exec, const JSC::Identifier& propertyName, + virtual void put(QScriptObject*, JSC::ExecState* exec, + const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&); - virtual bool deleteProperty(JSC::ExecState*, + virtual bool deleteProperty(QScriptObject*, JSC::ExecState*, const JSC::Identifier& propertyName); - virtual bool getPropertyAttributes(JSC::ExecState*, const JSC::Identifier&, + virtual bool getPropertyAttributes(const QScriptObject*, JSC::ExecState*, + const JSC::Identifier&, unsigned&) const; - virtual void getPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&); - virtual JSC::JSValue lookupGetter(JSC::ExecState*, const JSC::Identifier& propertyName); - virtual JSC::JSValue lookupSetter(JSC::ExecState*, const JSC::Identifier& propertyName); - virtual void mark(); - - virtual const JSC::ClassInfo* classInfo() const { return &info; } - static const JSC::ClassInfo info; + virtual void getPropertyNames(QScriptObject*, JSC::ExecState*, JSC::PropertyNameArray&); + virtual void mark(QScriptObject*); inline QObject *value() const { return data->value; } inline void setValue(QObject* value) { data->value = value; } @@ -96,16 +92,11 @@ public: inline void setOptions(QScriptEngine::QObjectWrapOptions options) { data->options = options; } - static WTF::PassRefPtr<JSC::Structure> createStructure(JSC::JSValue prototype) - { - return JSC::Structure::create(prototype, JSC::TypeInfo(JSC::ObjectType)); - } - protected: Data *data; }; -class QObjectPrototype : public QObjectWrapperObject +class QObjectPrototype : public QScriptObject { public: QObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>, @@ -168,7 +159,7 @@ public: JSC::JSValue execute(JSC::ExecState *exec, JSC::JSValue thisValue, const JSC::ArgList &args); - QObjectWrapperObject *wrapperObject() const; + QScriptObject *wrapperObject() const; QObject *qobject() const; const QMetaObject *metaObject() const; int initialIndex() const; diff --git a/src/script/bridge/qscriptvariant.cpp b/src/script/bridge/qscriptvariant.cpp index 0255961..46da70a 100644 --- a/src/script/bridge/qscriptvariant.cpp +++ b/src/script/bridge/qscriptvariant.cpp @@ -21,7 +21,6 @@ QT_BEGIN_NAMESPACE namespace JSC { -ASSERT_CLASS_FITS_IN_CELL(QScript::QVariantWrapperObject); ASSERT_CLASS_FITS_IN_CELL(QScript::QVariantPrototype); } @@ -30,34 +29,52 @@ namespace QScript JSC::UString qtStringToJSCUString(const QString &str); -const JSC::ClassInfo QVariantWrapperObject::info = { "QVariant", 0, 0, 0 }; +QVariantDelegate::QVariantDelegate(const QVariant &value) + : m_value(value) +{ +} + +QVariantDelegate::~QVariantDelegate() +{ +} -QVariantWrapperObject::QVariantWrapperObject(WTF::PassRefPtr<JSC::Structure> sid) - : JSC::JSObject(sid), data(new Data()) +QVariant &QVariantDelegate::value() { + return m_value; } -QVariantWrapperObject::~QVariantWrapperObject() +void QVariantDelegate::setValue(const QVariant &value) { - delete data; + m_value = value; +} + +QScriptObjectDelegate::Type QVariantDelegate::type() const +{ + return Variant; } static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList&) { - if (!thisValue.isObject(&QVariantWrapperObject::info)) - return throwError(exec, JSC::TypeError); - const QVariant &v = static_cast<QVariantWrapperObject*>(JSC::asObject(thisValue))->value(); - // ### check the type + if (!thisValue.isObject(&QScriptObject::info)) + return throwError(exec, JSC::TypeError, "This object is not a QVariant"); + QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant)) + return throwError(exec, JSC::TypeError, "This object is not a QVariant"); + const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value(); + // ### call valueOf() return JSC::jsString(exec, QScript::qtStringToJSCUString(v.toString())); } static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList&) { - if (!thisValue.isObject(&QVariantWrapperObject::info)) + if (!thisValue.isObject(&QScriptObject::info)) + return throwError(exec, JSC::TypeError); + QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant)) return throwError(exec, JSC::TypeError); - const QVariant &v = static_cast<QVariantWrapperObject*>(JSC::asObject(thisValue))->value(); + const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value(); switch (v.type()) { case QVariant::Invalid: return JSC::jsUndefined(); @@ -87,9 +104,9 @@ static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure, JSC::Structure* prototypeFunctionStructure) - : QVariantWrapperObject(structure) + : QScriptObject(structure) { - setValue(QVariant()); + setDelegate(new QVariantDelegate(QVariant())); putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, variantProtoFuncToString), JSC::DontEnum); putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().valueOf, variantProtoFuncValueOf), JSC::DontEnum); diff --git a/src/script/bridge/qscriptvariant_p.h b/src/script/bridge/qscriptvariant_p.h index 6e74a6b..ae6564c 100644 --- a/src/script/bridge/qscriptvariant_p.h +++ b/src/script/bridge/qscriptvariant_p.h @@ -27,36 +27,29 @@ #ifndef QT_NO_SCRIPT -#include "JSObject.h" +#include "qscriptobject_p.h" QT_BEGIN_NAMESPACE namespace QScript { -class QVariantWrapperObject : public JSC::JSObject +class QVariantDelegate : public QScriptObjectDelegate { public: - // work around CELL_SIZE limitation - struct Data - { - QVariant value; - }; + QVariantDelegate(const QVariant &value); + ~QVariantDelegate(); - explicit QVariantWrapperObject(WTF::PassRefPtr<JSC::Structure> sid); - ~QVariantWrapperObject(); - - virtual const JSC::ClassInfo* classInfo() const { return &info; } - static const JSC::ClassInfo info; + QVariant &value(); + void setValue(const QVariant &value); - inline QVariant &value() const { return data->value; } - inline void setValue(const QVariant &value) { data->value = value; } + Type type() const; private: - Data *data; + QVariant m_value; }; -class QVariantPrototype : public QVariantWrapperObject +class QVariantPrototype : public QScriptObject { public: QVariantPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>, |