Permalink
Browse files

Fix handling of regular expression in util.inspect().

Regular expressions in V8 are no longer instances of Function
and this broke a number of assumptions in util.inspect().

Fixes #1260.
  • Loading branch information...
1 parent 6d8b43c commit d74c059b333d74026b72b9de2e7c3c64e4fa90ac @bnoordhuis committed Jul 3, 2011
Showing with 14 additions and 9 deletions.
  1. +9 −9 lib/util.js
  2. +5 −0 test/simple/test-sys.js
View
@@ -139,12 +139,12 @@ exports.inspect = function(obj, showHidden, depth, colors) {
var keys = showHidden ? Object.getOwnPropertyNames(value) : visible_keys;
// Functions without properties can be shortcutted.
- if (typeof value === 'function' && keys.length === 0) {
- if (isRegExp(value)) {
- return stylize('' + value, 'regexp');
- } else {
+ if (keys.length === 0) {
+ if (typeof value === 'function' && keys.length === 0) {
var name = value.name ? ': ' + value.name : '';
return stylize('[Function' + name + ']', 'special');
+ } else if (isRegExp(value)) {
+ return stylize('' + value, 'regexp');
}
}
@@ -166,7 +166,9 @@ exports.inspect = function(obj, showHidden, depth, colors) {
// Make functions say that they are functions
if (typeof value === 'function') {
var n = value.name ? ': ' + value.name : '';
- base = (isRegExp(value)) ? ' ' + value : ' [Function' + n + ']';
+ base = ' [Function' + n + ']';
+ } else if (isRegExp(value)) {
+ base = ' ' + value;
} else {
base = '';
}
@@ -284,15 +286,13 @@ function isArray(ar) {
function isRegExp(re) {
- var s = '' + re;
return re instanceof RegExp || // easy case
// duck-type for context-switching evalcx case
- typeof(re) === 'function' &&
- re.constructor.name === 'RegExp' &&
+ re.constructor === RegExp &&
re.compile &&
re.test &&
re.exec &&
- s.match(/^\/.*\/[gim]{0,3}$/);
+ ('' + s).match(/^\/.*\/[gim]{0,3}$/);
}
@@ -22,6 +22,11 @@
var common = require('../common');
var assert = require('assert');
+// Verify our assumptions of what a regular expression is
+assert.equal(false, /abc/ instanceof Function);
+assert.equal(true, /abc/ instanceof Object);
+assert.equal(true, /abc/ instanceof RegExp);
+
assert.equal('0', common.inspect(0));
assert.equal('1', common.inspect(1));
assert.equal('false', common.inspect(false));

0 comments on commit d74c059

Please sign in to comment.