Commit 1d5a50d3 authored by yangguo's avatar yangguo Committed by Commit bot

[regexp] implement RegExp.prototype.toString for non-RegExp receiver.

R=littledan@chromium.org, verwaest@chromium.org
BUG=v8:4524
LOG=N

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

Cr-Commit-Position: refs/heads/master@{#33862}
parent 0fc7b2c4
...@@ -278,8 +278,11 @@ function RegExpToString() { ...@@ -278,8 +278,11 @@ function RegExpToString() {
%IncrementUseCounter(kRegExpPrototypeToString); %IncrementUseCounter(kRegExpPrototypeToString);
return '/(?:)/'; return '/(?:)/';
} }
throw MakeTypeError(kIncompatibleMethodReceiver, if (!IS_RECEIVER(this)) {
'RegExp.prototype.toString', this); throw MakeTypeError(
kIncompatibleMethodReceiver, 'RegExp.prototype.toString', this);
}
return '/' + TO_STRING(this.pattern()) + '/' + TO_STRING(this.flags());
} }
var result = '/' + REGEXP_SOURCE(this) + '/'; var result = '/' + REGEXP_SOURCE(this) + '/';
if (REGEXP_GLOBAL(this)) result += 'g'; if (REGEXP_GLOBAL(this)) result += 'g';
......
// Copyright 2016 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.
var log = [];
var fake =
{
pattern: function() {
log.push("p");
return {
toString: function() {
log.push("ps");
return "pattern";
}
};
},
flags: function() {
log.push("f");
return {
toString: function() {
log.push("fs");
return "flags";
}
};
}
}
function testThrows(x) {
try {
RegExp.prototype.toString.call(x);
} catch (e) {
assertTrue(/incompatible receiver/.test(e.message));
return;
}
assertUnreachable();
}
testThrows(1);
testThrows(null);
Number.prototype.pattern = () => "a";
Number.prototype.flags = () => "b";
testThrows(1);
assertEquals("/pattern/flags", RegExp.prototype.toString.call(fake));
assertEquals(["p", "ps", "f", "fs"], log);
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