Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Unified Diff: src/js/regexp.js

Issue 1419823010: Implement flag and source getters on RegExp.prototype. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@rproto
Patch Set: fix windows build Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/js/regexp.js
diff --git a/src/js/regexp.js b/src/js/regexp.js
index 51c8ed0d6ee921a523ece0c1e885c6164534ce0d..f6d89267eb589519d92a59596c0b5b6cfc4e2696 100644
--- a/src/js/regexp.js
+++ b/src/js/regexp.js
@@ -16,6 +16,8 @@ var GlobalObject = global.Object;
var GlobalRegExp = global.RegExp;
var InternalPackedArray = utils.InternalPackedArray;
var MakeTypeError;
+var regExpFlagsSymbol = utils.ImportNow("regexp_flags_symbol");
+var regExpSourceSymbol = utils.ImportNow("regexp_source_symbol");
utils.ImportFromExperimental(function(from) {
FLAG_harmony_regexps = from.FLAG_harmony_regexps;
@@ -51,14 +53,14 @@ function DoConstructRegExp(object, pattern, flags) {
// RegExp : Called as constructor; see ECMA-262, section 15.10.4.
if (IS_REGEXP(pattern)) {
if (!IS_UNDEFINED(flags)) throw MakeTypeError(kRegExpFlags);
- flags = (pattern.global ? 'g' : '')
- + (pattern.ignoreCase ? 'i' : '')
- + (pattern.multiline ? 'm' : '');
+ flags = (REGEXP_GLOBAL(pattern) ? 'g' : '')
+ + (REGEXP_IGNORE_CASE(pattern) ? 'i' : '')
+ + (REGEXP_MULTILINE(pattern) ? 'm' : '');
if (FLAG_harmony_unicode_regexps)
- flags += (pattern.unicode ? 'u' : '');
+ flags += (REGEXP_UNICODE(pattern) ? 'u' : '');
if (FLAG_harmony_regexps)
- flags += (pattern.sticky ? 'y' : '');
- pattern = pattern.source;
+ flags += (REGEXP_STICKY(pattern) ? 'y' : '');
+ pattern = REGEXP_SOURCE(pattern);
}
pattern = IS_UNDEFINED(pattern) ? '' : TO_STRING(pattern);
@@ -165,7 +167,8 @@ function RegExpExecJS(string) {
// algorithm, step 4) even if the value is discarded for non-global RegExps.
var i = TO_LENGTH_OR_INTEGER(lastIndex);
- var updateLastIndex = this.global || (FLAG_harmony_regexps && this.sticky);
+ var updateLastIndex = REGEXP_GLOBAL(this) ||
+ (FLAG_harmony_regexps && REGEXP_STICKY(this));
if (updateLastIndex) {
if (i < 0 || i > string.length) {
this.lastIndex = 0;
@@ -212,7 +215,7 @@ function RegExpTest(string) {
// algorithm, step 4) even if the value is discarded for non-global RegExps.
var i = TO_LENGTH_OR_INTEGER(lastIndex);
- if (this.global || (FLAG_harmony_regexps && this.sticky)) {
+ if (REGEXP_GLOBAL(this) || (FLAG_harmony_regexps && REGEXP_STICKY(this))) {
if (i < 0 || i > string.length) {
this.lastIndex = 0;
return false;
@@ -231,10 +234,11 @@ function RegExpTest(string) {
// checks whether this.source starts with '.*' and that the third char is
// not a '?'. But see https://code.google.com/p/v8/issues/detail?id=3560
var regexp = this;
- if (regexp.source.length >= 3 &&
- %_StringCharCodeAt(regexp.source, 0) == 46 && // '.'
- %_StringCharCodeAt(regexp.source, 1) == 42 && // '*'
- %_StringCharCodeAt(regexp.source, 2) != 63) { // '?'
+ var source = REGEXP_SOURCE(regexp);
+ if (regexp.length >= 3 &&
+ %_StringCharCodeAt(regexp, 0) == 46 && // '.'
+ %_StringCharCodeAt(regexp, 1) == 42 && // '*'
+ %_StringCharCodeAt(regexp, 2) != 63) { // '?'
regexp = TrimRegExp(regexp);
}
// matchIndices is either null or the RegExpLastMatchInfo array.
@@ -251,9 +255,10 @@ function TrimRegExp(regexp) {
if (!%_ObjectEquals(regexp_key, regexp)) {
regexp_key = regexp;
regexp_val =
- new GlobalRegExp(%_SubString(regexp.source, 2, regexp.source.length),
- (regexp.ignoreCase ? regexp.multiline ? "im" : "i"
- : regexp.multiline ? "m" : ""));
+ new GlobalRegExp(
+ %_SubString(REGEXP_SOURCE(regexp), 2, REGEXP_SOURCE(regexp).length),
+ (REGEXP_IGNORE_CASE(regexp) ? REGEXP_MULTILINE(regexp) ? "im" : "i"
+ : REGEXP_MULTILINE(regexp) ? "m" : ""));
}
return regexp_val;
}
@@ -264,12 +269,12 @@ function RegExpToString() {
throw MakeTypeError(kIncompatibleMethodReceiver,
'RegExp.prototype.toString', this);
}
- var result = '/' + this.source + '/';
- if (this.global) result += 'g';
- if (this.ignoreCase) result += 'i';
- if (this.multiline) result += 'm';
- if (FLAG_harmony_unicode_regexps && this.unicode) result += 'u';
- if (FLAG_harmony_regexps && this.sticky) result += 'y';
+ var result = '/' + REGEXP_SOURCE(this) + '/';
+ if (REGEXP_GLOBAL(this)) result += 'g';
+ if (REGEXP_IGNORE_CASE(this)) result += 'i';
+ if (REGEXP_MULTILINE(this)) result += 'm';
+ if (FLAG_harmony_unicode_regexps && REGEXP_UNICODE(this)) result += 'u';
+ if (FLAG_harmony_regexps && REGEXP_STICKY(this)) result += 'y';
return result;
}
@@ -334,6 +339,40 @@ function RegExpMakeCaptureGetter(n) {
};
}
+
+// ES6 21.2.5.4, 21.2.5.5, 21.2.5.7, 21.2.5.12, 21.2.5.15.
+function GetRegExpFlagGetter(name, mask) {
+ var getter = function() {
+ if (!IS_SPEC_OBJECT(this)) {
+ throw MakeTypeError(kRegExpNonObject, name, TO_STRING(this));
+ }
+ var flags = this[regExpFlagsSymbol];
+ if (IS_UNDEFINED(flags)) {
+ throw MakeTypeError(kRegExpNonRegExp, TO_STRING(this));
+ }
+ return !!(flags & mask);
+ };
+ %FunctionSetName(getter, name);
+ %SetNativeFlag(getter);
+ return getter;
+}
+
+
+// ES6 21.2.5.10.
+function RegExpGetSource() {
+ if (!IS_SPEC_OBJECT(this)) {
+ throw MakeTypeError(kRegExpNonObject, "RegExp.prototype.source",
+ TO_STRING(this));
+ }
+ var source = this[regExpSourceSymbol];
+ if (IS_UNDEFINED(source)) {
+ throw MakeTypeError(kRegExpNonRegExp, TO_STRING(this));
+ }
+ return source;
+}
+
+%SetNativeFlag(RegExpGetSource);
+
// -------------------------------------------------------------------
%FunctionSetInstanceClassName(GlobalRegExp, 'RegExp');
@@ -349,6 +388,15 @@ utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [
"compile", RegExpCompileJS
]);
+%DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "global",
+ GetRegExpFlagGetter("RegExp.prototype.global", 1), DONT_ENUM);
+%DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "ignoreCase",
+ GetRegExpFlagGetter("RegExp.prototype.ignoreCase", 2), DONT_ENUM);
+%DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "multiline",
+ GetRegExpFlagGetter("RegExp.prototype.multiline", 4), DONT_ENUM);
+%DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "source",
+ RegExpGetSource, DONT_ENUM);
+
// The length of compile is 1 in SpiderMonkey.
%FunctionSetLength(GlobalRegExp.prototype.compile, 1);
@@ -422,6 +470,7 @@ for (var i = 1; i < 10; ++i) {
// Exports
utils.Export(function(to) {
+ to.GetRegExpFlagGetter = GetRegExpFlagGetter;
to.RegExpExec = DoRegExpExec;
to.RegExpExecNoTests = RegExpExecNoTests;
to.RegExpLastMatchInfo = RegExpLastMatchInfo;

Powered by Google App Engine
This is Rietveld 408576698