Make the runtime entry for setting/changing accessors "atomic".
Previously, there were 1 or 2 calls to the runtime when accessors were changed
or set. This doesn't really work well with property attributes, leading to some
hacks and complicates things even further when trying to share maps in presence
of accessors. Therefore, the runtime entry now takes the full triple (getter,
setter, attributes), where the getter and/or the setter can be null in case they
shouldn't be changed.
For now, we do basically the same on the native side as we did before on the
JavaScript side, but this will change in future CLs, the current CL is already
large enough.
Note that object literals with a getter and a setter for the same property still
do 2 calls, but this is a little bit more tricky to fix and will be handled in a
separate CL.
Review URL: https://chromiumcodereview.appspot.com/9616016
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@10956 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-03-07 13:24:44 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
var $regexpExec;
|
|
|
|
var $regexpExecNoTests;
|
|
|
|
var $regexpLastMatchInfo;
|
|
|
|
var $regexpLastMatchInfoOverride;
|
2015-03-12 15:44:32 +00:00
|
|
|
var harmony_regexps = false;
|
|
|
|
var harmony_unicode_regexps = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
var GlobalRegExp = global.RegExp;
|
|
|
|
var GlobalArray = global.Array;
|
|
|
|
|
|
|
|
// Property of the builtins object for recording the result of the last
|
|
|
|
// regexp match. The property $regexpLastMatchInfo includes the matchIndices
|
|
|
|
// array of the last successful regexp match (an array of start/end index
|
|
|
|
// pairs for the match and all the captured substrings), the invariant is
|
|
|
|
// that there are at least two capture indeces. The array also contains
|
|
|
|
// the subject string for the last successful match.
|
|
|
|
$regexpLastMatchInfo = new InternalPackedArray(
|
|
|
|
2, // REGEXP_NUMBER_OF_CAPTURES
|
|
|
|
"", // Last subject.
|
|
|
|
UNDEFINED, // Last input - settable with RegExpSetInput.
|
|
|
|
0, // REGEXP_FIRST_CAPTURE + 0
|
|
|
|
0 // REGEXP_FIRST_CAPTURE + 1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Override last match info with an array of actual substrings.
|
|
|
|
// Used internally by replace regexp with function.
|
|
|
|
// The array has the format of an "apply" argument for a replacement
|
|
|
|
// function.
|
|
|
|
$regexpLastMatchInfoOverride = null;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// A recursive descent parser for Patterns according to the grammar of
|
|
|
|
// ECMA-262 15.10.1, with deviations noted below.
|
2010-12-17 11:57:10 +00:00
|
|
|
function DoConstructRegExp(object, pattern, flags) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// RegExp : Called as constructor; see ECMA-262, section 15.10.4.
|
|
|
|
if (IS_REGEXP(pattern)) {
|
|
|
|
if (!IS_UNDEFINED(flags)) {
|
|
|
|
throw MakeTypeError('regexp_flags', []);
|
|
|
|
}
|
|
|
|
flags = (pattern.global ? 'g' : '')
|
|
|
|
+ (pattern.ignoreCase ? 'i' : '')
|
|
|
|
+ (pattern.multiline ? 'm' : '');
|
2015-02-05 14:16:54 +00:00
|
|
|
if (harmony_unicode_regexps)
|
2015-01-12 09:50:15 +00:00
|
|
|
flags += (pattern.unicode ? 'u' : '');
|
2014-09-19 07:36:05 +00:00
|
|
|
if (harmony_regexps)
|
|
|
|
flags += (pattern.sticky ? 'y' : '');
|
2008-07-03 15:10:15 +00:00
|
|
|
pattern = pattern.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern = IS_UNDEFINED(pattern) ? '' : ToString(pattern);
|
|
|
|
flags = IS_UNDEFINED(flags) ? '' : ToString(flags);
|
|
|
|
|
2014-11-19 14:13:44 +00:00
|
|
|
%RegExpInitializeAndCompile(object, pattern, flags);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
function RegExpConstructor(pattern, flags) {
|
2009-06-29 08:14:06 +00:00
|
|
|
if (%_IsConstructCall()) {
|
2010-12-17 11:57:10 +00:00
|
|
|
DoConstructRegExp(this, pattern, flags);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
// RegExp : Called as function; see ECMA-262, section 15.10.3.1.
|
|
|
|
if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) {
|
|
|
|
return pattern;
|
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
return new GlobalRegExp(pattern, flags);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Deprecated RegExp.prototype.compile method. We behave like the constructor
|
|
|
|
// were called again. In SpiderMonkey, this method returns the regexp object.
|
2009-03-11 14:00:55 +00:00
|
|
|
// In JSC, it returns undefined. For compatibility with JSC, we match their
|
2008-07-03 15:10:15 +00:00
|
|
|
// behavior.
|
2014-05-14 08:51:10 +00:00
|
|
|
function RegExpCompileJS(pattern, flags) {
|
2009-03-11 14:00:55 +00:00
|
|
|
// Both JSC and SpiderMonkey treat a missing pattern argument as the
|
2008-07-03 15:10:15 +00:00
|
|
|
// empty subject string, and an actual undefined value passed as the
|
2009-03-11 14:00:55 +00:00
|
|
|
// pattern as the string 'undefined'. Note that JSC is inconsistent
|
2008-07-03 15:10:15 +00:00
|
|
|
// here, treating undefined values differently in
|
|
|
|
// RegExp.prototype.compile and in the constructor, where they are
|
2009-03-11 14:00:55 +00:00
|
|
|
// the empty string. For compatibility with JSC, we match their
|
2008-07-03 15:10:15 +00:00
|
|
|
// behavior.
|
2015-03-12 14:47:35 +00:00
|
|
|
if (this == GlobalRegExp.prototype) {
|
2011-09-26 08:42:01 +00:00
|
|
|
// We don't allow recompiling RegExp.prototype.
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['RegExp.prototype.compile', this]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) {
|
2010-12-17 11:57:10 +00:00
|
|
|
DoConstructRegExp(this, 'undefined', flags);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2010-12-17 11:57:10 +00:00
|
|
|
DoConstructRegExp(this, pattern, flags);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function DoRegExpExec(regexp, string, index) {
|
2015-03-12 14:47:35 +00:00
|
|
|
var result = %_RegExpExec(regexp, string, index, $regexpLastMatchInfo);
|
|
|
|
if (result !== null) $regexpLastMatchInfoOverride = null;
|
2010-04-21 08:33:04 +00:00
|
|
|
return result;
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-05-16 13:43:19 +00:00
|
|
|
// This is kind of performance sensitive, so we want to avoid unnecessary
|
|
|
|
// type checks on inputs. But we also don't want to inline it several times
|
|
|
|
// manually, so we use a macro :-)
|
|
|
|
macro RETURN_NEW_RESULT_FROM_MATCH_INFO(MATCHINFO, STRING)
|
|
|
|
var numResults = NUMBER_OF_CAPTURES(MATCHINFO) >> 1;
|
|
|
|
var start = MATCHINFO[CAPTURE0];
|
|
|
|
var end = MATCHINFO[CAPTURE1];
|
2014-05-16 15:42:00 +00:00
|
|
|
// Calculate the substring of the first match before creating the result array
|
|
|
|
// to avoid an unnecessary write barrier storing the first result.
|
|
|
|
var first = %_SubString(STRING, start, end);
|
2014-05-16 13:43:19 +00:00
|
|
|
var result = %_RegExpConstructResult(numResults, start, STRING);
|
2014-05-16 15:42:00 +00:00
|
|
|
result[0] = first;
|
|
|
|
if (numResults == 1) return result;
|
2010-12-13 12:19:10 +00:00
|
|
|
var j = REGEXP_FIRST_CAPTURE + 2;
|
|
|
|
for (var i = 1; i < numResults; i++) {
|
2014-05-16 13:43:19 +00:00
|
|
|
start = MATCHINFO[j++];
|
2012-09-14 12:01:12 +00:00
|
|
|
if (start != -1) {
|
2014-05-16 13:43:19 +00:00
|
|
|
end = MATCHINFO[j];
|
|
|
|
result[i] = %_SubString(STRING, start, end);
|
2010-04-13 09:31:03 +00:00
|
|
|
}
|
2012-09-14 12:01:12 +00:00
|
|
|
j++;
|
2010-04-13 09:31:03 +00:00
|
|
|
}
|
|
|
|
return result;
|
2014-05-16 13:43:19 +00:00
|
|
|
endmacro
|
2010-04-13 09:31:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
function RegExpExecNoTests(regexp, string, start) {
|
|
|
|
// Must be called with RegExp, string and positive integer as arguments.
|
2015-03-12 14:47:35 +00:00
|
|
|
var matchInfo = %_RegExpExec(regexp, string, start, $regexpLastMatchInfo);
|
2010-04-13 09:31:03 +00:00
|
|
|
if (matchInfo !== null) {
|
2015-03-12 14:47:35 +00:00
|
|
|
$regexpLastMatchInfoOverride = null;
|
2014-05-16 13:43:19 +00:00
|
|
|
RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string);
|
2010-04-13 09:31:03 +00:00
|
|
|
}
|
2012-12-05 12:32:25 +00:00
|
|
|
regexp.lastIndex = 0;
|
2010-12-17 11:57:10 +00:00
|
|
|
return null;
|
2010-04-13 09:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
function RegExpExec(string) {
|
2010-03-17 10:23:06 +00:00
|
|
|
if (!IS_REGEXP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['RegExp.prototype.exec', this]);
|
|
|
|
}
|
|
|
|
|
2010-12-13 12:19:10 +00:00
|
|
|
string = TO_STRING_INLINE(string);
|
2010-11-02 13:37:59 +00:00
|
|
|
var lastIndex = this.lastIndex;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-10-14 12:54:00 +00:00
|
|
|
// Conversion is required by the ES5 specification (RegExp.prototype.exec
|
|
|
|
// algorithm, step 5) even if the value is discarded for non-global RegExps.
|
|
|
|
var i = TO_INTEGER(lastIndex);
|
2010-11-02 13:37:59 +00:00
|
|
|
|
2014-09-19 07:36:05 +00:00
|
|
|
var updateLastIndex = this.global || (harmony_regexps && this.sticky);
|
|
|
|
if (updateLastIndex) {
|
2010-12-13 12:19:10 +00:00
|
|
|
if (i < 0 || i > string.length) {
|
2010-10-14 12:54:00 +00:00
|
|
|
this.lastIndex = 0;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
// matchIndices is either null or the $regexpLastMatchInfo array.
|
|
|
|
var matchIndices = %_RegExpExec(this, string, i, $regexpLastMatchInfo);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-10-17 10:02:45 +00:00
|
|
|
if (IS_NULL(matchIndices)) {
|
2012-12-05 12:32:25 +00:00
|
|
|
this.lastIndex = 0;
|
2010-11-02 13:37:59 +00:00
|
|
|
return null;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2010-10-14 12:54:00 +00:00
|
|
|
|
|
|
|
// Successful match.
|
2015-03-12 14:47:35 +00:00
|
|
|
$regexpLastMatchInfoOverride = null;
|
2014-09-19 07:36:05 +00:00
|
|
|
if (updateLastIndex) {
|
2015-03-12 14:47:35 +00:00
|
|
|
this.lastIndex = $regexpLastMatchInfo[CAPTURE1];
|
2010-03-10 12:21:00 +00:00
|
|
|
}
|
2014-05-16 13:43:19 +00:00
|
|
|
RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2010-06-04 11:58:40 +00:00
|
|
|
// One-element cache for the simplified test regexp.
|
|
|
|
var regexp_key;
|
|
|
|
var regexp_val;
|
|
|
|
|
2009-03-11 14:00:55 +00:00
|
|
|
// Section 15.10.6.3 doesn't actually make sense, but the intention seems to be
|
2009-03-13 10:22:38 +00:00
|
|
|
// that test is defined in terms of String.prototype.exec. However, it probably
|
|
|
|
// means the original value of String.prototype.exec, which is what everybody
|
|
|
|
// else implements.
|
2008-07-03 15:10:15 +00:00
|
|
|
function RegExpTest(string) {
|
2009-03-11 14:00:55 +00:00
|
|
|
if (!IS_REGEXP(this)) {
|
2010-02-26 15:46:57 +00:00
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
2009-03-11 14:00:55 +00:00
|
|
|
['RegExp.prototype.test', this]);
|
|
|
|
}
|
2010-12-13 12:19:10 +00:00
|
|
|
string = TO_STRING_INLINE(string);
|
2010-10-14 12:54:00 +00:00
|
|
|
|
2010-11-02 13:37:59 +00:00
|
|
|
var lastIndex = this.lastIndex;
|
|
|
|
|
2010-10-14 12:54:00 +00:00
|
|
|
// Conversion is required by the ES5 specification (RegExp.prototype.exec
|
|
|
|
// algorithm, step 5) even if the value is discarded for non-global RegExps.
|
|
|
|
var i = TO_INTEGER(lastIndex);
|
2011-07-05 06:19:53 +00:00
|
|
|
|
2014-09-19 07:36:05 +00:00
|
|
|
if (this.global || (harmony_regexps && this.sticky)) {
|
2010-12-13 12:19:10 +00:00
|
|
|
if (i < 0 || i > string.length) {
|
2010-10-14 12:54:00 +00:00
|
|
|
this.lastIndex = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
// matchIndices is either null or the $regexpLastMatchInfo array.
|
|
|
|
var matchIndices = %_RegExpExec(this, string, i, $regexpLastMatchInfo);
|
2013-10-17 10:02:45 +00:00
|
|
|
if (IS_NULL(matchIndices)) {
|
2010-11-11 08:47:30 +00:00
|
|
|
this.lastIndex = 0;
|
|
|
|
return false;
|
2010-06-04 11:58:40 +00:00
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
$regexpLastMatchInfoOverride = null;
|
|
|
|
this.lastIndex = $regexpLastMatchInfo[CAPTURE1];
|
2011-07-05 06:19:53 +00:00
|
|
|
return true;
|
2010-11-11 08:47:30 +00:00
|
|
|
} else {
|
2014-09-19 07:36:05 +00:00
|
|
|
// Non-global, non-sticky regexp.
|
|
|
|
// Remove irrelevant preceeding '.*' in a test regexp. The expression
|
|
|
|
// 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
|
2012-03-15 17:21:42 +00:00
|
|
|
var regexp = this;
|
2014-09-19 07:36:05 +00:00
|
|
|
if (regexp.source.length >= 3 &&
|
|
|
|
%_StringCharCodeAt(regexp.source, 0) == 46 && // '.'
|
2012-03-15 17:21:42 +00:00
|
|
|
%_StringCharCodeAt(regexp.source, 1) == 42 && // '*'
|
|
|
|
%_StringCharCodeAt(regexp.source, 2) != 63) { // '?'
|
|
|
|
regexp = TrimRegExp(regexp);
|
2011-07-05 06:19:53 +00:00
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
// matchIndices is either null or the $regexpLastMatchInfo array.
|
|
|
|
var matchIndices = %_RegExpExec(regexp, string, 0, $regexpLastMatchInfo);
|
2013-10-17 10:02:45 +00:00
|
|
|
if (IS_NULL(matchIndices)) {
|
2012-12-05 12:32:25 +00:00
|
|
|
this.lastIndex = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
$regexpLastMatchInfoOverride = null;
|
2010-11-11 08:47:30 +00:00
|
|
|
return true;
|
2010-10-14 12:54:00 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-03-15 17:21:42 +00:00
|
|
|
function TrimRegExp(regexp) {
|
|
|
|
if (!%_ObjectEquals(regexp_key, regexp)) {
|
|
|
|
regexp_key = regexp;
|
|
|
|
regexp_val =
|
2015-03-12 14:47:35 +00:00
|
|
|
new GlobalRegExp(%_SubString(regexp.source, 2, regexp.source.length),
|
|
|
|
(regexp.ignoreCase ? regexp.multiline ? "im" : "i"
|
|
|
|
: regexp.multiline ? "m" : ""));
|
2012-03-15 17:21:42 +00:00
|
|
|
}
|
|
|
|
return regexp_val;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
function RegExpToString() {
|
2012-05-23 20:48:08 +00:00
|
|
|
if (!IS_REGEXP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['RegExp.prototype.toString', this]);
|
|
|
|
}
|
2012-04-23 13:59:43 +00:00
|
|
|
var result = '/' + this.source + '/';
|
2010-10-14 12:54:00 +00:00
|
|
|
if (this.global) result += 'g';
|
|
|
|
if (this.ignoreCase) result += 'i';
|
|
|
|
if (this.multiline) result += 'm';
|
2015-02-05 14:16:54 +00:00
|
|
|
if (harmony_unicode_regexps && this.unicode) result += 'u';
|
2014-09-19 07:36:05 +00:00
|
|
|
if (harmony_regexps && this.sticky) result += 'y';
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Getters for the static properties lastMatch, lastParen, leftContext, and
|
|
|
|
// rightContext of the RegExp constructor. The properties are computed based
|
|
|
|
// on the captures array of the last successful match and the subject string
|
|
|
|
// of the last successful match.
|
|
|
|
function RegExpGetLastMatch() {
|
2015-03-12 14:47:35 +00:00
|
|
|
if ($regexpLastMatchInfoOverride !== null) {
|
|
|
|
return OVERRIDE_MATCH($regexpLastMatchInfoOverride);
|
2010-04-21 08:33:04 +00:00
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
var regExpSubject = LAST_SUBJECT($regexpLastMatchInfo);
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(regExpSubject,
|
2015-03-12 14:47:35 +00:00
|
|
|
$regexpLastMatchInfo[CAPTURE0],
|
|
|
|
$regexpLastMatchInfo[CAPTURE1]);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
function RegExpGetLastParen() {
|
2015-03-12 14:47:35 +00:00
|
|
|
if ($regexpLastMatchInfoOverride) {
|
|
|
|
var override = $regexpLastMatchInfoOverride;
|
2010-03-30 07:15:23 +00:00
|
|
|
if (override.length <= 3) return '';
|
2010-03-25 12:57:58 +00:00
|
|
|
return override[override.length - 3];
|
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
var length = NUMBER_OF_CAPTURES($regexpLastMatchInfo);
|
2009-03-11 14:00:55 +00:00
|
|
|
if (length <= 2) return ''; // There were no captures.
|
2008-07-03 15:10:15 +00:00
|
|
|
// We match the SpiderMonkey behavior: return the substring defined by the
|
|
|
|
// last pair (after the first pair) of elements of the capture array even if
|
|
|
|
// it is empty.
|
2015-03-12 14:47:35 +00:00
|
|
|
var regExpSubject = LAST_SUBJECT($regexpLastMatchInfo);
|
|
|
|
var start = $regexpLastMatchInfo[CAPTURE(length - 2)];
|
|
|
|
var end = $regexpLastMatchInfo[CAPTURE(length - 1)];
|
2009-03-11 14:00:55 +00:00
|
|
|
if (start != -1 && end != -1) {
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(regExpSubject, start, end);
|
2009-03-11 14:00:55 +00:00
|
|
|
}
|
|
|
|
return "";
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
function RegExpGetLeftContext() {
|
2010-03-25 12:57:58 +00:00
|
|
|
var start_index;
|
|
|
|
var subject;
|
2015-03-12 14:47:35 +00:00
|
|
|
if (!$regexpLastMatchInfoOverride) {
|
|
|
|
start_index = $regexpLastMatchInfo[CAPTURE0];
|
|
|
|
subject = LAST_SUBJECT($regexpLastMatchInfo);
|
2010-03-25 12:57:58 +00:00
|
|
|
} else {
|
2015-03-12 14:47:35 +00:00
|
|
|
var override = $regexpLastMatchInfoOverride;
|
2012-04-13 11:03:22 +00:00
|
|
|
start_index = OVERRIDE_POS(override);
|
|
|
|
subject = OVERRIDE_SUBJECT(override);
|
2010-03-25 12:57:58 +00:00
|
|
|
}
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(subject, 0, start_index);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
function RegExpGetRightContext() {
|
2010-03-25 12:57:58 +00:00
|
|
|
var start_index;
|
|
|
|
var subject;
|
2015-03-12 14:47:35 +00:00
|
|
|
if (!$regexpLastMatchInfoOverride) {
|
|
|
|
start_index = $regexpLastMatchInfo[CAPTURE1];
|
|
|
|
subject = LAST_SUBJECT($regexpLastMatchInfo);
|
2010-03-25 12:57:58 +00:00
|
|
|
} else {
|
2015-03-12 14:47:35 +00:00
|
|
|
var override = $regexpLastMatchInfoOverride;
|
2012-04-13 11:03:22 +00:00
|
|
|
subject = OVERRIDE_SUBJECT(override);
|
|
|
|
var match = OVERRIDE_MATCH(override);
|
|
|
|
start_index = OVERRIDE_POS(override) + match.length;
|
2010-03-25 12:57:58 +00:00
|
|
|
}
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(subject, start_index, subject.length);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// The properties $1..$9 are the first nine capturing substrings of the last
|
|
|
|
// successful match, or ''. The function RegExpMakeCaptureGetter will be
|
2009-03-17 12:44:20 +00:00
|
|
|
// called with indices from 1 to 9.
|
2008-07-03 15:10:15 +00:00
|
|
|
function RegExpMakeCaptureGetter(n) {
|
2015-03-12 14:47:35 +00:00
|
|
|
return function foo() {
|
|
|
|
if ($regexpLastMatchInfoOverride) {
|
|
|
|
if (n < $regexpLastMatchInfoOverride.length - 2) {
|
|
|
|
return OVERRIDE_CAPTURE($regexpLastMatchInfoOverride, n);
|
2012-04-13 11:03:22 +00:00
|
|
|
}
|
2010-03-25 12:57:58 +00:00
|
|
|
return '';
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
var index = n * 2;
|
2015-03-12 14:47:35 +00:00
|
|
|
if (index >= NUMBER_OF_CAPTURES($regexpLastMatchInfo)) return '';
|
|
|
|
var matchStart = $regexpLastMatchInfo[CAPTURE(index)];
|
|
|
|
var matchEnd = $regexpLastMatchInfo[CAPTURE(index + 1)];
|
2008-07-03 15:10:15 +00:00
|
|
|
if (matchStart == -1 || matchEnd == -1) return '';
|
2015-03-12 14:47:35 +00:00
|
|
|
return %_SubString(LAST_SUBJECT($regexpLastMatchInfo), matchStart, matchEnd);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 07:14:31 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
%FunctionSetInstanceClassName(GlobalRegExp, 'RegExp');
|
|
|
|
%AddNamedProperty(
|
|
|
|
GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM);
|
|
|
|
%SetCode(GlobalRegExp, RegExpConstructor);
|
|
|
|
|
|
|
|
InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, GlobalArray(
|
|
|
|
"exec", RegExpExec,
|
|
|
|
"test", RegExpTest,
|
|
|
|
"toString", RegExpToString,
|
|
|
|
"compile", RegExpCompileJS
|
|
|
|
));
|
|
|
|
|
|
|
|
// The length of compile is 1 in SpiderMonkey.
|
|
|
|
%FunctionSetLength(GlobalRegExp.prototype.compile, 1);
|
|
|
|
|
|
|
|
// The properties `input` and `$_` are aliases for each other. When this
|
|
|
|
// value is set the value it is set to is coerced to a string.
|
|
|
|
// Getter and setter for the input.
|
|
|
|
var RegExpGetInput = function() {
|
|
|
|
var regExpInput = LAST_INPUT($regexpLastMatchInfo);
|
|
|
|
return IS_UNDEFINED(regExpInput) ? "" : regExpInput;
|
|
|
|
};
|
|
|
|
var RegExpSetInput = function(string) {
|
|
|
|
LAST_INPUT($regexpLastMatchInfo) = ToString(string);
|
|
|
|
};
|
|
|
|
|
|
|
|
%OptimizeObjectForAddingMultipleProperties(GlobalRegExp, 22);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, 'input', RegExpGetInput,
|
|
|
|
RegExpSetInput, DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, '$_', RegExpGetInput,
|
|
|
|
RegExpSetInput, DONT_ENUM | DONT_DELETE);
|
|
|
|
|
|
|
|
// The properties multiline and $* are aliases for each other. When this
|
|
|
|
// value is set in SpiderMonkey, the value it is set to is coerced to a
|
|
|
|
// boolean. We mimic that behavior with a slight difference: in SpiderMonkey
|
|
|
|
// the value of the expression 'RegExp.multiline = null' (for instance) is the
|
|
|
|
// boolean false (i.e., the value after coercion), while in V8 it is the value
|
|
|
|
// null (i.e., the value before coercion).
|
|
|
|
|
|
|
|
// Getter and setter for multiline.
|
|
|
|
var multiline = false;
|
|
|
|
var RegExpGetMultiline = function() { return multiline; };
|
|
|
|
var RegExpSetMultiline = function(flag) { multiline = flag ? true : false; };
|
|
|
|
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, 'multiline', RegExpGetMultiline,
|
|
|
|
RegExpSetMultiline, DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, '$*', RegExpGetMultiline,
|
|
|
|
RegExpSetMultiline,
|
|
|
|
DONT_ENUM | DONT_DELETE);
|
|
|
|
|
|
|
|
|
|
|
|
var NoOpSetter = function(ignored) {};
|
|
|
|
|
|
|
|
|
|
|
|
// Static properties set by a successful match.
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, 'lastMatch', RegExpGetLastMatch,
|
|
|
|
NoOpSetter, DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, '$&', RegExpGetLastMatch,
|
|
|
|
NoOpSetter, DONT_ENUM | DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, 'lastParen', RegExpGetLastParen,
|
|
|
|
NoOpSetter, DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, '$+', RegExpGetLastParen,
|
|
|
|
NoOpSetter, DONT_ENUM | DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, 'leftContext',
|
|
|
|
RegExpGetLeftContext, NoOpSetter,
|
|
|
|
DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, '$`', RegExpGetLeftContext,
|
|
|
|
NoOpSetter, DONT_ENUM | DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, 'rightContext',
|
|
|
|
RegExpGetRightContext, NoOpSetter,
|
|
|
|
DONT_DELETE);
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, "$'", RegExpGetRightContext,
|
|
|
|
NoOpSetter, DONT_ENUM | DONT_DELETE);
|
|
|
|
|
|
|
|
for (var i = 1; i < 10; ++i) {
|
|
|
|
%DefineAccessorPropertyUnchecked(GlobalRegExp, '$' + i,
|
|
|
|
RegExpMakeCaptureGetter(i), NoOpSetter,
|
2014-06-27 13:48:37 +00:00
|
|
|
DONT_DELETE);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2015-03-12 14:47:35 +00:00
|
|
|
%ToFastProperties(GlobalRegExp);
|
|
|
|
|
|
|
|
$regexpExecNoTests = RegExpExecNoTests;
|
|
|
|
$regexpExec = DoRegExpExec;
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
})();
|