Fix parts of workerscript
authorLars Knoll <lars.knoll@digia.com>
Tue, 18 Jun 2013 10:48:08 +0000 (12:48 +0200)
committerSimon Hausmann <simon.hausmann@digia.com>
Tue, 18 Jun 2013 11:49:18 +0000 (13:49 +0200)
The worker script creates a special QmlContextWrapper
that doesn't contain many things found in the main thread.
However we still need to be able to cast to it, so it
should be the same class as the regular context wrapper.

Fixes parts of the worker script auto tests.

Change-Id: I3697b2b0080dc4ac967eb447e2efd0f28fbab465
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
src/qml/qml/qqmlcontextwrapper.cpp
src/qml/qml/qqmlcontextwrapper_p.h
src/qml/qml/v4/qv4include.cpp
src/qml/types/qquickworkerscript.cpp

index 5507c8b..6bdcda9 100644 (file)
@@ -58,11 +58,10 @@ QT_BEGIN_NAMESPACE
 using namespace QV4;
 
 DEFINE_MANAGED_VTABLE(QmlContextWrapper);
-DEFINE_MANAGED_VTABLE(QmlContextNullWrapper);
 
 QmlContextWrapper::QmlContextWrapper(QV8Engine *engine, QQmlContextData *context, QObject *scopeObject, bool ownsContext)
     : Object(QV8Engine::getV4(engine)),
-      v8(engine), readOnly(true), ownsContext(ownsContext),
+      v8(engine), readOnly(true), ownsContext(ownsContext), isNullWrapper(false),
       context(context), scopeObject(scopeObject)
 {
     vtbl = &static_vtbl;
@@ -92,7 +91,8 @@ QV4::Value QmlContextWrapper::urlScope(QV8Engine *v8, const QUrl &url)
     context->isInternal = true;
     context->isJSContext = true;
 
-    QmlContextWrapper *w = new (v4->memoryManager) QmlContextNullWrapper(v8, context, 0);
+    QmlContextWrapper *w = new (v4->memoryManager) QmlContextWrapper(v8, context, 0);
+    w->isNullWrapper = true;
     w->prototype = v4->objectPrototype;
     return Value::fromObject(w);
 }
@@ -132,6 +132,9 @@ Value QmlContextWrapper::get(Managed *m, ExecutionContext *ctx, String *name, bo
     if (!resource)
         ctx->throwTypeError();
 
+    if (resource->isNullWrapper)
+        return Object::get(m, ctx, name, hasProperty);
+
     bool hasProp;
     Value result = Object::get(m, ctx, name, &hasProp);
     if (hasProp) {
@@ -264,6 +267,17 @@ void QmlContextWrapper::put(Managed *m, ExecutionContext *ctx, String *name, con
     if (!wrapper)
         ctx->throwTypeError();
 
+    if (wrapper->isNullWrapper) {
+        if (wrapper && wrapper->readOnly) {
+            QString error = QLatin1String("Invalid write to global property \"") + name->toQString() +
+                            QLatin1Char('"');
+            ctx->throwError(Value::fromString(ctx->engine->newString(error)));
+        }
+
+        Object::put(m, ctx, name, value);
+        return;
+    }
+
     PropertyAttributes attrs;
     Property *pd  = wrapper->__getOwnProperty__(name, &attrs);
     if (pd) {
@@ -321,17 +335,4 @@ void QmlContextWrapper::destroy(Managed *that)
     static_cast<QmlContextWrapper *>(that)->~QmlContextWrapper();
 }
 
-void QmlContextNullWrapper::put(Managed *m, ExecutionContext *ctx, String *name, const Value &value)
-{
-    QmlContextWrapper *w = m->as<QmlContextWrapper>();
-    if (w && w->readOnly) {
-        QString error = QLatin1String("Invalid write to global property \"") + name->toQString() +
-                        QLatin1Char('"');
-        ctx->throwError(Value::fromString(ctx->engine->newString(error)));
-    }
-
-    Object::put(m, ctx, name, value);
-}
-
-
 QT_END_NAMESPACE
index 41026ee..1ef8f74 100644 (file)
@@ -90,24 +90,12 @@ struct Q_QML_EXPORT QmlContextWrapper : Object
     QV8Engine *v8; // ### temporary, remove
     bool readOnly;
     bool ownsContext;
+    bool isNullWrapper;
 
     QQmlGuardedContextData context;
     QQmlGuard<QObject> scopeObject;
 };
 
-struct QmlContextNullWrapper : QmlContextWrapper
-{
-    Q_MANAGED
-    QmlContextNullWrapper(QV8Engine *engine, QQmlContextData *context, QObject *scopeObject, bool ownsContext = false)
-        : QmlContextWrapper(engine, context, scopeObject, ownsContext)
-    {
-        vtbl = &static_vtbl;
-    }
-
-    using Object::get;
-    static void put(Managed *m, ExecutionContext *ctx, String *name, const Value &value);
-};
-
 }
 
 QT_END_NAMESPACE
index 0718f9b..35ccb14 100644 (file)
@@ -52,6 +52,7 @@
 #include <private/qv4functionobject_p.h>
 #include <private/qv4script_p.h>
 #include <private/qv4context_p.h>
+#include <private/qqmlcontextwrapper_p.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -173,7 +174,7 @@ QV4::Value QV4Include::include(QV4::SimpleCallContext *ctx)
 
     QV4::ExecutionEngine *v4 = ctx->engine;
     QV8Engine *engine = v4->v8Engine;
-    QQmlContextData *context = engine->callingContext();
+    QQmlContextData *context = QV4::QmlContextWrapper::callingContext(v4);
 
     if (!context || !context->isJSContext)
         V4THROW_ERROR("Qt.include(): Can only be called from JavaScript files");
index f832cb8..825ab6b 100644 (file)
@@ -300,7 +300,7 @@ QV4::Value QQuickWorkerScriptEnginePrivate::getWorker(WorkerScript *script)
 
         script->object = QV4::QmlContextWrapper::urlScope(workerEngine, script->source);
 
-        QV4::QmlContextWrapper *w = script->object.value().asObject()->as<QV4::QmlContextNullWrapper>();
+        QV4::QmlContextWrapper *w = script->object.value().asObject()->as<QV4::QmlContextWrapper>();
         w->setReadOnly(false);
 
         QV4::Object *api = v4->newObject();