From 9c5e112e12f09ad5c8356d6815f9b23102e99bfd Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 16 Jun 2011 15:39:12 +1000 Subject: [PATCH] Add "shared mode" to QML contexts --- src/declarative/qml/v8/qv8contextwrapper.cpp | 36 ++++++++++++++++++++++------ src/declarative/qml/v8/qv8contextwrapper_p.h | 9 +++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/declarative/qml/v8/qv8contextwrapper.cpp b/src/declarative/qml/v8/qv8contextwrapper.cpp index 937be3f..4984102 100644 --- a/src/declarative/qml/v8/qv8contextwrapper.cpp +++ b/src/declarative/qml/v8/qv8contextwrapper.cpp @@ -59,12 +59,12 @@ public: ~QV8ContextResource(); inline QDeclarativeContextData *getContext() const; + inline QObject *getScopeObject() const; - QDeclarativeGuard scopeObject; - + quint32 isSharedContext:1; quint32 hasSubContexts:1; quint32 readOnly:1; - quint32 dummy:30; + quint32 dummy:29; QObject *secondaryScope; @@ -84,11 +84,13 @@ public: private: QDeclarativeGuardedContextData context; + QDeclarativeGuard scopeObject; + }; QV8ContextResource::QV8ContextResource(QV8Engine *engine, QDeclarativeContextData *context, QObject *scopeObject) -: QV8ObjectResource(engine), scopeObject(scopeObject), hasSubContexts(false), readOnly(true), - secondaryScope(0), context(context) +: QV8ObjectResource(engine), isSharedContext(false), hasSubContexts(false), readOnly(true), + secondaryScope(0), context(context), scopeObject(scopeObject) { } @@ -98,9 +100,21 @@ QV8ContextResource::~QV8ContextResource() context->destroy(); } +// Returns the scope object +QObject *QV8ContextResource::getScopeObject() const +{ + if (isSharedContext) + return QDeclarativeEnginePrivate::get(engine->engine())->sharedScope; + else + return scopeObject; +} + // Returns the context, including resolving a subcontext QDeclarativeContextData *QV8ContextResource::getContext() const { + if (isSharedContext) + return QDeclarativeEnginePrivate::get(engine->engine())->sharedContext; + if (!hasSubContexts) return context; @@ -127,6 +141,7 @@ QV8ContextWrapper::~QV8ContextWrapper() void QV8ContextWrapper::destroy() { + qPersistentDispose(m_sharedContext); qPersistentDispose(m_urlConstructor); qPersistentDispose(m_constructor); } @@ -146,6 +161,13 @@ void QV8ContextWrapper::init(QV8Engine *engine) ft->InstanceTemplate()->SetFallbackPropertyHandler(NullGetter, NullSetter); m_urlConstructor = qPersistentNew(ft->GetFunction()); } + { + v8::Local sharedContext = m_constructor->NewInstance(); + QV8ContextResource *r = new QV8ContextResource(engine, 0, 0); + r->isSharedContext = true; + sharedContext->SetExternalResource(r); + m_sharedContext = qPersistentNew(sharedContext); + } } v8::Local QV8ContextWrapper::qmlScope(QDeclarativeContextData *ctxt, QObject *scope) @@ -254,7 +276,7 @@ v8::Handle QV8ContextWrapper::Getter(v8::Local property, QV8Engine *engine = resource->engine; - QObject *scopeObject = resource->scopeObject; + QObject *scopeObject = resource->getScopeObject(); QHashedV8String propertystring(property); @@ -386,7 +408,7 @@ v8::Handle QV8ContextWrapper::Setter(v8::Local property, // See QV8ContextWrapper::Getter for resolution order QV8Engine *engine = resource->engine; - QObject *scopeObject = resource->scopeObject; + QObject *scopeObject = resource->getScopeObject(); QHashedV8String propertystring(property); diff --git a/src/declarative/qml/v8/qv8contextwrapper_p.h b/src/declarative/qml/v8/qv8contextwrapper_p.h index 12e01ba..7a7e745 100644 --- a/src/declarative/qml/v8/qv8contextwrapper_p.h +++ b/src/declarative/qml/v8/qv8contextwrapper_p.h @@ -88,6 +88,9 @@ public: QDeclarativeContextData *callingContext(); QDeclarativeContextData *context(v8::Handle); + + inline v8::Handle sharedContext() const; + private: static v8::Handle NullGetter(v8::Local property, const v8::AccessorInfo &info); @@ -103,8 +106,14 @@ private: QV8Engine *m_engine; v8::Persistent m_constructor; v8::Persistent m_urlConstructor; + v8::Persistent m_sharedContext; }; +v8::Handle QV8ContextWrapper::sharedContext() const +{ + return m_sharedContext; +} + QT_END_NAMESPACE #endif // QV8CONTEXTWRAPPER_P_H -- 2.7.4