Skip to content

Commit b421119

Browse files
cjihrigMyles Borins
authored and
Myles Borins
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 acc3d66 commit b421119

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

@@ -305,7 +304,7 @@ function formatValue(ctx, value, recurseTimes) {
305304
braces = ['[', ']'];
306305
empty = value.length === 0;
307306
formatter = formatArray;
308-
} else if (objectToString(value) === '[object Set]') {
307+
} else if (binding.isSet(value)) {
309308
braces = ['{', '}'];
310309
// With `showHidden`, `length` will display as a hidden property for
311310
// arrays. For consistency's sake, do the same for `size`, even though this
@@ -314,7 +313,7 @@ function formatValue(ctx, value, recurseTimes) {
314313
keys.unshift('size');
315314
empty = value.size === 0;
316315
formatter = formatSet;
317-
} else if (objectToString(value) === '[object Map]') {
316+
} else if (binding.isMap(value)) {
318317
braces = ['{', '}'];
319318
// Ditto.
320319
if (ctx.showHidden)
@@ -673,7 +672,7 @@ function isUndefined(arg) {
673672
exports.isUndefined = isUndefined;
674673

675674
function isRegExp(re) {
676-
return objectToString(re) === '[object RegExp]';
675+
return binding.isRegExp(re);
677676
}
678677
exports.isRegExp = isRegExp;
679678

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

685684
function isDate(d) {
686-
return objectToString(d) === '[object Date]';
685+
return binding.isDate(d);
687686
}
688687
exports.isDate = isDate;
689688

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());
@@ -50,7 +75,11 @@ void Initialize(Local<Object> target,
5075
Local<Value> unused,
5176
Local<Context> context) {
5277
Environment* env = Environment::GetCurrent(context);
78+
env->SetMethod(target, "isRegExp", IsRegExp);
79+
env->SetMethod(target, "isDate", IsDate);
80+
env->SetMethod(target, "isMap", IsMap);
5381
env->SetMethod(target, "isMapIterator", IsMapIterator);
82+
env->SetMethod(target, "isSet", IsSet);
5483
env->SetMethod(target, "isSetIterator", IsSetIterator);
5584
env->SetMethod(target, "isPromise", IsPromise);
5685
env->SetMethod(target, "getHiddenValue", GetHiddenValue);

0 commit comments

Comments
 (0)