Initial implementation of the Error prototype and constructor
authorLars Knoll <lars.knoll@digia.com>
Thu, 1 Nov 2012 14:37:47 +0000 (15:37 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Thu, 1 Nov 2012 14:58:07 +0000 (15:58 +0100)
Change-Id: Iffd1a01b75bc923c0cd8c0b786558be20a52ab2c
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
qmljs_objects.cpp
qmljs_objects.h
qv4ecmaobjects.cpp
qv4ecmaobjects_p.h

index 33dd11e..725c5ac 100644 (file)
@@ -501,6 +501,7 @@ ExecutionEngine::ExecutionEngine()
     datePrototype = new DatePrototype();
     functionPrototype = new FunctionPrototype(rootContext);
     regExpPrototype = new RegExpPrototype();
+    errorPrototype = new ErrorPrototype();
 
     stringPrototype->prototype = objectPrototype;
     numberPrototype->prototype = objectPrototype;
@@ -509,6 +510,7 @@ ExecutionEngine::ExecutionEngine()
     datePrototype->prototype = objectPrototype;
     functionPrototype->prototype = objectPrototype;
     regExpPrototype->prototype = objectPrototype;
+    errorPrototype->prototype = objectPrototype;
 
     objectCtor = Value::fromObject(new ObjectCtor(rootContext));
     stringCtor = Value::fromObject(new StringCtor(rootContext));
@@ -518,6 +520,7 @@ ExecutionEngine::ExecutionEngine()
     functionCtor = Value::fromObject(new FunctionCtor(rootContext));
     dateCtor = Value::fromObject(new DateCtor(rootContext));
     regExpCtor = Value::fromObject(new RegExpCtor(rootContext));
+    errorCtor = Value::fromObject(new ErrorCtor(rootContext));
 
     stringCtor.objectValue()->prototype = functionPrototype;
     numberCtor.objectValue()->prototype = functionPrototype;
@@ -526,6 +529,7 @@ ExecutionEngine::ExecutionEngine()
     functionCtor.objectValue()->prototype = functionPrototype;
     dateCtor.objectValue()->prototype = functionPrototype;
     regExpCtor.objectValue()->prototype = functionPrototype;
+    errorCtor.objectValue()->prototype = functionPrototype;
 
     objectPrototype->init(rootContext, objectCtor);
     stringPrototype->init(rootContext, stringCtor);
@@ -535,6 +539,7 @@ ExecutionEngine::ExecutionEngine()
     datePrototype->init(rootContext, dateCtor);
     functionPrototype->init(rootContext, functionCtor);
     regExpPrototype->init(rootContext, regExpCtor);
+    errorPrototype->init(rootContext, errorCtor);
 
     //
     // set up the global object
@@ -551,6 +556,7 @@ ExecutionEngine::ExecutionEngine()
     glo->__put__(rootContext, identifier(QStringLiteral("Function")), functionCtor);
     glo->__put__(rootContext, identifier(QStringLiteral("Date")), dateCtor);
     glo->__put__(rootContext, identifier(QStringLiteral("RegExp")), regExpCtor);
+    glo->__put__(rootContext, identifier(QStringLiteral("Error")), errorCtor);
     glo->__put__(rootContext, identifier(QStringLiteral("Math")), Value::fromObject(newMathObject(rootContext)));
     glo->__put__(rootContext, identifier(QStringLiteral("undefined")), Value::undefinedValue());
     glo->__put__(rootContext, identifier(QStringLiteral("NaN")), Value::fromDouble(nan("")));
index f7c3507..1b8a4b9 100644 (file)
@@ -82,6 +82,7 @@ struct ArrayPrototype;
 struct FunctionPrototype;
 struct DatePrototype;
 struct RegExpPrototype;
+struct ErrorPrototype;
 
 struct String {
     String(const QString &text)
@@ -549,6 +550,7 @@ struct ExecutionEngine
     Value functionCtor;
     Value dateCtor;
     Value regExpCtor;
+    Value errorCtor;
 
     ObjectPrototype *objectPrototype;
     StringPrototype *stringPrototype;
@@ -558,6 +560,7 @@ struct ExecutionEngine
     FunctionPrototype *functionPrototype;
     DatePrototype *datePrototype;
     RegExpPrototype *regExpPrototype;
+    ErrorPrototype *errorPrototype;
 
     QHash<QString, String *> identifiers;
 
index f7f8455..5d2a6ed 100644 (file)
@@ -2583,6 +2583,67 @@ void RegExpPrototype::method_toString(Context *ctx)
 }
 
 //
+// ErrorCtr
+//
+ErrorCtor::ErrorCtor(Context *scope)
+    : FunctionObject(scope)
+{
+}
+
+void ErrorCtor::construct(Context *ctx)
+{
+    ctx->thisObject = Value::fromObject(new ErrorObject(ctx->argument(0)));
+}
+
+void ErrorCtor::call(Context *ctx)
+{
+    Value that = ctx->thisObject;
+    construct(ctx);
+    ctx->result = ctx->thisObject;
+    ctx->thisObject = that;
+}
+
+void ErrorPrototype::init(Context *ctx, const Value &ctor)
+{
+    ctor.objectValue()->__put__(ctx, ctx->engine->id_prototype, Value::fromObject(this));
+    __put__(ctx, QStringLiteral("constructor"), ctor);
+    __put__(ctx, QStringLiteral("toString"), method_toString, 0);
+}
+
+void ErrorPrototype::method_toString(Context *ctx)
+{
+    Object *o = ctx->thisObject.asObject();
+    if (!o)
+        __qmljs_throw_type_error(ctx);
+
+    String n(QString::fromLatin1("name"));
+    Value name = o->__get__(ctx, &n);
+    QString qname;
+    if (name.isUndefined())
+        qname = QString::fromLatin1("Error");
+    else
+        qname = __qmljs_to_string(name, ctx).stringValue()->toQString();
+
+    String m(QString::fromLatin1("message"));
+    Value message = o->__get__(ctx, &m);
+    QString qmessage;
+    if (!message.isUndefined())
+        qmessage = __qmljs_to_string(message, ctx).stringValue()->toQString();
+
+    QString str;
+    if (qname.isEmpty()) {
+        str = qmessage;
+    } else if (qmessage.isEmpty()) {
+        str = qname;
+    } else {
+        str = qname + QLatin1String(": ") + qmessage;
+    }
+
+    ctx->result = Value::fromString(ctx, str);
+}
+
+
+//
 // Math object
 //
 MathObject::MathObject(Context *ctx)
@@ -2833,3 +2894,4 @@ void MathObject::method_tan(Context *ctx)
     else
         ctx->result = Value::fromDouble(::tan(v));
 }
+
index 6b20e27..3f78fea 100644 (file)
@@ -294,6 +294,23 @@ struct RegExpPrototype: RegExpObject
     static void method_toString(Context *ctx);
 };
 
+struct ErrorCtor: FunctionObject
+{
+    ErrorCtor(Context *scope);
+
+    virtual void construct(Context *ctx);
+    virtual void call(Context *ctx);
+};
+
+struct ErrorPrototype: ErrorObject
+{
+    // ### shouldn't be undefined
+    ErrorPrototype(): ErrorObject(Value::undefinedValue()) {}
+    void init(Context *ctx, const Value &ctor);
+
+    static void method_toString(Context *ctx);
+};
+
 struct MathObject: Object
 {
     MathObject(Context *ctx);