Skip to content

Commit b421119

Browse files
cjihrigMyles Borins
authored andcommitted
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 <evanlucas@me.com> Reviewed-By: Michaël Zasso <mic.besace@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent acc3d66 commit b421119

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎lib/util.js‎

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

88
constisError=internalUtil.isError;
9-
constobjectToString=internalUtil.objectToString;
109

1110
varDebug;
1211

@@ -305,7 +304,7 @@ function formatValue(ctx, value, recurseTimes) {
305304
braces=['[',']'];
306305
empty=value.length===0;
307306
formatter=formatArray;
308-
}elseif(objectToString(value)==='[object Set]'){
307+
}elseif(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-
}elseif(objectToString(value)==='[object Map]'){
316+
}elseif(binding.isMap(value)){
318317
braces=['{','}'];
319318
// Ditto.
320319
if(ctx.showHidden)
@@ -673,7 +672,7 @@ function isUndefined(arg) {
673672
exports.isUndefined=isUndefined;
674673

675674
functionisRegExp(re){
676-
returnobjectToString(re)==='[object RegExp]';
675+
returnbinding.isRegExp(re);
677676
}
678677
exports.isRegExp=isRegExp;
679678

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

685684
functionisDate(d){
686-
returnobjectToString(d)==='[object Date]';
685+
returnbinding.isDate(d);
687686
}
688687
exports.isDate=isDate;
689688

‎src/node_util.cc‎

Lines changed: 29 additions & 0 deletions
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+
staticvoidIsRegExp(const FunctionCallbackInfo<Value>& args) {
18+
CHECK_EQ(1, args.Length());
19+
args.GetReturnValue().Set(args[0]->IsRegExp());
20+
}
21+
22+
23+
staticvoidIsDate(const FunctionCallbackInfo<Value>& args) {
24+
CHECK_EQ(1, args.Length());
25+
args.GetReturnValue().Set(args[0]->IsDate());
26+
}
27+
28+
29+
staticvoidIsMap(const FunctionCallbackInfo<Value>& args) {
30+
CHECK_EQ(1, args.Length());
31+
args.GetReturnValue().Set(args[0]->IsMap());
32+
}
33+
34+
1635
staticvoidIsMapIterator(const FunctionCallbackInfo<Value>& args) {
1736
CHECK_EQ(1, args.Length());
1837
args.GetReturnValue().Set(args[0]->IsMapIterator());
1938
}
2039

2140

41+
staticvoidIsSet(const FunctionCallbackInfo<Value>& args) {
42+
CHECK_EQ(1, args.Length());
43+
args.GetReturnValue().Set(args[0]->IsSet());
44+
}
45+
46+
2247
staticvoidIsSetIterator(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)