Add support for throwing RangeErrors
authorLars Knoll <lars.knoll@digia.com>
Fri, 11 Jan 2013 08:56:56 +0000 (09:56 +0100)
committerLars Knoll <lars.knoll@digia.com>
Sat, 12 Jan 2013 22:19:00 +0000 (23:19 +0100)
Change-Id: I15bad9d7fe33f9a906d3b2d8da94a969d54d918c
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
qmljs_engine.cpp
qmljs_engine.h
qmljs_environment.cpp
qmljs_environment.h
qmljs_objects.h
qv4ecmaobjects.cpp

index 0d54c77..e8a3389 100644 (file)
@@ -400,6 +400,13 @@ Object *ExecutionEngine::newTypeErrorObject(ExecutionContext *ctx, const QString
     return object;
 }
 
+Object *ExecutionEngine::newRangeErrorObject(ExecutionContext *ctx, const QString &message)
+{
+    RangeErrorObject *object = new (memoryManager) RangeErrorObject(ctx, message);
+    object->prototype = rangeErrorPrototype;
+    return object;
+}
+
 Object *ExecutionEngine::newMathObject(ExecutionContext *ctx)
 {
     MathObject *object = new (memoryManager) MathObject(ctx);
index a2cba07..776363a 100644 (file)
@@ -199,6 +199,7 @@ struct ExecutionEngine
     Object *newSyntaxErrorObject(ExecutionContext *ctx, DiagnosticMessage *message);
     Object *newReferenceErrorObject(ExecutionContext *ctx, const QString &message);
     Object *newTypeErrorObject(ExecutionContext *ctx, const QString &message);
+    Object *newRangeErrorObject(ExecutionContext *ctx, const QString &message);
 
     Object *newMathObject(ExecutionContext *ctx);
     Object *newActivationObject();
index 745554f..067116d 100644 (file)
@@ -385,6 +385,13 @@ void ExecutionContext::throwReferenceError(Value value)
     throwError(Value::fromObject(engine->newReferenceErrorObject(this, msg)));
 }
 
+void ExecutionContext::throwRangeError(Value value)
+{
+    String *s = value.toString(this);
+    QString msg = s->toQString() + QStringLiteral(" out of range");
+    throwError(Value::fromObject(engine->newRangeErrorObject(this, msg)));
+}
+
 void ExecutionContext::initCallContext(ExecutionContext *parent, const Value that, FunctionObject *f, Value *args, unsigned argc)
 {
     MemoryManager::GCBlocker blockGC(parent->engine->memoryManager);
index 5ecc56e..faa04aa 100644 (file)
@@ -118,6 +118,7 @@ struct ExecutionContext
     void throwSyntaxError(DiagnosticMessage *message);
     void throwTypeError();
     void throwReferenceError(Value value);
+    void throwRangeError(Value value);
     void throwUnimplemented(const QString &message);
 
     void setProperty(String *name, Value value);
index b1d55a6..522958b 100644 (file)
@@ -374,6 +374,8 @@ struct EvalErrorObject: ErrorObject {
 struct RangeErrorObject: ErrorObject {
     RangeErrorObject(ExecutionContext *ctx)
         : ErrorObject(ctx->argument(0)) { setNameProperty(ctx); }
+    RangeErrorObject(ExecutionContext *ctx, const QString &msg)
+        : ErrorObject(Value::fromString(ctx,msg)) { setNameProperty(ctx); }
     virtual QString className() { return QStringLiteral("RangeError"); }
 };
 
index e349957..cd4d22d 100644 (file)
@@ -1562,8 +1562,7 @@ Value ArrayCtor::call(ExecutionContext *ctx)
         quint32 isize = Value::toUInt32(size);
 
         if (size != double(isize)) {
-            // ### Should be a RangeError
-            ctx->throwError(QStringLiteral("Invalid array length"));
+            ctx->throwRangeError(ctx->argument(0));
             return Value::undefinedValue();
         }