aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNathan Rajlich <nathan@tootallnate.net>2011-10-25 21:42:23 -0700
committerkoichik <koichik@improvement.jp>2011-10-27 04:33:24 +0900
commit2dbb470ea10697f7514094934dfbf635969b8566 (patch)
treeb75027ba9b36675e034cdff90b74f1361fe64158 /lib
parent194511ff1a0309e01f6cad03292ccc2e67134390 (diff)
downloadandroid-node-v8-2dbb470ea10697f7514094934dfbf635969b8566.tar.gz
android-node-v8-2dbb470ea10697f7514094934dfbf635969b8566.tar.bz2
android-node-v8-2dbb470ea10697f7514094934dfbf635969b8566.zip
Don't use `instanceof` in lib/util.js "is" checks.
While using `instanceof`, these functions could easily be faked with something like: Object.create(Date.prototype) So let's just not use it at all. A little slower, but these functions are only used in the REPL / for debugging so it's OK. Fixes #1941. Fixes #1942.
Diffstat (limited to 'lib')
-rw-r--r--lib/util.js16
1 files changed, 7 insertions, 9 deletions
diff --git a/lib/util.js b/lib/util.js
index 098aab2432..07e2e3ba99 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -378,31 +378,29 @@ function reduceToSingleString(output, base, braces) {
}
+// NOTE: These type checking functions intentionally don't use `instanceof`
+// because it is fragile and can be easily faked with `Object.create()`.
function isArray(ar) {
- return ar instanceof Array ||
- Array.isArray(ar) ||
- (ar && ar !== Object.prototype && isArray(ar.__proto__));
+ return Array.isArray(ar) ||
+ (typeof ar === 'object' && objectToString(ar) === '[object Array]');
}
exports.isArray = isArray;
function isRegExp(re) {
- return re instanceof RegExp ||
- (typeof re === 'object' && objectToString(re) === '[object RegExp]');
+ return typeof re === 'object' && objectToString(re) === '[object RegExp]';
}
exports.isRegExp = isRegExp;
function isDate(d) {
- return d instanceof Date ||
- (typeof d === 'object' && objectToString(d) === '[object Date]');
+ return typeof d === 'object' && objectToString(d) === '[object Date]';
}
exports.isDate = isDate;
function isError(e) {
- return e instanceof Error ||
- (typeof e === 'object' && objectToString(e) === '[object Error]');
+ return typeof e === 'object' && objectToString(e) === '[object Error]';
}
exports.isError = isError;