Commit eeb7c055 authored by yangguo's avatar yangguo Committed by Commit bot

[es6] Implement @@match subclassing.

BUG=v8:4305
LOG=N

Review URL: https://codereview.chromium.org/1434523002

Cr-Commit-Position: refs/heads/master@{#32210}
parent e7560013
...@@ -15,8 +15,8 @@ var GlobalRegExp = global.RegExp; ...@@ -15,8 +15,8 @@ var GlobalRegExp = global.RegExp;
var InternalArray = utils.InternalArray; var InternalArray = utils.InternalArray;
var InternalPackedArray = utils.InternalPackedArray; var InternalPackedArray = utils.InternalPackedArray;
var MakeTypeError; var MakeTypeError;
var matchSymbol = utils.ImportNow("match_symbol");
var splitSymbol = utils.ImportNow("split_symbol"); var splitSymbol = utils.ImportNow("split_symbol");
var matchSymbol = utils.ImportNow("match_symbol");;
utils.ImportFromExperimental(function(from) { utils.ImportFromExperimental(function(from) {
FLAG_harmony_tolength = from.FLAG_harmony_tolength; FLAG_harmony_tolength = from.FLAG_harmony_tolength;
...@@ -355,6 +355,21 @@ function RegExpSplit(string, limit) { ...@@ -355,6 +355,21 @@ function RegExpSplit(string, limit) {
} }
// ES6 21.2.5.6.
function RegExpMatch(string) {
if (!IS_REGEXP(this)) {
throw MakeTypeError(kIncompatibleMethodReceiver,
"RegExp.prototype.@@match", this);
}
var subject = TO_STRING(string);
if (!REGEXP_GLOBAL(this)) return RegExpExecNoTests(this, subject, 0);
this.lastIndex = 0;
var result = %StringMatch(subject, this, RegExpLastMatchInfo);
return result;
}
// Getters for the static properties lastMatch, lastParen, leftContext, and // Getters for the static properties lastMatch, lastParen, leftContext, and
// rightContext of the RegExp constructor. The properties are computed based // rightContext of the RegExp constructor. The properties are computed based
// on the captures array of the last successful match and the subject string // on the captures array of the last successful match and the subject string
...@@ -472,6 +487,7 @@ utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ ...@@ -472,6 +487,7 @@ utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [
"test", RegExpTest, "test", RegExpTest,
"toString", RegExpToString, "toString", RegExpToString,
"compile", RegExpCompileJS, "compile", RegExpCompileJS,
matchSymbol, RegExpMatch,
splitSymbol, RegExpSplit, splitSymbol, RegExpSplit,
]); ]);
......
...@@ -19,6 +19,7 @@ var MakeRangeError; ...@@ -19,6 +19,7 @@ var MakeRangeError;
var MakeTypeError; var MakeTypeError;
var MathMax; var MathMax;
var MathMin; var MathMin;
var matchSymbol = utils.ImportNow("match_symbol");
var RegExpExec; var RegExpExec;
var RegExpExecNoTests; var RegExpExecNoTests;
var RegExpLastMatchInfo; var RegExpLastMatchInfo;
...@@ -155,18 +156,23 @@ function StringLocaleCompareJS(other) { ...@@ -155,18 +156,23 @@ function StringLocaleCompareJS(other) {
// ECMA-262 section 15.5.4.10 // ECMA-262 section 15.5.4.10
function StringMatchJS(regexp) { function StringMatchJS(pattern) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.match"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.match");
var subject = TO_STRING(this); if (!IS_NULL_OR_UNDEFINED(pattern)) {
if (IS_REGEXP(regexp)) { var matcher = pattern[matchSymbol];
if (!REGEXP_GLOBAL(regexp)) return RegExpExecNoTests(regexp, subject, 0); if (!IS_UNDEFINED(matcher)) {
var result = %StringMatch(subject, regexp, RegExpLastMatchInfo); if (!IS_CALLABLE(matcher)) {
regexp.lastIndex = 0; throw MakeTypeError(kCalledNonCallable, matcher);
return result; }
return %_Call(matcher, pattern, this);
}
} }
var subject = TO_STRING(this);
// Non-regexp argument. // Non-regexp argument.
regexp = new GlobalRegExp(regexp); var regexp = new GlobalRegExp(pattern);
return RegExpExecNoTests(regexp, subject, 0); return RegExpExecNoTests(regexp, subject, 0);
} }
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-regexp-subclass
var pattern = {};
pattern[Symbol.match] = function(string) {
return string.length;
};
// Check object coercible fails.
assertThrows(() => String.prototype.match.call(null, pattern),
TypeError);
// Override is called.
assertEquals(5, "abcde".match(pattern));
// Non-callable override.
pattern[Symbol.match] = "dumdidum";
assertThrows(() => "abcde".match(pattern), TypeError);
assertEquals("[Symbol.match]", RegExp.prototype[Symbol.match].name);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment