Repository: nodejs/node
Pull request: node-api: add support for Float16Array

This PR aims to add support for Float16Array in n-api, addressing this [issue](https://github.com/nodejs/node/issues/58873) 
cc: @nodejs/node-api

Changed files:
filename: doc/api/n-api.md
patch: @@ -2286,6 +2286,13 @@ object such that no properties can be set on it, and no prototype.
 
 #### `napi_typedarray_type`
 
+<!-- YAML
+changes:
+  - version: REPLACEME
+    pr-url: https://github.com/nodejs/node/pull/58879
+    description: Added `napi_float16_array` for Float16Array support.
+-->
+
 ```c
 typedef enum {
   napi_int8_array,
@@ -2299,6 +2306,7 @@ typedef enum {
   napi_float64_array,
   napi_bigint64_array,
   napi_biguint64_array,
+  napi_float16_array,
 } napi_typedarray_type;
 ```
 

filename: src/js_native_api_types.h
patch: @@ -133,6 +133,8 @@ typedef enum {
   napi_float64_array,
   napi_bigint64_array,
   napi_biguint64_array,
+#define NODE_API_HAS_FLOAT16_ARRAY
+  napi_float16_array,
 } napi_typedarray_type;
 
 typedef enum {

filename: src/js_native_api_v8.cc
patch: @@ -3259,6 +3259,10 @@ napi_status NAPI_CDECL napi_create_typedarray(napi_env env,
       CREATE_TYPED_ARRAY(
           env, BigUint64Array, 8, buffer, byte_offset, length, typedArray);
       break;
+    case napi_float16_array:
+      CREATE_TYPED_ARRAY(
+          env, Float16Array, 2, buffer, byte_offset, length, typedArray);
+      break;
     default:
       return napi_set_last_error(env, napi_invalid_arg);
   }
@@ -3297,6 +3301,8 @@ napi_status NAPI_CDECL napi_get_typedarray_info(napi_env env,
       *type = napi_int32_array;
     } else if (value->IsUint32Array()) {
       *type = napi_uint32_array;
+    } else if (value->IsFloat16Array()) {
+      *type = napi_float16_array;
     } else if (value->IsFloat32Array()) {
       *type = napi_float32_array;
     } else if (value->IsFloat64Array()) {

filename: test/js-native-api/test_typedarray/test.js
patch: @@ -41,8 +41,8 @@ assert.strictEqual(externalResult[2], 2);
 // Validate creation of all kinds of TypedArrays
 const buffer = new ArrayBuffer(128);
 const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
-                     Uint16Array, Int32Array, Uint32Array, Float32Array,
-                     Float64Array, BigInt64Array, BigUint64Array ];
+                     Uint16Array, Int32Array, Uint32Array, Float16Array,
+                     Float32Array, Float64Array, BigInt64Array, BigUint64Array ];
 
 arrayTypes.forEach((currentType) => {
   const template = Reflect.construct(currentType, buffer);
@@ -64,7 +64,7 @@ arrayTypes.forEach((currentType) => {
 });
 
 const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array,
-                            Float32Array, Float64Array,
+                            Float16Array, Float32Array, Float64Array,
                             BigInt64Array, BigUint64Array ];
 nonByteArrayTypes.forEach((currentType) => {
   const template = Reflect.construct(currentType, buffer);

filename: test/js-native-api/test_typedarray/test_typedarray.c
patch: @@ -75,10 +75,9 @@ static napi_value Multiply(napi_env env, napi_callback_info info) {
   return output_array;
 }
 
-static void FinalizeCallback(napi_env env,
+static void FinalizeCallback(node_api_basic_env env,
                              void* finalize_data,
-                             void* finalize_hint)
-{
+                             void* finalize_hint) {
   free(finalize_data);
 }