Move Number object into it's own file
authorLars Knoll <lars.knoll@digia.com>
Mon, 21 Jan 2013 21:12:05 +0000 (22:12 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Mon, 21 Jan 2013 21:56:00 +0000 (22:56 +0100)
Change-Id: Ie431e653956efa35dc92523e236af1097d57bab9
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
qmljs_engine.cpp
qv4ecmaobjects.cpp
qv4ecmaobjects_p.h
qv4numberobject.cpp [new file with mode: 0644]
qv4numberobject.h [new file with mode: 0644]
v4.pro

index 0e9abb5..982b7e5 100644 (file)
@@ -46,6 +46,7 @@
 #include <qv4errorobject.h>
 #include <qv4functionobject.h>
 #include <qv4mathobject.h>
+#include <qv4numberobject.h>
 #include <qv4regexpobject.h>
 #include <qmljs_runtime.h>
 #include "qv4mm.h"
index e64852e..3db6297 100644 (file)
@@ -559,185 +559,6 @@ Value ObjectPrototype::fromPropertyDescriptor(ExecutionContext *ctx, const Prope
 
 
 //
-// Number object
-//
-NumberCtor::NumberCtor(ExecutionContext *scope)
-    : FunctionObject(scope)
-{
-}
-
-Value NumberCtor::construct(ExecutionContext *ctx)
-{
-    double d = ctx->argumentCount ? ctx->argument(0).toNumber(ctx) : 0;
-    return Value::fromObject(ctx->engine->newNumberObject(Value::fromDouble(d)));
-}
-
-Value NumberCtor::call(ExecutionContext *ctx)
-{
-    double d = ctx->argumentCount ? ctx->argument(0).toNumber(ctx) : 0;
-    return Value::fromDouble(d);
-}
-
-void NumberPrototype::init(ExecutionContext *ctx, const Value &ctor)
-{
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine->id_prototype, Value::fromObject(this));
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine->id_length, Value::fromInt32(1));
-
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("NaN"), Value::fromDouble(qSNaN()));
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("NEGATIVE_INFINITY"), Value::fromDouble(-qInf()));
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("POSITIVE_INFINITY"), Value::fromDouble(qInf()));
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("MAX_VALUE"), Value::fromDouble(1.7976931348623158e+308));
-
-#ifdef __INTEL_COMPILER
-# pragma warning( push )
-# pragma warning(disable: 239)
-#endif
-    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("MIN_VALUE"), Value::fromDouble(5e-324));
-#ifdef __INTEL_COMPILER
-# pragma warning( pop )
-#endif
-
-    defineDefaultProperty(ctx, QStringLiteral("constructor"), ctor);
-    defineDefaultProperty(ctx, QStringLiteral("toString"), method_toString);
-    defineDefaultProperty(ctx, QStringLiteral("toLocalString"), method_toLocaleString);
-    defineDefaultProperty(ctx, QStringLiteral("valueOf"), method_valueOf);
-    defineDefaultProperty(ctx, QStringLiteral("toFixed"), method_toFixed, 1);
-    defineDefaultProperty(ctx, QStringLiteral("toExponential"), method_toExponential);
-    defineDefaultProperty(ctx, QStringLiteral("toPrecision"), method_toPrecision);
-}
-
-Value NumberPrototype::method_toString(ExecutionContext *ctx)
-{
-    NumberObject *thisObject = ctx->thisObject.asNumberObject();
-    if (!thisObject)
-        ctx->throwTypeError();
-
-    Value arg = ctx->argument(0);
-    if (!arg.isUndefined()) {
-        int radix = arg.toInt32(ctx);
-        if (radix < 2 || radix > 36) {
-            ctx->throwError(QString::fromLatin1("Number.prototype.toString: %0 is not a valid radix")
-                            .arg(radix));
-            return Value::undefinedValue();
-        }
-
-        double num = thisObject->value.asDouble();
-        if (std::isnan(num)) {
-            return Value::fromString(ctx, QStringLiteral("NaN"));
-        } else if (qIsInf(num)) {
-            return Value::fromString(ctx, QLatin1String(num < 0 ? "-Infinity" : "Infinity"));
-        }
-
-        if (radix != 10) {
-            QString str;
-            bool negative = false;
-            if (num < 0) {
-                negative = true;
-                num = -num;
-            }
-            double frac = num - ::floor(num);
-            num = Value::toInteger(num);
-            do {
-                char c = (char)::fmod(num, radix);
-                c = (c < 10) ? (c + '0') : (c - 10 + 'a');
-                str.prepend(QLatin1Char(c));
-                num = ::floor(num / radix);
-            } while (num != 0);
-            if (frac != 0) {
-                str.append(QLatin1Char('.'));
-                do {
-                    frac = frac * radix;
-                    char c = (char)::floor(frac);
-                    c = (c < 10) ? (c + '0') : (c - 10 + 'a');
-                    str.append(QLatin1Char(c));
-                    frac = frac - ::floor(frac);
-                } while (frac != 0);
-            }
-            if (negative)
-                str.prepend(QLatin1Char('-'));
-            return Value::fromString(ctx, str);
-        }
-    }
-
-    Value internalValue = thisObject->value;
-    String *str = internalValue.toString(ctx);
-    return Value::fromString(str);
-}
-
-Value NumberPrototype::method_toLocaleString(ExecutionContext *ctx)
-{
-    NumberObject *thisObject = ctx->thisObject.asNumberObject();
-    if (!thisObject)
-        ctx->throwTypeError();
-
-    String *str = thisObject->value.toString(ctx);
-    return Value::fromString(str);
-}
-
-Value NumberPrototype::method_valueOf(ExecutionContext *ctx)
-{
-    NumberObject *thisObject = ctx->thisObject.asNumberObject();
-    if (!thisObject)
-        ctx->throwTypeError();
-
-    return thisObject->value;
-}
-
-Value NumberPrototype::method_toFixed(ExecutionContext *ctx)
-{
-    NumberObject *thisObject = ctx->thisObject.asNumberObject();
-    if (!thisObject)
-        ctx->throwTypeError();
-
-    double fdigits = 0;
-
-    if (ctx->argumentCount > 0)
-        fdigits = ctx->argument(0).toInteger(ctx);
-
-    if (std::isnan(fdigits))
-        fdigits = 0;
-
-    double v = thisObject->value.asDouble();
-    QString str;
-    if (std::isnan(v))
-        str = QString::fromLatin1("NaN");
-    else if (qIsInf(v))
-        str = QString::fromLatin1(v < 0 ? "-Infinity" : "Infinity");
-    else
-        str = QString::number(v, 'f', int (fdigits));
-    return Value::fromString(ctx, str);
-}
-
-Value NumberPrototype::method_toExponential(ExecutionContext *ctx)
-{
-    NumberObject *thisObject = ctx->thisObject.asNumberObject();
-    if (!thisObject)
-        ctx->throwTypeError();
-
-    double fdigits = 0;
-
-    if (ctx->argumentCount > 0)
-        fdigits = ctx->argument(0).toInteger(ctx);
-
-    QString z = QString::number(thisObject->value.asDouble(), 'e', int (fdigits));
-    return Value::fromString(ctx, z);
-}
-
-Value NumberPrototype::method_toPrecision(ExecutionContext *ctx)
-{
-    NumberObject *thisObject = ctx->thisObject.asNumberObject();
-    if (!thisObject)
-        ctx->throwTypeError();
-
-    double fdigits = 0;
-
-    if (ctx->argumentCount > 0)
-        fdigits = ctx->argument(0).toInteger(ctx);
-
-    return Value::fromString(ctx, QString::number(thisObject->value.asDouble(), 'g', int (fdigits)));
-}
-
-//
 // Boolean object
 //
 BooleanCtor::BooleanCtor(ExecutionContext *scope)
index 12cf99b..1dcf82b 100644 (file)
@@ -88,27 +88,6 @@ struct ObjectPrototype: Object
     static Value fromPropertyDescriptor(ExecutionContext *ctx, const PropertyDescriptor *desc);
 };
 
-struct NumberCtor: FunctionObject
-{
-    NumberCtor(ExecutionContext *scope);
-
-    virtual Value construct(ExecutionContext *ctx);
-    virtual Value call(ExecutionContext *ctx);
-};
-
-struct NumberPrototype: NumberObject
-{
-    NumberPrototype(): NumberObject(Value::fromDouble(0)) {}
-    void init(ExecutionContext *ctx, const Value &ctor);
-
-    static Value method_toString(ExecutionContext *ctx);
-    static Value method_toLocaleString(ExecutionContext *ctx);
-    static Value method_valueOf(ExecutionContext *ctx);
-    static Value method_toFixed(ExecutionContext *ctx);
-    static Value method_toExponential(ExecutionContext *ctx);
-    static Value method_toPrecision(ExecutionContext *ctx);
-};
-
 struct BooleanCtor: FunctionObject
 {
     BooleanCtor(ExecutionContext *scope);
diff --git a/qv4numberobject.cpp b/qv4numberobject.cpp
new file mode 100644 (file)
index 0000000..1926e5a
--- /dev/null
@@ -0,0 +1,226 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the V4VM module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qv4numberobject.h"
+#include <QtCore/qnumeric.h>
+#include <QtCore/qmath.h>
+#include <QtCore/QDebug>
+#include <cassert>
+
+
+using namespace QQmlJS::VM;
+
+
+NumberCtor::NumberCtor(ExecutionContext *scope)
+    : FunctionObject(scope)
+{
+}
+
+Value NumberCtor::construct(ExecutionContext *ctx)
+{
+    double d = ctx->argumentCount ? ctx->argument(0).toNumber(ctx) : 0;
+    return Value::fromObject(ctx->engine->newNumberObject(Value::fromDouble(d)));
+}
+
+Value NumberCtor::call(ExecutionContext *ctx)
+{
+    double d = ctx->argumentCount ? ctx->argument(0).toNumber(ctx) : 0;
+    return Value::fromDouble(d);
+}
+
+void NumberPrototype::init(ExecutionContext *ctx, const Value &ctor)
+{
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine->id_prototype, Value::fromObject(this));
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine->id_length, Value::fromInt32(1));
+
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("NaN"), Value::fromDouble(qSNaN()));
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("NEGATIVE_INFINITY"), Value::fromDouble(-qInf()));
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("POSITIVE_INFINITY"), Value::fromDouble(qInf()));
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("MAX_VALUE"), Value::fromDouble(1.7976931348623158e+308));
+
+#ifdef __INTEL_COMPILER
+# pragma warning( push )
+# pragma warning(disable: 239)
+#endif
+    ctor.objectValue()->defineReadonlyProperty(ctx->engine, QStringLiteral("MIN_VALUE"), Value::fromDouble(5e-324));
+#ifdef __INTEL_COMPILER
+# pragma warning( pop )
+#endif
+
+    defineDefaultProperty(ctx, QStringLiteral("constructor"), ctor);
+    defineDefaultProperty(ctx, QStringLiteral("toString"), method_toString);
+    defineDefaultProperty(ctx, QStringLiteral("toLocalString"), method_toLocaleString);
+    defineDefaultProperty(ctx, QStringLiteral("valueOf"), method_valueOf);
+    defineDefaultProperty(ctx, QStringLiteral("toFixed"), method_toFixed, 1);
+    defineDefaultProperty(ctx, QStringLiteral("toExponential"), method_toExponential);
+    defineDefaultProperty(ctx, QStringLiteral("toPrecision"), method_toPrecision);
+}
+
+Value NumberPrototype::method_toString(ExecutionContext *ctx)
+{
+    NumberObject *thisObject = ctx->thisObject.asNumberObject();
+    if (!thisObject)
+        ctx->throwTypeError();
+
+    Value arg = ctx->argument(0);
+    if (!arg.isUndefined()) {
+        int radix = arg.toInt32(ctx);
+        if (radix < 2 || radix > 36) {
+            ctx->throwError(QString::fromLatin1("Number.prototype.toString: %0 is not a valid radix")
+                            .arg(radix));
+            return Value::undefinedValue();
+        }
+
+        double num = thisObject->value.asDouble();
+        if (std::isnan(num)) {
+            return Value::fromString(ctx, QStringLiteral("NaN"));
+        } else if (qIsInf(num)) {
+            return Value::fromString(ctx, QLatin1String(num < 0 ? "-Infinity" : "Infinity"));
+        }
+
+        if (radix != 10) {
+            QString str;
+            bool negative = false;
+            if (num < 0) {
+                negative = true;
+                num = -num;
+            }
+            double frac = num - ::floor(num);
+            num = Value::toInteger(num);
+            do {
+                char c = (char)::fmod(num, radix);
+                c = (c < 10) ? (c + '0') : (c - 10 + 'a');
+                str.prepend(QLatin1Char(c));
+                num = ::floor(num / radix);
+            } while (num != 0);
+            if (frac != 0) {
+                str.append(QLatin1Char('.'));
+                do {
+                    frac = frac * radix;
+                    char c = (char)::floor(frac);
+                    c = (c < 10) ? (c + '0') : (c - 10 + 'a');
+                    str.append(QLatin1Char(c));
+                    frac = frac - ::floor(frac);
+                } while (frac != 0);
+            }
+            if (negative)
+                str.prepend(QLatin1Char('-'));
+            return Value::fromString(ctx, str);
+        }
+    }
+
+    Value internalValue = thisObject->value;
+    String *str = internalValue.toString(ctx);
+    return Value::fromString(str);
+}
+
+Value NumberPrototype::method_toLocaleString(ExecutionContext *ctx)
+{
+    NumberObject *thisObject = ctx->thisObject.asNumberObject();
+    if (!thisObject)
+        ctx->throwTypeError();
+
+    String *str = thisObject->value.toString(ctx);
+    return Value::fromString(str);
+}
+
+Value NumberPrototype::method_valueOf(ExecutionContext *ctx)
+{
+    NumberObject *thisObject = ctx->thisObject.asNumberObject();
+    if (!thisObject)
+        ctx->throwTypeError();
+
+    return thisObject->value;
+}
+
+Value NumberPrototype::method_toFixed(ExecutionContext *ctx)
+{
+    NumberObject *thisObject = ctx->thisObject.asNumberObject();
+    if (!thisObject)
+        ctx->throwTypeError();
+
+    double fdigits = 0;
+
+    if (ctx->argumentCount > 0)
+        fdigits = ctx->argument(0).toInteger(ctx);
+
+    if (std::isnan(fdigits))
+        fdigits = 0;
+
+    double v = thisObject->value.asDouble();
+    QString str;
+    if (std::isnan(v))
+        str = QString::fromLatin1("NaN");
+    else if (qIsInf(v))
+        str = QString::fromLatin1(v < 0 ? "-Infinity" : "Infinity");
+    else
+        str = QString::number(v, 'f', int (fdigits));
+    return Value::fromString(ctx, str);
+}
+
+Value NumberPrototype::method_toExponential(ExecutionContext *ctx)
+{
+    NumberObject *thisObject = ctx->thisObject.asNumberObject();
+    if (!thisObject)
+        ctx->throwTypeError();
+
+    double fdigits = 0;
+
+    if (ctx->argumentCount > 0)
+        fdigits = ctx->argument(0).toInteger(ctx);
+
+    QString z = QString::number(thisObject->value.asDouble(), 'e', int (fdigits));
+    return Value::fromString(ctx, z);
+}
+
+Value NumberPrototype::method_toPrecision(ExecutionContext *ctx)
+{
+    NumberObject *thisObject = ctx->thisObject.asNumberObject();
+    if (!thisObject)
+        ctx->throwTypeError();
+
+    double fdigits = 0;
+
+    if (ctx->argumentCount > 0)
+        fdigits = ctx->argument(0).toInteger(ctx);
+
+    return Value::fromString(ctx, QString::number(thisObject->value.asDouble(), 'g', int (fdigits)));
+}
diff --git a/qv4numberobject.h b/qv4numberobject.h
new file mode 100644 (file)
index 0000000..85f9e1f
--- /dev/null
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the V4VM module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#ifndef QV4NUMBEROBJECT_H
+#define QV4NUMBEROBJECT_H
+
+#include "qv4object.h"
+#include "qv4functionobject.h"
+#include <QtCore/qnumeric.h>
+
+namespace QQmlJS {
+namespace VM {
+
+struct NumberCtor: FunctionObject
+{
+    NumberCtor(ExecutionContext *scope);
+
+    virtual Value construct(ExecutionContext *ctx);
+    virtual Value call(ExecutionContext *ctx);
+};
+
+struct NumberPrototype: NumberObject
+{
+    NumberPrototype(): NumberObject(Value::fromDouble(0)) {}
+    void init(ExecutionContext *ctx, const Value &ctor);
+
+    static Value method_toString(ExecutionContext *ctx);
+    static Value method_toLocaleString(ExecutionContext *ctx);
+    static Value method_valueOf(ExecutionContext *ctx);
+    static Value method_toFixed(ExecutionContext *ctx);
+    static Value method_toExponential(ExecutionContext *ctx);
+    static Value method_toPrecision(ExecutionContext *ctx);
+};
+
+
+} // end of namespace VM
+} // end of namespace QQmlJS
+
+#endif // QV4ECMAOBJECTS_P_H
diff --git a/v4.pro b/v4.pro
index bc54c45..ac77693 100644 (file)
--- a/v4.pro
+++ b/v4.pro
@@ -34,6 +34,7 @@ SOURCES += main.cpp \
     qv4globalobject.cpp \
     qv4jsonobject.cpp \
     qv4mathobject.cpp \
+    qv4numberobject.cpp \
     qv4object.cpp \
     qv4regexpobject.cpp \
     qv4stringobject.cpp \
@@ -66,6 +67,7 @@ HEADERS += \
     qv4globalobject.h \
     qv4jsonobject.h \
     qv4mathobject.h \
+    qv4numberobject.h \
     qv4object.h \
     qv4regexpobject.h \
     qv4stringobject.h \