2016-02-10 10:53:03 +00:00
|
|
|
// 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 =
|
|
|
|
{
|
2016-02-11 13:22:49 +00:00
|
|
|
get source() {
|
2016-02-10 10:53:03 +00:00
|
|
|
log.push("p");
|
|
|
|
return {
|
|
|
|
toString: function() {
|
|
|
|
log.push("ps");
|
|
|
|
return "pattern";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2016-02-11 13:22:49 +00:00
|
|
|
get flags() {
|
2016-02-10 10:53:03 +00:00
|
|
|
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);
|
2016-02-11 13:22:49 +00:00
|
|
|
Number.prototype.source = "a";
|
|
|
|
Number.prototype.flags = "b";
|
2016-02-10 10:53:03 +00:00
|
|
|
testThrows(1);
|
|
|
|
|
|
|
|
assertEquals("/pattern/flags", RegExp.prototype.toString.call(fake));
|
|
|
|
assertEquals(["p", "ps", "f", "fs"], log);
|
2016-04-04 07:05:47 +00:00
|
|
|
|
|
|
|
// Monkey-patching is also possible on RegExp instances
|
|
|
|
|
|
|
|
let weird = /foo/;
|
|
|
|
Object.defineProperty(weird, 'flags', {value: 'bar'});
|
|
|
|
Object.defineProperty(weird, 'source', {value: 'baz'});
|
|
|
|
assertEquals('/baz/bar', weird.toString());
|
|
|
|
|
|
|
|
assertEquals('/(?:)/', RegExp.prototype.toString());
|
|
|
|
assertEquals('(?:)', RegExp.prototype.source);
|
|
|
|
assertEquals('', RegExp.prototype.flags);
|