diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-08-08 14:37:13 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2018-08-15 14:24:30 +0000 |
commit | 2e3d2565aab881d4b86911a59769e777de8a9c9f (patch) | |
tree | 918c6a66c3be965669a70edb41663a422586c08f /src | |
parent | cda5885efab0b9905260e0d48013d1d10644eb29 (diff) |
Implement String.prototype.match as per ES7 spec
The implementation is supposed to call arg[Symbol.match].
Change-Id: Ia8028d259e152b1e65eb6b0e817ef60bdc0d37e2
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/jsruntime/qv4stringobject.cpp | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp index fd2d297d55..de8fa0c2ee 100644 --- a/src/qml/jsruntime/qv4stringobject.cpp +++ b/src/qml/jsruntime/qv4stringobject.cpp @@ -554,6 +554,19 @@ ReturnedValue StringPrototype::method_match(const FunctionObject *b, const Value return v4->throwTypeError(); Scope scope(v4); + if (argc && !argv[0].isNullOrUndefined()) { + ScopedObject r(scope, argv[0].toObject(scope.engine)); + if (scope.hasException()) + return Encode::undefined(); + ScopedValue f(scope, r->get(scope.engine->symbol_match())); + if (!f->isNullOrUndefined()) { + ScopedFunctionObject fo(scope, f); + if (!fo) + return scope.engine->throwTypeError(); + return fo->call(r, thisObject, 1); + } + } + ScopedString s(scope, thisObject->toString(v4)); if (v4->hasException) return Encode::undefined(); @@ -567,35 +580,10 @@ ReturnedValue StringPrototype::method_match(const FunctionObject *b, const Value } Q_ASSERT(!!that); - bool global = that->global(); - - if (!global) - return RegExpPrototype::method_exec(b, that, s, 1); - - // rx is now in thisObject - that->setLastIndex(0); - ScopedArrayObject a(scope, scope.engine->newArrayObject()); - - int previousLastIndex = 0; - uint n = 0; - while (1) { - Value result = Primitive::fromReturnedValue(RegExpPrototype::execFirstMatch(b, that, s, 1)); - if (result.isNull()) - break; - int index = that->lastIndex(); - if (previousLastIndex == index) { - previousLastIndex = index + 1; - that->setLastIndex(previousLastIndex); - } else { - previousLastIndex = index; - } - a->arraySet(n, result); - ++n; - } - if (!n) - return Encode::null(); - else - return a.asReturnedValue(); + ScopedFunctionObject match(scope, that->get(scope.engine->symbol_match())); + if (!match) + return scope.engine->throwTypeError(); + return match->call(that, s, 1); } ReturnedValue StringPrototype::method_normalize(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) |