Add the Boolean Object.
authorRoberto Raggi <roberto.raggi@nokia.com>
Tue, 15 May 2012 09:19:10 +0000 (11:19 +0200)
committerRoberto Raggi <roberto.raggi@nokia.com>
Tue, 15 May 2012 09:19:10 +0000 (11:19 +0200)
qmljs_objects.cpp
qmljs_objects.h
qmljs_runtime.cpp
qv4ecmaobjects.cpp
qv4ecmaobjects_p.h

index 3439ae7..3192ac9 100644 (file)
@@ -258,7 +258,9 @@ FunctionObject *ExecutionEngine::newStringCtor(Context *ctx)
 
 Object *ExecutionEngine::newStringPrototype(Context *ctx, FunctionObject *proto)
 {
-    return new StringPrototype(ctx, proto);
+    Object *stringProto = new StringPrototype(ctx, proto);
+    stringProto->prototype = objectPrototype.objectValue;
+    return stringProto;
 }
 
 Object *ExecutionEngine::newNumberObject(const Value &value)
@@ -273,7 +275,9 @@ FunctionObject *ExecutionEngine::newNumberCtor(Context *ctx)
 
 Object *ExecutionEngine::newNumberPrototype(Context *ctx, FunctionObject *proto)
 {
-    return new NumberPrototype(ctx, proto);
+    Object *numberProto = new NumberPrototype(ctx, proto);
+    numberProto->prototype = objectPrototype.objectValue;
+    return numberProto;
 }
 
 Object *ExecutionEngine::newBooleanObject(const Value &value)
@@ -281,6 +285,18 @@ Object *ExecutionEngine::newBooleanObject(const Value &value)
     return new BooleanObject(value);
 }
 
+FunctionObject *ExecutionEngine::newBooleanCtor(Context *ctx)
+{
+    return new BooleanCtor(ctx);
+}
+
+Object *ExecutionEngine::newBooleanPrototype(Context *ctx, FunctionObject *proto)
+{
+    Object *booleanProto = new BooleanPrototype(ctx, proto);
+    booleanProto->prototype = objectPrototype.objectValue;
+    return booleanProto;
+}
+
 Object *ExecutionEngine::newErrorObject(const Value &value)
 {
     return new ErrorObject(value);
index ae4efab..ebc3cd0 100644 (file)
@@ -370,6 +370,9 @@ struct ExecutionEngine
     Object *newNumberPrototype(Context *ctx, FunctionObject *proto);
 
     Object *newBooleanObject(const Value &value);
+    FunctionObject *newBooleanCtor(Context *ctx);
+    Object *newBooleanPrototype(Context *ctx, FunctionObject *proto);
+
     Object *newErrorObject(const Value &value);
     Object *newMathObject(Context *ctx);
     Object *newArgumentsObject(Context *ctx);
index 2c28359..572928b 100644 (file)
@@ -211,6 +211,7 @@ void __qmljs_new_boolean_object(Context *ctx, Value *result, bool boolean)
     Value value;
     __qmljs_init_boolean(&value, boolean);
     __qmljs_init_object(result, ctx->engine->newBooleanObject(value));
+    result->objectValue->prototype = ctx->engine->objectPrototype.objectValue;
 }
 
 void __qmljs_new_number_object(Context *ctx, Value *result, double number)
index 4e215ef..506e866 100644 (file)
@@ -568,6 +568,70 @@ void NumberPrototype::method_toPrecision(Context *ctx)
 }
 
 //
+// Boolean object
+//
+Value BooleanCtor::create(ExecutionEngine *engine)
+{
+    Context *ctx = engine->rootContext;
+    FunctionObject *ctor = ctx->engine->newBooleanCtor(ctx);
+    ctor->setProperty(ctx, QLatin1String("prototype"), Value::fromObject(ctx->engine->newBooleanPrototype(ctx, ctor)));
+    return Value::fromObject(ctor);
+}
+
+BooleanCtor::BooleanCtor(Context *scope)
+    : FunctionObject(scope)
+{
+}
+
+void BooleanCtor::construct(Context *ctx)
+{
+    const double n = ctx->argument(0).toBoolean(ctx);
+    __qmljs_init_object(&ctx->thisObject, ctx->engine->newBooleanObject(Value::fromBoolean(n)));
+}
+
+void BooleanCtor::call(Context *ctx)
+{
+    double value = ctx->argumentCount ? ctx->argument(0).toBoolean(ctx) : 0;
+    __qmljs_init_boolean(&ctx->result, value);
+}
+
+BooleanPrototype::BooleanPrototype(Context *ctx, FunctionObject *ctor)
+{
+    ctor->setProperty(ctx, QLatin1String("constructor"), Value::fromObject(ctor));
+    ctor->setProperty(ctx, QLatin1String("toString"), method_toString);
+    ctor->setProperty(ctx, QLatin1String("valueOf"), method_valueOf);
+}
+
+void BooleanPrototype::method_toString(Context *ctx)
+{
+    Value self = ctx->thisObject;
+//    if (self.classInfo() != classInfo) {
+//        return throwThisObjectTypeError(
+//            context, QLatin1String("Boolean.prototype.toString"));
+//    }
+    assert(self.isObject());
+    Value internalValue;
+    self.objectValue->defaultValue(ctx, &internalValue, PREFERREDTYPE_HINT);
+    assert(internalValue.isBoolean());
+    bool v = internalValue.booleanValue;
+    ctx->result = Value::fromString(ctx, QLatin1String(v ? "true" : "false"));
+}
+
+void BooleanPrototype::method_valueOf(Context *ctx)
+{
+    Value self = ctx->thisObject;
+//    if (self.classInfo() != classInfo) {
+//        return throwThisObjectTypeError(
+//            context, QLatin1String("Boolean.prototype.valueOf"));
+//    }
+    assert(self.isObject());
+    Value internalValue;
+    self.objectValue->defaultValue(ctx, &internalValue, PREFERREDTYPE_HINT);
+    assert(internalValue.isBoolean());
+    ctx->result = internalValue;
+}
+
+//
 // Math object
 //
 MathObject::MathObject(Context *ctx)
index 386c2f1..d37aefc 100644 (file)
@@ -83,6 +83,25 @@ protected:
     static void method_toPrecision(Context *ctx);
 };
 
+struct BooleanCtor: FunctionObject
+{
+    static Value create(ExecutionEngine *engine);
+
+    BooleanCtor(Context *scope);
+
+    virtual void construct(Context *ctx);
+    virtual void call(Context *ctx);
+};
+
+struct BooleanPrototype: Object
+{
+    BooleanPrototype(Context *ctx, FunctionObject *ctor);
+
+protected:
+    static void method_toString(Context *ctx);
+    static void method_valueOf(Context *ctx);
+};
+
 struct MathObject: Object
 {
     MathObject(Context *ctx);