Skip to content

Commit 900d329

Browse files
authored
node-api: add support for Float16Array
PR-URL: #58879 Reviewed-By: Vladimir Morozov <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 8716146 commit 900d329

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

doc/api/n-api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,13 @@ object such that no properties can be set on it, and no prototype.
22862286

22872287
#### `napi_typedarray_type`
22882288

2289+
<!-- YAML
2290+
changes:
2291+
- version: REPLACEME
2292+
pr-url: https://github.com/nodejs/node/pull/58879
2293+
description: Added `napi_float16_array` for Float16Array support.
2294+
-->
2295+
22892296
```c
22902297
typedef enum {
22912298
napi_int8_array,
@@ -2299,6 +2306,7 @@ typedef enum {
22992306
napi_float64_array,
23002307
napi_bigint64_array,
23012308
napi_biguint64_array,
2309+
napi_float16_array,
23022310
} napi_typedarray_type;
23032311
```
23042312

src/js_native_api_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ typedef enum {
133133
napi_float64_array,
134134
napi_bigint64_array,
135135
napi_biguint64_array,
136+
#define NODE_API_HAS_FLOAT16_ARRAY
137+
napi_float16_array,
136138
} napi_typedarray_type;
137139

138140
typedef enum {

src/js_native_api_v8.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,10 @@ napi_status NAPI_CDECL napi_create_typedarray(napi_env env,
32593259
CREATE_TYPED_ARRAY(
32603260
env, BigUint64Array, 8, buffer, byte_offset, length, typedArray);
32613261
break;
3262+
case napi_float16_array:
3263+
CREATE_TYPED_ARRAY(
3264+
env, Float16Array, 2, buffer, byte_offset, length, typedArray);
3265+
break;
32623266
default:
32633267
return napi_set_last_error(env, napi_invalid_arg);
32643268
}
@@ -3297,6 +3301,8 @@ napi_status NAPI_CDECL napi_get_typedarray_info(napi_env env,
32973301
*type = napi_int32_array;
32983302
} else if (value->IsUint32Array()) {
32993303
*type = napi_uint32_array;
3304+
} else if (value->IsFloat16Array()) {
3305+
*type = napi_float16_array;
33003306
} else if (value->IsFloat32Array()) {
33013307
*type = napi_float32_array;
33023308
} else if (value->IsFloat64Array()) {

test/js-native-api/test_typedarray/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ assert.strictEqual(externalResult[2], 2);
4141
// Validate creation of all kinds of TypedArrays
4242
const buffer = new ArrayBuffer(128);
4343
const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
44-
Uint16Array, Int32Array, Uint32Array, Float32Array,
45-
Float64Array, BigInt64Array, BigUint64Array ];
44+
Uint16Array, Int32Array, Uint32Array, Float16Array,
45+
Float32Array, Float64Array, BigInt64Array, BigUint64Array ];
4646

4747
arrayTypes.forEach((currentType) => {
4848
const template = Reflect.construct(currentType, buffer);
@@ -64,7 +64,7 @@ arrayTypes.forEach((currentType) => {
6464
});
6565

6666
const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array,
67-
Float32Array, Float64Array,
67+
Float16Array, Float32Array, Float64Array,
6868
BigInt64Array, BigUint64Array ];
6969
nonByteArrayTypes.forEach((currentType) => {
7070
const template = Reflect.construct(currentType, buffer);

test/js-native-api/test_typedarray/test_typedarray.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ static napi_value Multiply(napi_env env, napi_callback_info info) {
7575
return output_array;
7676
}
7777

78-
static void FinalizeCallback(napi_env env,
78+
static void FinalizeCallback(node_api_basic_env env,
7979
void* finalize_data,
80-
void* finalize_hint)
81-
{
80+
void* finalize_hint) {
8281
free(finalize_data);
8382
}
8483

0 commit comments

Comments
 (0)