Move the destructor into the new vtable.
authorLars Knoll <lars.knoll@digia.com>
Thu, 14 Feb 2013 14:00:06 +0000 (15:00 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Thu, 14 Feb 2013 14:05:47 +0000 (15:05 +0100)
This makes all runtime structures fully non virtual.

Change-Id: I804568ca9bc33d4be0324ed542df8eab5892c0eb
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
15 files changed:
src/v4/qv4argumentsobject.cpp
src/v4/qv4argumentsobject.h
src/v4/qv4errorobject.cpp
src/v4/qv4errorobject.h
src/v4/qv4functionobject.cpp
src/v4/qv4functionobject.h
src/v4/qv4managed.cpp
src/v4/qv4managed.h
src/v4/qv4mm.cpp
src/v4/qv4object.cpp
src/v4/qv4object.h
src/v4/qv4regexpobject.cpp
src/v4/qv4regexpobject.h
src/v4/qv4string.cpp
src/v4/qv4string.h

index d1791e0..f2afe19 100644 (file)
@@ -89,6 +89,11 @@ ArgumentsObject::ArgumentsObject(ExecutionContext *context, int formalParameterC
     }
 }
 
+void ArgumentsObject::destroy(Managed *that)
+{
+    static_cast<ArgumentsObject *>(that)->~ArgumentsObject();
+}
+
 bool ArgumentsObject::defineOwnProperty(ExecutionContext *ctx, uint index, const PropertyDescriptor *desc)
 {
     PropertyDescriptor *pd = arrayAt(index);
index acbcc75..13f6b40 100644 (file)
@@ -80,11 +80,14 @@ struct ArgumentsObject: Object {
     ExecutionContext *context;
     QVector<Value> mappedArguments;
     ArgumentsObject(ExecutionContext *context, int formalParameterCount, int actualParameterCount);
+    ~ArgumentsObject() {}
 
     bool defineOwnProperty(ExecutionContext *ctx, uint index, const PropertyDescriptor *desc);
 
-    static const ManagedVTable static_vtbl;
     static void markObjects(Managed *that);
+protected:
+    static const ManagedVTable static_vtbl;
+    static void destroy(Managed *);
 };
 
 }
index 418dbb7..addf163 100644 (file)
@@ -88,10 +88,13 @@ void ErrorObject::setNameProperty(ExecutionContext *ctx)
     defineDefaultProperty(ctx, QLatin1String("name"), Value::fromString(ctx, className()));
 }
 
+DEFINE_MANAGED_VTABLE(SyntaxErrorObject);
+
 SyntaxErrorObject::SyntaxErrorObject(ExecutionContext *ctx, DiagnosticMessage *message)
     : ErrorObject(ctx->engine, message ? Value::fromString(message->buildFullMessage(ctx)) : ctx->argument(0))
     , msg(message)
 {
+    vtbl = &static_vtbl;
     subtype = SyntaxError;
     prototype = ctx->engine->syntaxErrorPrototype;
     setNameProperty(ctx);
index ae132a9..c03d822 100644 (file)
@@ -87,11 +87,14 @@ struct ReferenceErrorObject: ErrorObject {
 struct SyntaxErrorObject: ErrorObject {
     SyntaxErrorObject(ExecutionContext *ctx, DiagnosticMessage *msg);
     ~SyntaxErrorObject() { delete msg; }
+    static void destroy(Managed *that) { static_cast<SyntaxErrorObject *>(that)->~SyntaxErrorObject(); }
 
     DiagnosticMessage *message() { return msg; }
 
 private:
     DiagnosticMessage *msg;
+protected:
+    static const ManagedVTable static_vtbl;
 };
 
 struct TypeErrorObject: ErrorObject {
index caa6987..af23182 100644 (file)
@@ -355,10 +355,6 @@ ScriptFunction::ScriptFunction(ExecutionContext *scope, Function *function)
     }
 }
 
-ScriptFunction::~ScriptFunction()
-{
-}
-
 Value ScriptFunction::construct(Managed *that, ExecutionContext *context, Value *args, int argc)
 {
     ScriptFunction *f = static_cast<ScriptFunction *>(that);
@@ -510,6 +506,11 @@ BoundFunction::BoundFunction(ExecutionContext *scope, FunctionObject *target, Va
     *insertMember(scope->engine->id_caller) = pd;
 }
 
+void BoundFunction::destroy(Managed *that)
+{
+    static_cast<BoundFunction *>(that)->~BoundFunction();
+}
+
 Value BoundFunction::call(Managed *that, ExecutionContext *context, const Value &, Value *args, int argc)
 {
     BoundFunction *f = static_cast<BoundFunction *>(that);
index cf99b78..ebf14f8 100644 (file)
@@ -219,7 +219,6 @@ protected:
 
 struct ScriptFunction: FunctionObject {
     ScriptFunction(ExecutionContext *scope, VM::Function *function);
-    ~ScriptFunction();
 
     static Value construct(Managed *, ExecutionContext *context, Value *args, int argc);
     static Value call(Managed *that, ExecutionContext *, const Value &, Value *, int);
@@ -236,10 +235,12 @@ struct BoundFunction: FunctionObject {
     BoundFunction(ExecutionContext *scope, FunctionObject *target, Value boundThis, const QVector<Value> &boundArgs);
     ~BoundFunction() {}
 
+
     static Value construct(Managed *, ExecutionContext *context, Value *args, int argc);
     static Value call(Managed *that, ExecutionContext *, const Value &, Value *, int);
 
     static const ManagedVTable static_vtbl;
+    static void destroy(Managed *);
     static void markObjects(Managed *that);
     static bool hasInstance(Managed *that, ExecutionContext *ctx, const Value &value);
 };
index ea4004e..f458565 100644 (file)
@@ -47,20 +47,15 @@ using namespace QQmlJS::VM;
 
 const ManagedVTable Managed::static_vtbl =
 {
-    "Managed",
+    call,
+    construct,
     0 /*markObjects*/,
+    destroy,
     hasInstance,
-    construct,
-    call,
-    ManagedVTable::EndOfVTable
+    "Managed",
 };
 
 
-Managed::~Managed()
-{
-    _data = 0;
-}
-
 void *Managed::operator new(size_t size, MemoryManager *mm)
 {
     assert(mm);
index 54c8cf4..94aaaf3 100644 (file)
@@ -76,26 +76,23 @@ struct Value;
 
 struct ManagedVTable
 {
-    enum _EndOfVTable {
-        EndOfVTable
-    };
-    const char *className;
+    Value (*call)(Managed *, ExecutionContext *context, const Value &thisObject, Value *args, int argc);
+    Value (*construct)(Managed *, ExecutionContext *context, Value *args, int argc);
     void (*markObjects)(Managed *);
+    void (*destroy)(Managed *);
     bool (*hasInstance)(Managed *, ExecutionContext *ctx, const Value &value);
-    Value (*construct)(Managed *, ExecutionContext *context, Value *args, int argc);
-    Value (*call)(Managed *, ExecutionContext *context, const Value &thisObject, Value *args, int argc);
-    _EndOfVTable endofVTable;
+    const char *className;
 };
 
 #define DEFINE_MANAGED_VTABLE(classname) \
 const ManagedVTable classname::static_vtbl =    \
 {                                               \
-    #classname,                                 \
+    call,                                       \
+    construct,                                  \
     markObjects,                                \
+    destroy,                                    \
     hasInstance,                                \
-    construct,                                  \
-    call,                                       \
-    ManagedVTable::EndOfVTable                  \
+    #classname                                  \
 }
 
 
@@ -110,7 +107,6 @@ protected:
     Managed()
         : vtbl(&static_vtbl), _data(0)
     { inUse = 1; extensible = 1; }
-    virtual ~Managed();
 
 public:
     void *operator new(size_t size, MemoryManager *mm);
@@ -177,6 +173,7 @@ public:
     inline Value construct(ExecutionContext *context, Value *args, int argc);
     inline Value call(ExecutionContext *context, const Value &thisObject, Value *args, int argc);
 
+    static void destroy(Managed *that) { that->_data = 0; }
     static bool hasInstance(Managed *that, ExecutionContext *ctx, const Value &value);
     static Value construct(Managed *, ExecutionContext *context, Value *, int);
     static Value call(Managed *, ExecutionContext *, const Value &, Value *, int);
@@ -184,6 +181,7 @@ public:
 protected:
 
     static const ManagedVTable static_vtbl;
+
     const ManagedVTable *vtbl;
 
     union {
index 81dd39a..b81456b 100644 (file)
@@ -229,7 +229,7 @@ std::size_t MemoryManager::sweep(char *chunkStart, std::size_t chunkSize, size_t
                 m->markBit = 0;
             } else {
 //                qDebug() << "-- collecting it." << m << *f << m->nextFree();
-                m->~Managed();
+                m->vtbl->destroy(m);
 
                 m->setNextFree(*f);
                 *f = m;
index 0d68f9e..16ce621 100644 (file)
@@ -82,6 +82,12 @@ Object::~Object()
     delete [] memberData;
     delete [] (arrayData - (sparseArray ? 0 : arrayOffset));
     delete sparseArray;
+    _data = 0;
+}
+
+void Object::destroy(Managed *that)
+{
+    static_cast<Object *>(that)->~Object();
 }
 
 void Object::__put__(ExecutionContext *ctx, const QString &name, const Value &value)
index 1b29daf..62283bc 100644 (file)
@@ -305,6 +305,7 @@ public:
 
 protected:
     static const ManagedVTable static_vtbl;
+    static void destroy(Managed *that);
     static void markObjects(Managed *that);
 
     friend struct ObjectIterator;
index ccb4d3b..d9abd51 100644 (file)
@@ -63,6 +63,7 @@
 
 using namespace QQmlJS::VM;
 
+DEFINE_MANAGED_VTABLE(RegExpObject);
 
 RegExpObject::RegExpObject(ExecutionEngine *engine, PassRefPtr<RegExp> value, bool global)
     : Object(engine)
@@ -85,6 +86,11 @@ RegExpObject::RegExpObject(ExecutionEngine *engine, PassRefPtr<RegExp> value, bo
     defineReadonlyProperty(engine->newIdentifier(QStringLiteral("multiline")), Value::fromBoolean(this->value->multiLine()));
 }
 
+void RegExpObject::destroy(Managed *that)
+{
+    static_cast<RegExpObject *>(that)->~RegExpObject();
+}
+
 PropertyDescriptor *RegExpObject::lastIndexProperty(ExecutionContext *ctx)
 {
     assert(0 == internalClass->find(ctx->engine->newIdentifier(QStringLiteral("lastIndex"))));
index 65df03c..c9cdb80 100644 (file)
@@ -69,6 +69,11 @@ struct RegExpObject: Object {
     PropertyDescriptor *lastIndexProperty(ExecutionContext *ctx);
     bool global;
     RegExpObject(ExecutionEngine *engine, PassRefPtr<RegExp> value, bool global);
+    ~RegExpObject() {}
+
+protected:
+    static const ManagedVTable static_vtbl;
+    static void destroy(Managed *that);
 };
 
 
index 22b7354..20656ce 100644 (file)
@@ -73,6 +73,21 @@ static uint toArrayIndex(const QChar *ch, const QChar *end, bool *ok)
     return i;
 }
 
+const ManagedVTable String::static_vtbl =
+{
+    call,
+    construct,
+    0 /*markObjects*/,
+    destroy,
+    hasInstance,
+    "String",
+};
+
+void String::destroy(Managed *that)
+{
+    static_cast<String*>(that)->~String();
+}
+
 uint String::toUInt(bool *ok) const
 {
     *ok = true;
index 81566bd..eecaa53 100644 (file)
@@ -61,7 +61,8 @@ struct String : public Managed {
 
     String(const QString &text)
         : _text(text), stringHash(UINT_MAX), identifier(UINT_MAX)
-    { type = Type_String; subtype = StringType_Unknown; }
+    { vtbl = &static_vtbl; type = Type_String; subtype = StringType_Unknown; }
+    ~String() { _data = 0; }
 
     inline bool isEqualTo(const String *other) const {
         if (this == other)
@@ -108,6 +109,10 @@ struct String : public Managed {
     QString _text;
     mutable uint stringHash;
     mutable uint identifier;
+
+protected:
+    static void destroy(Managed *);
+    static const ManagedVTable static_vtbl;
 };
 
 } // namespace VM