diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-08-08 15:03:57 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2018-08-15 14:24:34 +0000 |
commit | cecd8be881034153fc6b92e96f152a4a0c189ae4 (patch) | |
tree | cf554f3cb78ab6a0d02947870345d3660ddc36e0 /src/qml/jsruntime/qv4regexpobject.cpp | |
parent | 2e3d2565aab881d4b86911a59769e777de8a9c9f (diff) |
Implement RegExp.prototype[Symbol.search]
Change-Id: Ie966628d020eb010eb5ecc3279fed2b002975728
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4regexpobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4regexpobject.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4regexpobject.cpp b/src/qml/jsruntime/qv4regexpobject.cpp index 8d457352d7..63b0d1524f 100644 --- a/src/qml/jsruntime/qv4regexpobject.cpp +++ b/src/qml/jsruntime/qv4regexpobject.cpp @@ -342,6 +342,7 @@ void RegExpPrototype::init(ExecutionEngine *engine, Object *constructor) defineDefaultProperty(QStringLiteral("exec"), method_exec, 1); defineDefaultProperty(engine->symbol_match(), method_match, 1); defineAccessorProperty(scope.engine->id_multiline(), method_get_multiline, nullptr); + defineDefaultProperty(engine->symbol_search(), method_search, 1); defineAccessorProperty(QStringLiteral("source"), method_get_source, nullptr); defineAccessorProperty(scope.engine->id_sticky(), method_get_sticky, nullptr); defineDefaultProperty(QStringLiteral("test"), method_test, 1); @@ -557,6 +558,40 @@ ReturnedValue RegExpPrototype::method_get_multiline(const FunctionObject *f, con return Encode(b); } +ReturnedValue RegExpPrototype::method_search(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) +{ + Scope scope(f); + ScopedObject rx(scope, thisObject); + if (!rx) + return scope.engine->throwTypeError(); + + ScopedString s(scope, (argc ? argv[0] : Primitive::undefinedValue()).toString(scope.engine)); + if (scope.hasException()) + return Encode::undefined(); + + ScopedValue previousLastIndex(scope, rx->get(scope.engine->id_lastIndex())); + if (previousLastIndex->toNumber() != 0) { + if (!rx->put(scope.engine->id_lastIndex(), Primitive::fromInt32(0))) + return scope.engine->throwTypeError(); + } + + ScopedValue result(scope, exec(scope.engine, rx, s)); + if (scope.hasException()) + return Encode::undefined(); + + ScopedValue currentLastIndex(scope, rx->get(scope.engine->id_lastIndex())); + if (!currentLastIndex->sameValue(previousLastIndex)) { + if (!rx->put(scope.engine->id_lastIndex(), previousLastIndex)) + return scope.engine->throwTypeError(); + } + + if (result->isNull()) + return Encode(-1); + ScopedObject o(scope, result); + Q_ASSERT(o); + return o->get(scope.engine->id_index()); +} + ReturnedValue RegExpPrototype::method_get_source(const FunctionObject *f, const Value *thisObject, const Value *, int) { Scope scope(f); |