Clean up context initialization
authorSimon Hausmann <simon.hausmann@digia.com>
Mon, 13 May 2013 08:25:01 +0000 (10:25 +0200)
committerLars Knoll <lars.knoll@digia.com>
Mon, 13 May 2013 10:41:40 +0000 (12:41 +0200)
ExecutionContext and its subclasses have various members that are
initialized differently in different call sites. This makes it difficult
to add new members (must find all call sites).

This patch attempts to centralize the member initializations and changes the
usage pattern so that after object creation the corresponding init method needs
to be called before initializing any other members by hand.

Change-Id: I37ac8462ed894c8b234915e114ed032e12bda8bd
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/qml/v4/qv4context.cpp
src/qml/qml/v4/qv4context_p.h
src/qml/qml/v4/qv4engine.cpp
src/qml/qml/v4/qv4functionobject.cpp
src/qml/qml/v4/qv4functionobject_p.h
src/qml/qml/v4/qv4v8.cpp
src/qml/qml/v8/qv8engine.cpp

index 9644b1a..7a5f219 100644 (file)
@@ -125,38 +125,28 @@ unsigned int ExecutionContext::variableCount() const
 }
 
 
-void GlobalContext::init(ExecutionEngine *eng)
+void GlobalContext::initGlobalContext(ExecutionEngine *eng)
 {
-    type = Type_GlobalContext;
-    strictMode = false;
-    marked = false;
+    initBaseContext(Type_GlobalContext, eng);
     thisObject = Value::fromObject(eng->globalObject);
-    engine = eng;
-    outer = 0;
-    lookups = 0;
     global = 0;
 }
 
-void WithContext::init(ExecutionContext *p, Object *with)
+void WithContext::initWithContext(ExecutionContext *p, Object *with)
 {
-    type = Type_WithContext;
-    strictMode = false;
-    marked = false;
+    initBaseContext(Type_WithContext, p->engine);
     thisObject = p->thisObject;
-    engine = p->engine;
     outer = p;
     lookups = p->lookups;
 
     withObject = with;
 }
 
-void CatchContext::init(ExecutionContext *p, String *exceptionVarName, const Value &exceptionValue)
+void CatchContext::initCatchContext(ExecutionContext *p, String *exceptionVarName, const Value &exceptionValue)
 {
-    type = Type_CatchContext;
+    initBaseContext(Type_CatchContext, p->engine);
     strictMode = p->strictMode;
-    marked = false;
     thisObject = p->thisObject;
-    engine = p->engine;
     outer = p;
     lookups = p->lookups;
 
@@ -164,12 +154,17 @@ void CatchContext::init(ExecutionContext *p, String *exceptionVarName, const Val
     this->exceptionValue = exceptionValue;
 }
 
-void CallContext::initCallContext(ExecutionEngine *engine)
+void CallContext::initCallContext(ExecutionEngine *engine, FunctionObject *function, Value *_arguments, int _argumentCount, const Value &_thisObject)
 {
-    type = Type_CallContext;
+    initBaseContext(Type_CallContext, engine);
+
+    this->function = function;
+    this->arguments = _arguments;
+    this->argumentCount = _argumentCount;
+    this->thisObject = _thisObject;
+
     strictMode = function->strictMode;
     marked = false;
-    this->engine = engine;
     outer = function->scope;
 #ifndef QT_NO_DEBUG
     assert(outer->next != (ExecutionContext *)0x1);
index 5b694a2..2b725a6 100644 (file)
@@ -97,6 +97,18 @@ struct Q_QML_EXPORT ExecutionContext
     Lookup *lookups;
     ExecutionContext *next; // used in the GC
 
+    void initBaseContext(Type type, ExecutionEngine *engine)
+    {
+        this->type = type;
+        strictMode = false;
+        marked = false;
+        thisObject = Value::undefinedValue();
+        this->engine = engine;
+        parent = 0;
+        outer = 0;
+        lookups = 0;
+    }
+
     String * const *formals() const;
     unsigned int formalCount() const;
     String * const *variables() const;
@@ -129,6 +141,13 @@ struct Q_QML_EXPORT ExecutionContext
 
 struct SimpleCallContext : public ExecutionContext
 {
+    void initSimpleCallContext(ExecutionEngine *engine)
+    {
+        initBaseContext(Type_SimpleCallContext, engine);
+        function = 0;
+        arguments = 0;
+        argumentCount = 0;
+    }
     FunctionObject *function;
     Value *arguments;
     unsigned int argumentCount;
@@ -136,7 +155,8 @@ struct SimpleCallContext : public ExecutionContext
 
 struct CallContext : public SimpleCallContext
 {
-    void initCallContext(QV4::ExecutionEngine *engine);
+    void initCallContext(QV4::ExecutionEngine *engine, FunctionObject *function, Value *args, int argc,
+                         const Value &thisObject);
     bool needsOwnArguments() const;
 
     Value *locals;
@@ -145,14 +165,14 @@ struct CallContext : public SimpleCallContext
 
 struct GlobalContext : public ExecutionContext
 {
-    void init(ExecutionEngine *e);
+    void initGlobalContext(ExecutionEngine *e);
 
     Object *global;
 };
 
 struct CatchContext : public ExecutionContext
 {
-    void init(ExecutionContext *p, String *exceptionVarName, const QV4::Value &exceptionValue);
+    void initCatchContext(ExecutionContext *p, String *exceptionVarName, const QV4::Value &exceptionValue);
 
     String *exceptionVarName;
     Value exceptionValue;
@@ -162,7 +182,7 @@ struct WithContext : public ExecutionContext
 {
     Object *withObject;
 
-    void init(ExecutionContext *p, Object *with);
+    void initWithContext(ExecutionContext *p, Object *with);
 };
 
 inline Value ExecutionContext::argument(unsigned int index)
index f79ec15..9262436 100644 (file)
@@ -266,7 +266,7 @@ void ExecutionEngine::initRootContext()
     rootContext = static_cast<GlobalContext *>(memoryManager->allocContext(sizeof(GlobalContext)));
     current = rootContext;
     current->parent = 0;
-    rootContext->init(this);
+    rootContext->initGlobalContext(this);
 }
 
 InternalClass *ExecutionEngine::newClass(const InternalClass &other)
@@ -278,10 +278,9 @@ WithContext *ExecutionEngine::newWithContext(Object *with)
 {
     ExecutionContext *p = current;
     WithContext *w = static_cast<WithContext *>(memoryManager->allocContext(sizeof(WithContext)));
+    w->initWithContext(p, with);
     w->parent = current;
     current = w;
-
-    w->init(p, with);
     return w;
 }
 
@@ -289,25 +288,20 @@ CatchContext *ExecutionEngine::newCatchContext(String *exceptionVarName, const V
 {
     ExecutionContext *p = current;
     CatchContext *c = static_cast<CatchContext *>(memoryManager->allocContext(sizeof(CatchContext)));
+    c->initCatchContext(p, exceptionVarName, exceptionValue);
     c->parent = current;
     current = c;
-
-    c->init(p, exceptionVarName, exceptionValue);
     return c;
 }
 
 CallContext *ExecutionEngine::newCallContext(FunctionObject *f, const Value &thisObject, Value *args, int argc)
 {
     CallContext *c = static_cast<CallContext *>(memoryManager->allocContext(requiredMemoryForExecutionContect(f, argc)));
+
+    c->initCallContext(this, f, args, argc, thisObject);
     c->parent = current;
     current = c;
 
-    c->function = f;
-    c->thisObject = thisObject;
-    c->arguments = args;
-    c->argumentCount = argc;
-    c->initCallContext(this);
-
     return c;
 }
 
@@ -323,15 +317,11 @@ CallContext *ExecutionEngine::newCallContext(void *stackSpace, FunctionObject *f
         c->next = (CallContext *)0x1;
 #endif
     }
+
+    c->initCallContext(this, f, args, argc, thisObject);
     c->parent = current;
     current = c;
 
-    c->function = f;
-    c->thisObject = thisObject;
-    c->arguments = args;
-    c->argumentCount = argc;
-    c->initCallContext(this);
-
     return c;
 }
 
index 6a94e21..351e36b 100644 (file)
@@ -419,11 +419,9 @@ Value BuiltinFunctionOld::call(Managed *that, ExecutionContext *context, const V
 {
     BuiltinFunctionOld *f = static_cast<BuiltinFunctionOld *>(that);
     SimpleCallContext ctx;
-    ctx.type = ExecutionContext::Type_SimpleCallContext;
+    ctx.initSimpleCallContext(f->scope->engine);
     ctx.strictMode = f->scope->strictMode; // ### needed? scope or parent context?
-    ctx.marked = false;
     ctx.thisObject = thisObject;
-    ctx.engine = f->scope->engine;
     ctx.arguments = args;
     ctx.argumentCount = argc;
     context->engine->pushContext(&ctx);
index 5c62975..6360ef6 100644 (file)
@@ -216,11 +216,9 @@ public:
         T *o = reinterpret_cast<T *>(thisO);
 
         QV4::SimpleCallContext ctx;
-        ctx.type = ExecutionContext::Type_SimpleCallContext;
+        ctx.initSimpleCallContext(context->engine);
         ctx.strictMode = true;
-        ctx.marked = false;
         ctx.thisObject = thisObject;
-        ctx.engine = context->engine;
         ctx.arguments = args;
         ctx.argumentCount = argc;
         context->engine->pushContext(&ctx);
index 4d1f38b..6d7f971 100644 (file)
@@ -1677,11 +1677,9 @@ protected:
             result = that->m_functionTemplate->m_callback(arguments);
         else if (that->m_functionTemplate->m_newCallback) {
             QV4::SimpleCallContext ctx;
-            ctx.type = ExecutionContext::Type_SimpleCallContext;
+            ctx.initSimpleCallContext(context->engine);
             ctx.strictMode = true;
-            ctx.marked = false;
             ctx.thisObject = thisObject;
-            ctx.engine = context->engine;
             ctx.arguments = args;
             ctx.argumentCount = argc;
             context->engine->pushContext(&ctx);
@@ -1711,11 +1709,8 @@ protected:
             result = that->m_functionTemplate->m_callback(arguments);
         else if (that->m_functionTemplate->m_newCallback) {
             QV4::SimpleCallContext ctx;
-            ctx.type = ExecutionContext::Type_SimpleCallContext;
+            ctx.initSimpleCallContext(context->engine);
             ctx.strictMode = true;
-            ctx.marked = false;
-            ctx.thisObject = QV4::Value::undefinedValue();
-            ctx.engine = context->engine;
             ctx.arguments = args;
             ctx.argumentCount = argc;
             context->engine->pushContext(&ctx);
index 94cc319..e4850df 100644 (file)
@@ -468,11 +468,10 @@ QV4::Value QV8Engine::getOwnPropertyNames(const QV4::Value &o)
     if (!o.asObject())
         return QV4::Value::fromObject(m_v4Engine->newArrayObject());
     QV4::SimpleCallContext ctx;
-    ctx.type = QV4::ExecutionContext::Type_SimpleCallContext;
+    ctx.initSimpleCallContext(m_v4Engine);
     QV4::Value args = o;
     ctx.arguments = &args;
     ctx.argumentCount = 1;
-    ctx.engine = m_v4Engine;
     m_v4Engine->pushContext(&ctx);
     QV4::Value result = QV4::ObjectPrototype::method_getOwnPropertyNames(&ctx);
     m_v4Engine->popContext();