Skip to content

Commit 6526ae7

Browse files
committed
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: #4100 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent fbcd687 commit 6526ae7

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/util.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const internalUtil = require('internal/util');
66
const binding = process.binding('util');
77

88
const isError = internalUtil.isError;
9-
const objectToString = internalUtil.objectToString;
109

1110
var Debug;
1211

@@ -312,7 +311,7 @@ function formatValue(ctx, value, recurseTimes) {
312311
braces = ['[', ']'];
313312
empty = value.length === 0;
314313
formatter = formatArray;
315-
} else if (objectToString(value) === '[object Set]') {
314+
} else if (binding.isSet(value)) {
316315
braces = ['{', '}'];
317316
// With `showHidden`, `length` will display as a hidden property for
318317
// arrays. For consistency's sake, do the same for `size`, even though this
@@ -321,7 +320,7 @@ function formatValue(ctx, value, recurseTimes) {
321320
keys.unshift('size');
322321
empty = value.size === 0;
323322
formatter = formatSet;
324-
} else if (objectToString(value) === '[object Map]') {
323+
} else if (binding.isMap(value)) {
325324
braces = ['{', '}'];
326325
// Ditto.
327326
if (ctx.showHidden)
@@ -719,7 +718,7 @@ function isUndefined(arg) {
719718
exports.isUndefined = isUndefined;
720719

721720
function isRegExp(re) {
722-
return objectToString(re) === '[object RegExp]';
721+
return binding.isRegExp(re);
723722
}
724723
exports.isRegExp = isRegExp;
725724

@@ -729,7 +728,7 @@ function isObject(arg) {
729728
exports.isObject = isObject;
730729

731730
function isDate(d) {
732-
return objectToString(d) === '[object Date]';
731+
return binding.isDate(d);
733732
}
734733
exports.isDate = isDate;
735734

src/node_util.cc

+29
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,37 @@ using v8::Object;
1313
using v8::String;
1414
using v8::Value;
1515

16+
17+
static void IsRegExp(const FunctionCallbackInfo<Value>& args) {
18+
CHECK_EQ(1, args.Length());
19+
args.GetReturnValue().Set(args[0]->IsRegExp());
20+
}
21+
22+
23+
static void IsDate(const FunctionCallbackInfo<Value>& args) {
24+
CHECK_EQ(1, args.Length());
25+
args.GetReturnValue().Set(args[0]->IsDate());
26+
}
27+
28+
29+
static void IsMap(const FunctionCallbackInfo<Value>& args) {
30+
CHECK_EQ(1, args.Length());
31+
args.GetReturnValue().Set(args[0]->IsMap());
32+
}
33+
34+
1635
static void IsMapIterator(const FunctionCallbackInfo<Value>& args) {
1736
CHECK_EQ(1, args.Length());
1837
args.GetReturnValue().Set(args[0]->IsMapIterator());
1938
}
2039

2140

41+
static void IsSet(const FunctionCallbackInfo<Value>& args) {
42+
CHECK_EQ(1, args.Length());
43+
args.GetReturnValue().Set(args[0]->IsSet());
44+
}
45+
46+
2247
static void IsSetIterator(const FunctionCallbackInfo<Value>& args) {
2348
CHECK_EQ(1, args.Length());
2449
args.GetReturnValue().Set(args[0]->IsSetIterator());
@@ -68,7 +93,11 @@ void Initialize(Local<Object> target,
6893
Local<Value> unused,
6994
Local<Context> context) {
7095
Environment* env = Environment::GetCurrent(context);
96+
env->SetMethod(target, "isRegExp", IsRegExp);
97+
env->SetMethod(target, "isDate", IsDate);
98+
env->SetMethod(target, "isMap", IsMap);
7199
env->SetMethod(target, "isMapIterator", IsMapIterator);
100+
env->SetMethod(target, "isSet", IsSet);
72101
env->SetMethod(target, "isSetIterator", IsSetIterator);
73102
env->SetMethod(target, "isPromise", IsPromise);
74103
env->SetMethod(target, "isTypedArray", IsTypedArray);

0 commit comments

Comments
 (0)