diff options
-rw-r--r-- | qmljs_objects.cpp | 6 | ||||
-rw-r--r-- | qmljs_objects.h | 3 | ||||
-rw-r--r-- | qv4ecmaobjects.cpp | 62 | ||||
-rw-r--r-- | qv4ecmaobjects_p.h | 17 |
4 files changed, 88 insertions, 0 deletions
diff --git a/qmljs_objects.cpp b/qmljs_objects.cpp index 33dd11edb7..725c5aceb9 100644 --- a/qmljs_objects.cpp +++ b/qmljs_objects.cpp @@ -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(""))); diff --git a/qmljs_objects.h b/qmljs_objects.h index f7c35076ea..1b8a4b9fe3 100644 --- a/qmljs_objects.h +++ b/qmljs_objects.h @@ -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; diff --git a/qv4ecmaobjects.cpp b/qv4ecmaobjects.cpp index f7f8455f49..5d2a6edf38 100644 --- a/qv4ecmaobjects.cpp +++ b/qv4ecmaobjects.cpp @@ -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)); } + diff --git a/qv4ecmaobjects_p.h b/qv4ecmaobjects_p.h index 6b20e27eb1..3f78feaa64 100644 --- a/qv4ecmaobjects_p.h +++ b/qv4ecmaobjects_p.h @@ -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); |