From ff347ccb093c8be4b44305516e644a17d01d6c30 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 1 Sep 2015 13:46:46 +0200 Subject: Fixes to Error objects Move the name and message property into the prototype as per JS spec. Only define the message property in the object itself if the value used for construction is not undefined. In addition, clean up creation of the objects and centralize it in a few template methods. Change-Id: I014017b710575b30bf4e0b0228111878f5c73b9a Reviewed-by: Simon Hausmann --- src/qml/jsruntime/qv4errorobject_p.h | 67 ++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'src/qml/jsruntime/qv4errorobject_p.h') diff --git a/src/qml/jsruntime/qv4errorobject_p.h b/src/qml/jsruntime/qv4errorobject_p.h index 050979ad53..7f3babaa79 100644 --- a/src/qml/jsruntime/qv4errorobject_p.h +++ b/src/qml/jsruntime/qv4errorobject_p.h @@ -35,6 +35,7 @@ #include "qv4object_p.h" #include "qv4functionobject_p.h" +#include "qv4string_p.h" QT_BEGIN_NAMESPACE @@ -57,8 +58,7 @@ struct ErrorObject : Object { ErrorObject(); ErrorObject(const Value &message, ErrorType t = Error); - ErrorObject(const QString &message, ErrorType t = Error); - ErrorObject(const QString &message, const QString &fileName, int line, int column, ErrorType t = Error); + ErrorObject(const Value &message, const QString &fileName, int line, int column, ErrorType t = Error); ErrorType errorType; StackTrace stackTrace; @@ -71,23 +71,20 @@ struct EvalErrorObject : ErrorObject { struct RangeErrorObject : ErrorObject { RangeErrorObject(const Value &message); - RangeErrorObject(const QString &msg); }; struct ReferenceErrorObject : ErrorObject { ReferenceErrorObject(const Value &message); - ReferenceErrorObject(const QString &msg); - ReferenceErrorObject(const QString &msg, const QString &fileName, int lineNumber, int columnNumber); + ReferenceErrorObject(const Value &msg, const QString &fileName, int lineNumber, int columnNumber); }; struct SyntaxErrorObject : ErrorObject { SyntaxErrorObject(const Value &message); - SyntaxErrorObject(const QString &msg, const QString &fileName, int lineNumber, int columnNumber); + SyntaxErrorObject(const Value &msg, const QString &fileName, int lineNumber, int columnNumber); }; struct TypeErrorObject : ErrorObject { TypeErrorObject(const Value &message); - TypeErrorObject(const QString &msg); }; struct URIErrorObject : ErrorObject { @@ -132,10 +129,9 @@ struct ErrorObject: Object { enum { Index_Stack = 0, // Accessor Property - Index_Message = 2, - Index_Name = 3, - Index_FileName = 4, - Index_LineNumber = 5 + Index_FileName = 2, + Index_LineNumber = 3, + Index_Message = 4 }; V4_OBJECT2(ErrorObject, Object) @@ -144,8 +140,17 @@ struct ErrorObject: Object { V4_PROTOTYPE(errorPrototype) V4_NEEDS_DESTROY + template + static Heap::Object *create(ExecutionEngine *e, const Value &message); + template + static Heap::Object *create(ExecutionEngine *e, const QString &message); + template + static Heap::Object *create(ExecutionEngine *e, const QString &message, const QString &filename, int line, int column); + SyntaxErrorObject *asSyntaxError(); + static const char *className(Heap::ErrorObject::ErrorType t); + static ReturnedValue method_get_stack(CallContext *ctx); static void markObjects(Heap::Base *that, ExecutionEngine *e); }; @@ -248,40 +253,45 @@ struct URIErrorCtor: ErrorCtor struct ErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { init(engine, ctor, this); } + enum { + Index_Constructor = 0, + Index_Message = 1, + Index_Name = 2 + }; + void init(ExecutionEngine *engine, Object *ctor) { init(engine, ctor, this, Heap::ErrorObject::Error); } - static void init(ExecutionEngine *engine, Object *ctor, Object *obj); + static void init(ExecutionEngine *engine, Object *ctor, Object *obj, Heap::ErrorObject::ErrorType t); static ReturnedValue method_toString(CallContext *ctx); }; struct EvalErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this); } + void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this, Heap::ErrorObject::EvalError); } }; struct RangeErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this); } + void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this, Heap::ErrorObject::RangeError); } }; struct ReferenceErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this); } + void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this, Heap::ErrorObject::ReferenceError); } }; struct SyntaxErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this); } + void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this, Heap::ErrorObject::SyntaxError); } }; struct TypeErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this); } + void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this, Heap::ErrorObject::TypeError); } }; struct URIErrorPrototype : ErrorObject { - void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this); } + void init(ExecutionEngine *engine, Object *ctor) { ErrorPrototype::init(engine, ctor, this, Heap::ErrorObject::URIError); } }; @@ -290,6 +300,25 @@ inline SyntaxErrorObject *ErrorObject::asSyntaxError() return d()->errorType == QV4::Heap::ErrorObject::SyntaxError ? static_cast(this) : 0; } + +template +Heap::Object *ErrorObject::create(ExecutionEngine *e, const Value &message) { + return e->memoryManager->allocObject(message.isUndefined() ? e->errorClass : e->errorClassWithMessage, T::defaultPrototype(e), message); +} +template +Heap::Object *ErrorObject::create(ExecutionEngine *e, const QString &message) { + Scope scope(e); + ScopedValue v(scope, message.isEmpty() ? Encode::undefined() : e->newString(message)->asReturnedValue()); + return e->memoryManager->allocObject(v->isUndefined() ? e->errorClass : e->errorClassWithMessage, T::defaultPrototype(e), v); +} +template +Heap::Object *ErrorObject::create(ExecutionEngine *e, const QString &message, const QString &filename, int line, int column) { + Scope scope(e); + ScopedValue v(scope, message.isEmpty() ? Encode::undefined() : e->newString(message)->asReturnedValue()); + return e->memoryManager->allocObject(v->isUndefined() ? e->errorClass : e->errorClassWithMessage, T::defaultPrototype(e), v, filename, line, column); +} + + } QT_END_NAMESPACE -- cgit v1.2.3