Added isNaN and isFinite to the global context.
authorErik Verbruggen <erik.verbruggen@digia.com>
Mon, 19 Nov 2012 10:54:54 +0000 (11:54 +0100)
committerLars Knoll <lars.knoll@digia.com>
Mon, 19 Nov 2012 12:57:51 +0000 (13:57 +0100)
Change-Id: Ia85d27a6ac82fd5dbf6b0f706747afa6418626b1
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
qmljs_engine.cpp
qmljs_objects.cpp
qmljs_objects.h

index 8097639..4415231 100644 (file)
@@ -162,7 +162,10 @@ ExecutionEngine::ExecutionEngine()
     glo->__put__(rootContext, identifier(QStringLiteral("Infinity")), Value::fromDouble(INFINITY));
     glo->__put__(rootContext, identifier(QStringLiteral("eval")), Value::fromObject(new EvalFunction(rootContext)));
 
-
+    // TODO: parseInt [15.1.2.2]
+    // TODO: parseFloat [15.1.2.3]
+    glo->__put__(rootContext, identifier(QStringLiteral("isNaN")), Value::fromObject(new IsNaNFunction(rootContext))); // isNaN [15.1.2.4]
+    glo->__put__(rootContext, identifier(QStringLiteral("isFinite")), Value::fromObject(new IsFiniteFunction(rootContext))); // isFinite [15.1.2.5]
 }
 
 ExecutionContext *ExecutionEngine::newContext()
index 4f831b0..290a4f7 100644 (file)
@@ -487,6 +487,22 @@ Value EvalFunction::call(ExecutionContext *context, Value thisObject, Value *arg
         ctx->leaveCallContext();
 }
 
+/// isNaN [15.1.2.4]
+Value IsNaNFunction::call(ExecutionContext * /*context*/, Value /*thisObject*/, Value *args, int /*argc*/, bool /*strictMode*/)
+{
+    // TODO: see if we can generate code for this directly
+    const Value &v = args[0];
+    return Value::fromBoolean(v.isDouble() ? std::isnan(v.doubleValue()) : false);
+}
+
+/// isFinite [15.1.2.5]
+Value IsFiniteFunction::call(ExecutionContext * /*context*/, Value /*thisObject*/, Value *args, int /*argc*/, bool /*strictMode*/)
+{
+    // TODO: see if we can generate code for this directly
+    const Value &v = args[0];
+    return Value::fromBoolean(v.isDouble() ? std::isfinite(v.doubleValue()) : true);
+}
+
 static inline bool protect(const void *addr, size_t size)
 {
     size_t pageSize = sysconf(_SC_PAGESIZE);
index 6d0e0fa..a2b956f 100644 (file)
@@ -532,6 +532,20 @@ struct EvalFunction : FunctionObject
     virtual Value call(ExecutionContext *context, Value thisObject, Value *args, int argc, bool strictMode = false);
 };
 
+struct IsNaNFunction: FunctionObject
+{
+    IsNaNFunction(ExecutionContext *scope): FunctionObject(scope) {}
+
+    virtual Value call(ExecutionContext *context, Value thisObject, Value *args, int argc, bool strictMode = false);
+};
+
+struct IsFiniteFunction: FunctionObject
+{
+    IsFiniteFunction(ExecutionContext *scope): FunctionObject(scope) {}
+
+    virtual Value call(ExecutionContext *context, Value thisObject, Value *args, int argc, bool strictMode = false);
+};
+
 struct RegExpObject: Object {
     QRegularExpression value;
     Value lastIndex;