Skip to content

Commit

Permalink
util: determine object types in C++
Browse files Browse the repository at this point in the history
Determine object types of regular expressions, Dates, Maps, and
Sets in the C++ layer instead of depending on toString()
behavior in JavaScript.

PR-URL: #4100
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
cjihrig authored and rvagg committed Dec 5, 2015
1 parent 1279adc commit 1b434e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const internalUtil = require('internal/util');
const binding = process.binding('util');

const isError = internalUtil.isError;
const objectToString = internalUtil.objectToString;

var Debug;

Expand Down Expand Up @@ -305,7 +304,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
Expand All @@ -314,7 +313,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)
Expand Down Expand Up @@ -673,7 +672,7 @@ function isUndefined(arg) {
exports.isUndefined = isUndefined;

function isRegExp(re) {
return objectToString(re) === '[object RegExp]';
return binding.isRegExp(re);
}
exports.isRegExp = isRegExp;

Expand All @@ -683,7 +682,7 @@ function isObject(arg) {
exports.isObject = isObject;

function isDate(d) {
return objectToString(d) === '[object Date]';
return binding.isDate(d);
}
exports.isDate = isDate;

Expand Down
29 changes: 29 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -50,7 +75,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, "getHiddenValue", GetHiddenValue);
Expand Down

0 comments on commit 1b434e0

Please sign in to comment.