-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
node-api: add support for Float16Array #58879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3d5bf56
4bff794
5bdffbb
9559ac9
5ff4dd4
dc28ba5
cf84ea1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,10 @@ typedef enum { | |
napi_float64_array, | ||
napi_bigint64_array, | ||
napi_biguint64_array, | ||
#ifdef NAPI_EXPERIMENTAL | ||
#define NODE_API_EXPERIMENTAL_HAS_FLOAT16_ARRAY | ||
napi_float16_array, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
#endif // NAPI_EXPERIMENTAL | ||
} napi_typedarray_type; | ||
|
||
typedef enum { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3165,6 +3165,13 @@ 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: | ||
vmoroz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (env->module_api_version != NAPI_VERSION_EXPERIMENTAL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit in particular is odd – why the check here? Even if this feature is experimental, the enum value being used at all should be enough to imply that the caller has opted into this feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, it may look silly, but if we want to ensure predictable behavior between Node-API versions, then we must do it. Say, someone creates a plugin that relies on the new Float16Array. Having the predictable behavior in this code file is also important for unit tests. Without versioning here, we cannot create reliable tests that can check the differences between versions. E.g. previously the unit test was successful even if the code relied on the experimental feature. Now it fails as expected because the addon must not use it yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It depends on a very specific question though, not stars; namely, whether the runtime has support for creating Float16Array instances through Node-API. The behavior is predictable; either In any case, do what you must. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! We just follow the established process here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel strongly about adding a runtime version guard. IMO, adding an We added runtime version checks for features like finalizers, which can cause side effects. But for |
||
return napi_set_last_error(env, napi_invalid_arg); | ||
} | ||
CREATE_TYPED_ARRAY( | ||
env, Float16Array, 2, buffer, byte_offset, length, typedArray); | ||
break; | ||
default: | ||
return napi_set_last_error(env, napi_invalid_arg); | ||
} | ||
|
@@ -3203,6 +3210,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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise here. Wrap the else if here in an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I support @jasnell point here. Since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though I see the issue that previously we did not return any deterministic value at all. |
||
*type = napi_float16_array; | ||
} else if (value->IsFloat32Array()) { | ||
*type = napi_float32_array; | ||
} else if (value->IsFloat64Array()) { | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the unit test is failing because the test
|
||||
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); | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed this in the 11 July Node-API meeting.
In the past, we have added two enum values to
napi_status
that were not behind experimental guards:napi_no_external_buffers_allowed
(ref) andnapi_cannot_run_js
(ref).I'm not sure this should be guarded by an
NAPI_EXPERIMENTAL
flag.Adding this under an experimental flag would mean that it is possible for
napi_get_typedarray_info
(which has no guard) can return an enum that I (if I do not defineNAPI_EXPERIMENTAL
) have no way of checking against.Does it make sense to have this under a guard?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KevinEady , I believe for the correct versioning support we must use the experimental flag.
While we did not provide the experimental guard for the status values, we always used to do it for other enum types.
E.g. see the property attributes:
node/src/js_native_api_types.h
Lines 73 to 80 in a472745
It is important that Node-API keeps a good versioning story and the ABI stability.
For the same reason the
napi_get_typedarray_info
behavior must be properly guarded.E.g. today before this PR, the
napi_get_typedarray_info
has certain behavior and returns values that does not include the Float16 array type. We must ensure that this behavior is kept unchanged unless developers vote to use the newer API version where the behavior is changed.