diff options
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/api/qscriptengine.cpp | 3 | ||||
-rw-r--r-- | src/script/bridge/qscriptqobject.cpp | 18 | ||||
-rw-r--r-- | src/script/bridge/qscriptqobject_p.h | 20 |
3 files changed, 36 insertions, 5 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 8b3b844..3b1cb9d 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -1253,6 +1253,9 @@ void QScriptEnginePrivate::setContextFlags(JSC::ExecState *exec, uint flags) } +// This function is called by JSC after all objects reachable by JSC itself +// have been processed (see JSC::Heap::markRoots()). +// Here we should mark additional objects managed by QtScript. void QScriptEnginePrivate::mark(JSC::MarkStack& markStack) { Q_Q(QScriptEngine); diff --git a/src/script/bridge/qscriptqobject.cpp b/src/script/bridge/qscriptqobject.cpp index 2bc2547..15288a7 100644 --- a/src/script/bridge/qscriptqobject.cpp +++ b/src/script/bridge/qscriptqobject.cpp @@ -97,7 +97,7 @@ struct QObjectConnection QObjectDelegate *inst = static_cast<QObjectDelegate*>(delegate); if ((inst->ownership() == QScriptEngine::ScriptOwnership) || ((inst->ownership() == QScriptEngine::AutoOwnership) - && inst->value() && !inst->value()->parent())) { + && !inst->hasParent())) { senderWrapper = JSC::JSValue(); } else { markStack.append(senderWrapper); @@ -2199,6 +2199,10 @@ QObjectData::~QObjectData() } } +// This function assumes all objects reachable elsewhere in the JS environment +// (stack, heap) have been marked already (see QScriptEnginePrivate::mark()). +// This determines whether any of QtScript's internal QObject wrappers are only +// weakly referenced and can be discarded. void QObjectData::mark(JSC::MarkStack& markStack) { if (connectionManager) @@ -2207,10 +2211,14 @@ void QObjectData::mark(JSC::MarkStack& markStack) QList<QScript::QObjectWrapperInfo>::iterator it; for (it = wrappers.begin(); it != wrappers.end(); ) { const QScript::QObjectWrapperInfo &info = *it; - // ### don't mark if there are no other references. - // we need something like isMarked() - markStack.append(info.object); - ++it; + if (JSC::Heap::isCellMarked(info.object)) { + ++it; + } else if (info.isCollectableWhenWeaklyReferenced()) { + it = wrappers.erase(it); + } else { + markStack.append(info.object); + ++it; + } } } } diff --git a/src/script/bridge/qscriptqobject_p.h b/src/script/bridge/qscriptqobject_p.h index 837368e..c6ebc8a 100644 --- a/src/script/bridge/qscriptqobject_p.h +++ b/src/script/bridge/qscriptqobject_p.h @@ -95,6 +95,7 @@ public: inline QObject *value() const { return data->value; } inline void setValue(QObject* value) { data->value = value; } + inline bool hasParent() const { return data->value && data->value->parent(); } inline QScriptEngine::ValueOwnership ownership() const { return data->ownership; } @@ -138,6 +139,25 @@ struct QObjectWrapperInfo QScriptObject *object; QScriptEngine::ValueOwnership ownership; QScriptEngine::QObjectWrapOptions options; + + // Returns true if this wrapper can be garbage-collected when there are no + // other references to it in the JS environment (weak reference), otherwise + // returns false (should not be collected). + bool isCollectableWhenWeaklyReferenced() const + { + switch (ownership) { + case QScriptEngine::ScriptOwnership: + return true; + case QScriptEngine::AutoOwnership: { + QScriptObjectDelegate *delegate = object->delegate(); + Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::QtObject)); + return !static_cast<QObjectDelegate *>(delegate)->hasParent(); + } + default: + break; + } + return false; + } }; class QObjectData // : public QObjectUserData |