Skip to content

Commit e61937b

Browse files
util: add types.isFloat16Array()
PR-URL: #57879 Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 02edc31 commit e61937b

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

doc/api/util.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,6 +3102,23 @@ types.isExternal(new String('foo')); // returns false
31023102
For further information on `napi_create_external`, refer to
31033103
[`napi_create_external()`][].
31043104
3105+
### `util.types.isFloat16Array(value)`
3106+
3107+
<!-- YAML
3108+
added: REPLACEME
3109+
-->
3110+
3111+
* `value` {any}
3112+
* Returns: {boolean}
3113+
3114+
Returns `true` if the value is a built-in {Float16Array} instance.
3115+
3116+
```js
3117+
util.types.isFloat16Array(new ArrayBuffer()); // Returns false
3118+
util.types.isFloat16Array(new Float16Array()); // Returns true
3119+
util.types.isFloat16Array(new Float32Array()); // Returns false
3120+
```
3121+
31053122
### `util.types.isFloat32Array(value)`
31063123
31073124
<!-- YAML

lib/internal/util/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function isInt32Array(value) {
3838
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int32Array';
3939
}
4040

41+
function isFloat16Array(value) {
42+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Float16Array';
43+
}
44+
4145
function isFloat32Array(value) {
4246
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Float32Array';
4347
}
@@ -65,6 +69,7 @@ module.exports = {
6569
isInt8Array,
6670
isInt16Array,
6771
isInt32Array,
72+
isFloat16Array,
6873
isFloat32Array,
6974
isFloat64Array,
7075
isBigInt64Array,

test/parallel/test-util-types.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Flags: --experimental-vm-modules --expose-internals --allow-natives-syntax
1+
// Flags: --experimental-vm-modules --expose-internals --allow-natives-syntax --js-float16array
2+
// TODO(LiviaMedeiros): once `Float16Array` is unflagged in v8, remove `--js-float16array` above
23
'use strict';
34
const common = require('../common');
45
const assert = require('assert');
@@ -9,6 +10,9 @@ const { JSStream } = internalBinding('js_stream');
910

1011
const external = (new JSStream())._externalStream;
1112

13+
// TODO(LiviaMedeiros): once linter recognizes `Float16Array`, remove next line
14+
const { Float16Array } = globalThis;
15+
1216
for (const [ value, _method ] of [
1317
[ external, 'isExternal' ],
1418
[ new Date() ],
@@ -38,6 +42,7 @@ for (const [ value, _method ] of [
3842
[ new Int8Array() ],
3943
[ new Int16Array() ],
4044
[ new Int32Array() ],
45+
[ new Float16Array() ],
4146
[ new Float32Array() ],
4247
[ new Float64Array() ],
4348
[ new BigInt64Array() ],
@@ -102,6 +107,9 @@ for (const [ value, _method ] of [
102107
assert(!types.isInt32Array({ [Symbol.toStringTag]: 'Int32Array' }));
103108
assert(types.isInt32Array(vm.runInNewContext('new Int32Array')));
104109

110+
assert(!types.isFloat16Array({ [Symbol.toStringTag]: 'Float16Array' }));
111+
assert(types.isFloat16Array(vm.runInNewContext('new Float16Array')));
112+
105113
assert(!types.isFloat32Array({ [Symbol.toStringTag]: 'Float32Array' }));
106114
assert(types.isFloat32Array(vm.runInNewContext('new Float32Array')));
107115

@@ -127,6 +135,7 @@ for (const [ value, _method ] of [
127135
const int8Array = new Int8Array(arrayBuffer);
128136
const int16Array = new Int16Array(arrayBuffer);
129137
const int32Array = new Int32Array(arrayBuffer);
138+
const float16Array = new Float16Array(arrayBuffer);
130139
const float32Array = new Float32Array(arrayBuffer);
131140
const float64Array = new Float64Array(arrayBuffer);
132141
const bigInt64Array = new BigInt64Array(arrayBuffer);
@@ -141,6 +150,7 @@ for (const [ value, _method ] of [
141150
const fakeInt8Array = { __proto__: Int8Array.prototype };
142151
const fakeInt16Array = { __proto__: Int16Array.prototype };
143152
const fakeInt32Array = { __proto__: Int32Array.prototype };
153+
const fakeFloat16Array = { __proto__: Float16Array.prototype };
144154
const fakeFloat32Array = { __proto__: Float32Array.prototype };
145155
const fakeFloat64Array = { __proto__: Float64Array.prototype };
146156
const fakeBigInt64Array = { __proto__: BigInt64Array.prototype };
@@ -164,6 +174,10 @@ for (const [ value, _method ] of [
164174
Object.setPrototypeOf(new Int16Array(arrayBuffer), Int16Array.prototype);
165175
const stealthyInt32Array =
166176
Object.setPrototypeOf(new Int32Array(arrayBuffer), Int32Array.prototype);
177+
const stealthyFloat16Array =
178+
Object.setPrototypeOf(
179+
new Float16Array(arrayBuffer), Float16Array.prototype
180+
);
167181
const stealthyFloat32Array =
168182
Object.setPrototypeOf(
169183
new Float32Array(arrayBuffer), Float32Array.prototype
@@ -191,6 +205,7 @@ for (const [ value, _method ] of [
191205
int8Array, fakeInt8Array, stealthyInt8Array,
192206
int16Array, fakeInt16Array, stealthyInt16Array,
193207
int32Array, fakeInt32Array, stealthyInt32Array,
208+
float16Array, fakeFloat16Array, stealthyFloat16Array,
194209
float32Array, fakeFloat32Array, stealthyFloat32Array,
195210
float64Array, fakeFloat64Array, stealthyFloat64Array,
196211
bigInt64Array, fakeBigInt64Array, stealthyBigInt64Array,
@@ -208,6 +223,7 @@ for (const [ value, _method ] of [
208223
int8Array, stealthyInt8Array,
209224
int16Array, stealthyInt16Array,
210225
int32Array, stealthyInt32Array,
226+
float16Array, stealthyFloat16Array,
211227
float32Array, stealthyFloat32Array,
212228
float64Array, stealthyFloat64Array,
213229
bigInt64Array, stealthyBigInt64Array,
@@ -222,6 +238,7 @@ for (const [ value, _method ] of [
222238
int8Array, stealthyInt8Array,
223239
int16Array, stealthyInt16Array,
224240
int32Array, stealthyInt32Array,
241+
float16Array, stealthyFloat16Array,
225242
float32Array, stealthyFloat32Array,
226243
float64Array, stealthyFloat64Array,
227244
bigInt64Array, stealthyBigInt64Array,
@@ -248,6 +265,9 @@ for (const [ value, _method ] of [
248265
isInt32Array: [
249266
int32Array, stealthyInt32Array,
250267
],
268+
isFloat16Array: [
269+
float16Array, stealthyFloat16Array,
270+
],
251271
isFloat32Array: [
252272
float32Array, stealthyFloat32Array,
253273
],

tools/doc/type-parser.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const jsGlobalTypes = [
1919
'TypeError', 'URIError', 'WeakMap', 'WeakSet',
2020

2121
'TypedArray',
22-
'Float32Array', 'Float64Array',
22+
'Float16Array', 'Float32Array', 'Float64Array',
2323
'Int8Array', 'Int16Array', 'Int32Array',
2424
'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array',
2525
];

0 commit comments

Comments
 (0)