diff options
author | cjihrig <cjihrig@gmail.com> | 2015-12-01 11:27:39 -0500 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2015-12-02 11:16:21 -0500 |
commit | 6526ae7b37316e301ff12accd2671640ae4a48c1 (patch) | |
tree | 4334de83a914df9bc5daac8434b0d59acdea5c91 | |
parent | fbcd687c08363053e2aff6ad29d906cc8df97f79 (diff) | |
download | android-node-v8-6526ae7b37316e301ff12accd2671640ae4a48c1.tar.gz android-node-v8-6526ae7b37316e301ff12accd2671640ae4a48c1.tar.bz2 android-node-v8-6526ae7b37316e301ff12accd2671640ae4a48c1.zip |
util: determine object types in C++
Determine object types of regular expressions, Dates, Maps, and
Sets in the C++ layer instead of depending on toString()
behavior in JavaScript.
PR-URL: https://github.com/nodejs/node/pull/4100
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Michaƫl Zasso <mic.besace@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
-rw-r--r-- | lib/util.js | 9 | ||||
-rw-r--r-- | src/node_util.cc | 29 |
2 files changed, 33 insertions, 5 deletions
diff --git a/lib/util.js b/lib/util.js index afc8bf280c..c1735e7c7c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -6,7 +6,6 @@ const internalUtil = require('internal/util'); const binding = process.binding('util'); const isError = internalUtil.isError; -const objectToString = internalUtil.objectToString; var Debug; @@ -312,7 +311,7 @@ function formatValue(ctx, value, recurseTimes) { braces = ['[', ']']; empty = value.length === 0; formatter = formatArray; - } else if (objectToString(value) === '[object Set]') { + } else if (binding.isSet(value)) { braces = ['{', '}']; // With `showHidden`, `length` will display as a hidden property for // arrays. For consistency's sake, do the same for `size`, even though this @@ -321,7 +320,7 @@ function formatValue(ctx, value, recurseTimes) { keys.unshift('size'); empty = value.size === 0; formatter = formatSet; - } else if (objectToString(value) === '[object Map]') { + } else if (binding.isMap(value)) { braces = ['{', '}']; // Ditto. if (ctx.showHidden) @@ -719,7 +718,7 @@ function isUndefined(arg) { exports.isUndefined = isUndefined; function isRegExp(re) { - return objectToString(re) === '[object RegExp]'; + return binding.isRegExp(re); } exports.isRegExp = isRegExp; @@ -729,7 +728,7 @@ function isObject(arg) { exports.isObject = isObject; function isDate(d) { - return objectToString(d) === '[object Date]'; + return binding.isDate(d); } exports.isDate = isDate; diff --git a/src/node_util.cc b/src/node_util.cc index dad005d760..b7eba15510 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -13,12 +13,37 @@ using v8::Object; using v8::String; using v8::Value; + +static void IsRegExp(const FunctionCallbackInfo<Value>& args) { + CHECK_EQ(1, args.Length()); + args.GetReturnValue().Set(args[0]->IsRegExp()); +} + + +static void IsDate(const FunctionCallbackInfo<Value>& args) { + CHECK_EQ(1, args.Length()); + args.GetReturnValue().Set(args[0]->IsDate()); +} + + +static void IsMap(const FunctionCallbackInfo<Value>& args) { + CHECK_EQ(1, args.Length()); + args.GetReturnValue().Set(args[0]->IsMap()); +} + + static void IsMapIterator(const FunctionCallbackInfo<Value>& args) { CHECK_EQ(1, args.Length()); args.GetReturnValue().Set(args[0]->IsMapIterator()); } +static void IsSet(const FunctionCallbackInfo<Value>& args) { + CHECK_EQ(1, args.Length()); + args.GetReturnValue().Set(args[0]->IsSet()); +} + + static void IsSetIterator(const FunctionCallbackInfo<Value>& args) { CHECK_EQ(1, args.Length()); args.GetReturnValue().Set(args[0]->IsSetIterator()); @@ -68,7 +93,11 @@ void Initialize(Local<Object> target, Local<Value> unused, Local<Context> context) { Environment* env = Environment::GetCurrent(context); + env->SetMethod(target, "isRegExp", IsRegExp); + env->SetMethod(target, "isDate", IsDate); + env->SetMethod(target, "isMap", IsMap); env->SetMethod(target, "isMapIterator", IsMapIterator); + env->SetMethod(target, "isSet", IsSet); env->SetMethod(target, "isSetIterator", IsSetIterator); env->SetMethod(target, "isPromise", IsPromise); env->SetMethod(target, "isTypedArray", IsTypedArray); |