util: add util.types.isBoxedPrimitive

Checking all boxed primitives individually requires to cross the C++
barrier multiple times besides being more complicated than just a
single check.

PR-URL: https://github.com/nodejs/node/pull/22620
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater authored and Michaël Zasso committed 2018-09-07 00:11:43 +02:00
1 parent 7c831b754a
commit 8cfa88aa5c
3 files changed
+45 -4

No files matched your search

+24 -3
View File
@@ -1054,10 +1054,31 @@ by `new Boolean()`.
```js
util.types.isBooleanObject(false); // Returns false
util.types.isBooleanObject(true); // Returns false
util.types.isBooleanObject(new Boolean(false)); // Returns true
util.types.isBooleanObject(new Boolean(true)); // Returns true
util.types.isBooleanObject(new Boolean(false)); // Returns true
util.types.isBooleanObject(new Boolean(true)); // Returns true
util.types.isBooleanObject(Boolean(false)); // Returns false
util.types.isBooleanObject(Boolean(true)); // Returns false
util.types.isBooleanObject(Boolean(true)); // Returns false
```
### util.types.isBoxedPrimitive(value)
<!-- YAML
added: REPLACEME
-->
* `value` {any}
* Returns: {boolean}
Returns `true` if the value is any boxed primitive object, e.g. created
by `new Boolean()`, `new String()` or `Object(Symbol())`.
For example:
```js
util.types.isBoxedPrimitive(false); // Returns false
util.types.isBoxedPrimitive(new Boolean(false)); // Returns true
util.types.isBoxedPrimitive(Symbol('foo')); // Returns false
util.types.isBoxedPrimitive(Object(Symbol('foo'))); // Returns true
util.types.isBoxedPrimitive(Object(BigInt(5))); // Returns true
```
### util.types.isDataView(value)
+10
View File
@@ -51,6 +51,15 @@ static void IsAnyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
}
static void IsBoxedPrimitive(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
args[0]->IsNumberObject() ||
args[0]->IsStringObject() ||
args[0]->IsBooleanObject() ||
args[0]->IsBigIntObject() ||
args[0]->IsSymbolObject());
}
void InitializeTypes(Local<Object> target,
Local<Value> unused,
Local<Context> context) {
@@ -63,6 +72,7 @@ void InitializeTypes(Local<Object> target,
#undef V
env->SetMethodNoSideEffect(target, "isAnyArrayBuffer", IsAnyArrayBuffer);
env->SetMethodNoSideEffect(target, "isBoxedPrimitive", IsBoxedPrimitive);
}
} // anonymous namespace
+11 -1
View File
@@ -57,7 +57,8 @@ for (const [ value, _method ] of [
for (const key of Object.keys(types)) {
if ((types.isArrayBufferView(value) ||
types.isAnyArrayBuffer(value)) && key.includes('Array')) {
types.isAnyArrayBuffer(value)) && key.includes('Array') ||
key === 'isBoxedPrimitive') {
continue;
}
@@ -68,6 +69,15 @@ for (const [ value, _method ] of [
}
}
// Check boxed primitives.
[
new Boolean(),
new Number(),
new String(),
Object(Symbol()),
Object(BigInt(0))
].forEach((entry) => assert(types.isBoxedPrimitive(entry)));
{
assert(!types.isUint8Array({ [Symbol.toStringTag]: 'Uint8Array' }));
assert(types.isUint8Array(vm.runInNewContext('new Uint8Array')));