diff options
-rw-r--r-- | src/script/bridge/qscriptdeclarativeclass.cpp | 107 | ||||
-rw-r--r-- | src/script/bridge/qscriptdeclarativeclass_p.h | 33 |
2 files changed, 87 insertions, 53 deletions
diff --git a/src/script/bridge/qscriptdeclarativeclass.cpp b/src/script/bridge/qscriptdeclarativeclass.cpp index c017e13..371a3c3 100644 --- a/src/script/bridge/qscriptdeclarativeclass.cpp +++ b/src/script/bridge/qscriptdeclarativeclass.cpp @@ -51,39 +51,38 @@ QT_BEGIN_NAMESPACE -class QScriptDeclarativeClassPrivate +QScriptDeclarativeClass::PersistentIdentifier::PersistentIdentifier() { -public: - QScriptDeclarativeClassPrivate() {} - - QScriptEngine *engine; - QScriptDeclarativeClass *q_ptr; -}; + new (&d) JSC::Identifier(); +} -void QScriptDeclarativeClass::destroyPersistentIdentifier(void **d) +QScriptDeclarativeClass::PersistentIdentifier::~PersistentIdentifier() { - ((JSC::Identifier *)d)->JSC::Identifier::~Identifier(); + ((JSC::Identifier &)d).JSC::Identifier::~Identifier(); } -void QScriptDeclarativeClass::initPersistentIdentifier(void **d, Identifier *i, const QString &str) +QScriptDeclarativeClass::PersistentIdentifier::PersistentIdentifier(const PersistentIdentifier &other) { - QScriptEnginePrivate *p = - static_cast<QScriptEnginePrivate *>(QObjectPrivate::get(d_ptr->engine)); - JSC::ExecState* exec = p->currentFrame; + identifier = other.identifier; + new (&d) JSC::Identifier((JSC::Identifier &)(other.d)); +} - new (d) JSC::Identifier(exec, (UChar *)str.constData(), str.size()); - *i = (Identifier)((JSC::Identifier *)d)->ustring().rep(); +QScriptDeclarativeClass::PersistentIdentifier & +QScriptDeclarativeClass::PersistentIdentifier::operator=(const PersistentIdentifier &other) +{ + identifier = other.identifier; + ((JSC::Identifier &)d) = (JSC::Identifier &)(other.d); + return *this; } -void QScriptDeclarativeClass::initPersistentIdentifier(void **d, Identifier *i, const Identifier &id) +class QScriptDeclarativeClassPrivate { - QScriptEnginePrivate *p = - static_cast<QScriptEnginePrivate *>(QObjectPrivate::get(d_ptr->engine)); - JSC::ExecState* exec = p->currentFrame; +public: + QScriptDeclarativeClassPrivate() {} - new (d) JSC::Identifier(exec, (JSC::UString::Rep *)id); - *i = id; -} + QScriptEngine *engine; + QScriptDeclarativeClass *q_ptr; +}; QScriptDeclarativeClass::QScriptDeclarativeClass(QScriptEngine *engine) : d_ptr(new QScriptDeclarativeClassPrivate) @@ -132,6 +131,51 @@ QScriptDeclarativeClass::Object QScriptDeclarativeClass::object(const QScriptVal return static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->object(); } +QScriptValue QScriptDeclarativeClass::function(const QScriptValue &v, const Identifier &name) +{ + QScriptValuePrivate *d = QScriptValuePrivate::get(v); + + if (!d->isObject()) + return QScriptValue(); + + JSC::ExecState *exec = d->engine->currentFrame; + JSC::JSObject *object = d->jscValue.getObject(); + JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object)); + JSC::JSValue result; + + JSC::Identifier id(exec, (JSC::UString::Rep *)name); + + if (const_cast<JSC::JSObject*>(object)->getOwnPropertySlot(exec, id, slot)) { + result = slot.getValue(exec, id); + if (QScript::isFunction(result)) + return d->engine->scriptValueFromJSCValue(result); + } + + return QScriptValue(); +} + +QScriptValue QScriptDeclarativeClass::property(const QScriptValue &v, const Identifier &name) +{ + QScriptValuePrivate *d = QScriptValuePrivate::get(v); + + if (!d->isObject()) + return QScriptValue(); + + JSC::ExecState *exec = d->engine->currentFrame; + JSC::JSObject *object = d->jscValue.getObject(); + JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object)); + JSC::JSValue result; + + JSC::Identifier id(exec, (JSC::UString::Rep *)name); + + if (const_cast<JSC::JSObject*>(object)->getOwnPropertySlot(exec, id, slot)) { + result = slot.getValue(exec, id); + return d->engine->scriptValueFromJSCValue(result); + } + + return QScriptValue(); +} + QScriptDeclarativeClass::~QScriptDeclarativeClass() { } @@ -141,34 +185,31 @@ QScriptEngine *QScriptDeclarativeClass::engine() const return d_ptr->engine; } -/* -QScriptDeclarativeClass::PersistentIdentifier * +QScriptDeclarativeClass::PersistentIdentifier QScriptDeclarativeClass::createPersistentIdentifier(const QString &str) { QScriptEnginePrivate *p = static_cast<QScriptEnginePrivate *>(QObjectPrivate::get(d_ptr->engine)); JSC::ExecState* exec = p->currentFrame; - PersistentIdentifierPrivate *rv = new PersistentIdentifierPrivate; - rv->identifierValue = JSC::Identifier(exec, (UChar *)str.constData(), str.size()); - rv->identifier = (void *)rv->identifierValue.ustring().rep(); + PersistentIdentifier rv(true); + new (&rv.d) JSC::Identifier(exec, (UChar *)str.constData(), str.size()); + rv.identifier = (void *)((JSC::Identifier &)rv.d).ustring().rep(); return rv; } -QScriptDeclarativeClass::PersistentIdentifier * +QScriptDeclarativeClass::PersistentIdentifier QScriptDeclarativeClass::createPersistentIdentifier(const Identifier &id) { QScriptEnginePrivate *p = static_cast<QScriptEnginePrivate *>(QObjectPrivate::get(d_ptr->engine)); JSC::ExecState* exec = p->currentFrame; - PersistentIdentifierPrivate *rv = new PersistentIdentifierPrivate; - rv->identifierValue = JSC::Identifier(exec, (JSC::UString::Rep *)id); - rv->identifier = (void *)rv->identifierValue.ustring().rep(); + PersistentIdentifier rv(true); + new (&rv.d) JSC::Identifier(exec, (JSC::UString::Rep *)id); + rv.identifier = (void *)((JSC::Identifier &)rv.d).ustring().rep(); return rv; } -*/ - QString QScriptDeclarativeClass::toString(const Identifier &identifier) { diff --git a/src/script/bridge/qscriptdeclarativeclass_p.h b/src/script/bridge/qscriptdeclarativeclass_p.h index a00a286..16f6942 100644 --- a/src/script/bridge/qscriptdeclarativeclass_p.h +++ b/src/script/bridge/qscriptdeclarativeclass_p.h @@ -71,15 +71,22 @@ public: static QScriptDeclarativeClass *scriptClass(const QScriptValue &); static Object object(const QScriptValue &); - template<class T> - class PersistentIdentifier : public T { + static QScriptValue function(const QScriptValue &, const Identifier &); + static QScriptValue property(const QScriptValue &, const Identifier &); + + class Q_SCRIPT_EXPORT PersistentIdentifier + { public: Identifier identifier; - ~PersistentIdentifier() { QScriptDeclarativeClass::destroyPersistentIdentifier(&d); } + PersistentIdentifier(); + ~PersistentIdentifier(); + PersistentIdentifier(const PersistentIdentifier &other); + PersistentIdentifier &operator=(const PersistentIdentifier &other); + private: friend class QScriptDeclarativeClass; - PersistentIdentifier() : identifier(0), d(0) {} + PersistentIdentifier(bool) : identifier(0), d(0) {} void *d; }; @@ -88,18 +95,8 @@ public: QScriptEngine *engine() const; - template<class T> - PersistentIdentifier<T> *createPersistentIdentifier(const QString &str) { - PersistentIdentifier<T> *rv = new PersistentIdentifier<T>; - initPersistentIdentifier(&rv->d, &rv->identifier, str); - return rv; - } - template<class T> - PersistentIdentifier<T> *createPersistentIdentifier(const Identifier &id) { - PersistentIdentifier<T> *rv = new PersistentIdentifier<T>; - initPersistentIdentifier(&rv->d, &rv->identifier, id); - return rv; - } + PersistentIdentifier createPersistentIdentifier(const QString &); + PersistentIdentifier createPersistentIdentifier(const Identifier &); QString toString(const Identifier &); @@ -116,10 +113,6 @@ public: virtual QVariant toVariant(const Object &, bool *ok = 0); virtual void destroyed(const Object &); - static void destroyPersistentIdentifier(void **); - void initPersistentIdentifier(void **, Identifier *, const QString &); - void initPersistentIdentifier(void **, Identifier *, const Identifier &); - protected: QScopedPointer<QScriptDeclarativeClassPrivate> d_ptr; }; |