Initial work on the RegExp object
authorRoberto Raggi <roberto.raggi@nokia.com>
Mon, 4 Jun 2012 13:34:50 +0000 (15:34 +0200)
committerRoberto Raggi <roberto.raggi@nokia.com>
Mon, 4 Jun 2012 13:34:50 +0000 (15:34 +0200)
qmljs_objects.cpp
qmljs_objects.h
qv4ecmaobjects.cpp
qv4ecmaobjects_p.h

index 464b9cc..a9899ec 100644 (file)
@@ -238,6 +238,7 @@ ExecutionEngine::ExecutionEngine()
     arrayPrototype = new ArrayPrototype();
     datePrototype = new DatePrototype();
     functionPrototype = new FunctionPrototype(rootContext);
+    regExpPrototype = new RegExpPrototype();
 
     stringPrototype->prototype = objectPrototype;
     numberPrototype->prototype = objectPrototype;
@@ -245,21 +246,24 @@ ExecutionEngine::ExecutionEngine()
     arrayPrototype->prototype = objectPrototype;
     datePrototype->prototype = objectPrototype;
     functionPrototype->prototype = objectPrototype;
-
-    Value objectCtor = Value::fromObject(new ObjectCtor(rootContext));
-    Value stringCtor = Value::fromObject(new StringCtor(rootContext));
-    Value numberCtor = Value::fromObject(new NumberCtor(rootContext));
-    Value booleanCtor = Value::fromObject(new BooleanCtor(rootContext));
-    Value arrayCtor = Value::fromObject(new ArrayCtor(rootContext));
-    Value functionCtor = Value::fromObject(new FunctionCtor(rootContext));
-    Value dateCtor = Value::fromObject(new DateCtor(rootContext));
-
-    stringCtor.objectValue->prototype = functionPrototype;
-    numberCtor.objectValue->prototype = functionPrototype;
-    booleanCtor.objectValue->prototype = functionPrototype;
-    arrayCtor.objectValue->prototype = functionPrototype;
+    regExpPrototype->prototype = objectPrototype;
+
+    objectCtor = Value::fromObject(new ObjectCtor(rootContext));
+    stringCtor = Value::fromObject(new StringCtor(rootContext));
+    numberCtor = Value::fromObject(new NumberCtor(rootContext));
+    booleanCtor = Value::fromObject(new BooleanCtor(rootContext));
+    arrayCtor = Value::fromObject(new ArrayCtor(rootContext));
+    functionCtor = Value::fromObject(new FunctionCtor(rootContext));
+    dateCtor = Value::fromObject(new DateCtor(rootContext));
+    regExpCtor = Value::fromObject(new RegExpCtor(rootContext));
+
+    stringCtor.objectValue->prototype = stringPrototype;
+    numberCtor.objectValue->prototype = numberPrototype;
+    booleanCtor.objectValue->prototype = booleanPrototype;
+    arrayCtor.objectValue->prototype = arrayPrototype;
     functionCtor.objectValue->prototype = functionPrototype;
-    dateCtor.objectValue->prototype = functionPrototype;
+    dateCtor.objectValue->prototype = datePrototype;
+    regExpCtor.objectValue->prototype = regExpPrototype;
 
     objectPrototype->init(rootContext, objectCtor);
     stringPrototype->init(rootContext, stringCtor);
@@ -268,6 +272,7 @@ ExecutionEngine::ExecutionEngine()
     arrayPrototype->init(rootContext, arrayCtor);
     datePrototype->init(rootContext, dateCtor);
     functionPrototype->init(rootContext, functionCtor);
+    regExpPrototype->init(rootContext, regExpCtor);
 
     //
     // set up the global object
@@ -283,6 +288,7 @@ ExecutionEngine::ExecutionEngine()
     glo->setProperty(rootContext, identifier(QStringLiteral("Array")), arrayCtor);
     glo->setProperty(rootContext, identifier(QStringLiteral("Function")), functionCtor);
     glo->setProperty(rootContext, identifier(QStringLiteral("Date")), dateCtor);
+    glo->setProperty(rootContext, identifier(QStringLiteral("RegExp")), regExpCtor);
     glo->setProperty(rootContext, identifier(QStringLiteral("Math")), Value::fromObject(newMathObject(rootContext)));
 }
 
@@ -412,6 +418,18 @@ FunctionObject *ExecutionEngine::newDateCtor(Context *ctx)
     return new DateCtor(ctx);
 }
 
+Object *ExecutionEngine::newRegExpObject(const Value &value)
+{
+    Object *object = new RegExpObject(value);
+    object->prototype = regExpPrototype;
+    return object;
+}
+
+FunctionObject *ExecutionEngine::newRegExpCtor(Context *ctx)
+{
+    return new RegExpCtor(ctx);
+}
+
 Object *ExecutionEngine::newErrorObject(const Value &value)
 {
     ErrorObject *object = new ErrorObject(value);
index bdaef3e..a1f4d0e 100644 (file)
@@ -24,6 +24,7 @@ struct StringObject;
 struct ArrayObject;
 struct DateObject;
 struct FunctionObject;
+struct RegExpObject;
 struct ErrorObject;
 struct ArgumentsObject;
 struct Context;
@@ -36,6 +37,7 @@ struct BooleanPrototype;
 struct ArrayPrototype;
 struct FunctionPrototype;
 struct DatePrototype;
+struct RegExpPrototype;
 
 struct String {
     String(const QString &text)
@@ -212,6 +214,7 @@ struct Object {
     virtual DateObject *asDateObject() { return 0; }
     virtual ArrayObject *asArrayObject() { return 0; }
     virtual FunctionObject *asFunctionObject() { return 0; }
+    virtual RegExpObject *asRegExpObject() { return 0; }
     virtual ErrorObject *asErrorObject() { return 0; }
     virtual ArgumentsObject *asArgumentsObject() { return 0; }
 
@@ -309,6 +312,13 @@ struct ScriptFunction: FunctionObject {
     virtual void construct(Context *ctx);
 };
 
+struct RegExpObject: Object {
+    Value value;
+    RegExpObject(const Value &value): value(value) {}
+    virtual QString className() { return QStringLiteral("RegExp"); }
+    virtual RegExpObject *asRegExpObject() { return this; }
+};
+
 struct ErrorObject: Object {
     Value value;
     ErrorObject(const Value &message): value(message) {}
@@ -410,6 +420,7 @@ struct ExecutionEngine
     Value arrayCtor;
     Value functionCtor;
     Value dateCtor;
+    Value regExpCtor;
 
     ObjectPrototype *objectPrototype;
     StringPrototype *stringPrototype;
@@ -418,6 +429,7 @@ struct ExecutionEngine
     ArrayPrototype *arrayPrototype;
     FunctionPrototype *functionPrototype;
     DatePrototype *datePrototype;
+    RegExpPrototype *regExpPrototype;
 
     QHash<QString, String *> identifiers;
 
@@ -458,6 +470,9 @@ struct ExecutionEngine
     Object *newDateObject(const Value &value);
     FunctionObject *newDateCtor(Context *ctx);
 
+    Object *newRegExpObject(const Value &value);
+    FunctionObject *newRegExpCtor(Context *ctx);
+
     Object *newErrorObject(const Value &value);
     Object *newMathObject(Context *ctx);
     Object *newArgumentsObject(Context *ctx);
index deab3c1..7692ace 100644 (file)
@@ -2330,6 +2330,48 @@ void DatePrototype::method_toUTCString(Context *ctx)
 }
 
 //
+// RegExp object
+//
+RegExpCtor::RegExpCtor(Context *scope)
+    : FunctionObject(scope)
+{
+}
+
+void RegExpCtor::construct(Context *ctx)
+{
+    __qmljs_init_object(&ctx->thisObject, ctx->engine->newStringObject(Value::undefinedValue()));
+}
+
+void RegExpCtor::call(Context *ctx)
+{
+    __qmljs_init_object(&ctx->result, ctx->engine->newRegExpObject(Value::undefinedValue()));
+}
+
+void RegExpPrototype::init(Context *ctx, const Value &ctor)
+{
+    ctor.objectValue->setProperty(ctx, ctx->engine->id_prototype, Value::fromObject(this));
+    setProperty(ctx, QStringLiteral("constructor"), ctor);
+    setProperty(ctx, QStringLiteral("exec"), method_exec, 0);
+    setProperty(ctx, QStringLiteral("test"), method_test, 0);
+    setProperty(ctx, QStringLiteral("toString"), method_toString, 0);
+}
+
+void RegExpPrototype::method_exec(Context *ctx)
+{
+    ctx->throwUnimplemented(QStringLiteral("RegExp.prototype.exec"));
+}
+
+void RegExpPrototype::method_test(Context *ctx)
+{
+    ctx->throwUnimplemented(QStringLiteral("RegExp.prototype.test"));
+}
+
+void RegExpPrototype::method_toString(Context *ctx)
+{
+    ctx->throwUnimplemented(QStringLiteral("RegExp.prototype.toString"));
+}
+
+//
 // Math object
 //
 MathObject::MathObject(Context *ctx)
index 6517ad1..38164fc 100644 (file)
@@ -235,6 +235,24 @@ struct DatePrototype: DateObject
     static void method_toUTCString(Context *ctx);
 };
 
+struct RegExpCtor: FunctionObject
+{
+    RegExpCtor(Context *scope);
+
+    virtual void construct(Context *ctx);
+    virtual void call(Context *ctx);
+};
+
+struct RegExpPrototype: RegExpObject
+{
+    RegExpPrototype(): RegExpObject(Value::fromNumber(qSNaN())) {}
+    void init(Context *ctx, const Value &ctor);
+
+    static void method_exec(Context *ctx);
+    static void method_test(Context *ctx);
+    static void method_toString(Context *ctx);
+};
+
 struct MathObject: Object
 {
     MathObject(Context *ctx);