Skip to content

feat: support Float16Array views from Node.js 24 - #12

Merged
h4l merged 12 commits into
mainfrom
nodejs-24-float16-buffer
Sep 29, 2025
Merged

feat: support Float16Array views from Node.js 24#12
h4l merged 12 commits into
mainfrom
nodejs-24-float16-buffer

Conversation

@h4l

@h4l h4l commented Sep 29, 2025

Copy link
Copy Markdown
Owner

We already supported Float16Array buffer views for vanilla V8, but Node.js uses its own custom HostObject encoding for buffer views, and Node.js added support in version 24, after we added the initial support.

This PR adds support decoding and encoding Float16Array from Node.js's custom HostObject encoding. Deno 2 also seems to be using this custom encoding.

Most already have it, but not all. We use an isort required-imports
entry instead of flake8-future-annotations lint check because the
flake8-future-annotations rules only add the import when certain
conditions are met; I prefer to always have the same semantics across
modules.
@h4l
h4l force-pushed the nodejs-24-float16-buffer branch from b13106a to 854a4af Compare September 29, 2025 07:39
h4l added 6 commits September 29, 2025 08:19
quarto/quartodoc create it automatically.
It makes more sense to me to evaluate the view tag first and only check
the feature if necessary.
Previously type checking rejected assignment of concrete view types,
like JSUint8Array to their base type JSTypedArray. e.g this was an
error:

    view: JSTypedArray = JSUint8Array(b'')

This also caused problems inferring the type of generic function
arguments, e.g. HostObjectSerializerFn[JSDataView | JSTypedArray],
implemented by NodeJsArrayBufferViewHostObjectHandler. When calling the
serialize_host_object, passing a specific view, like JSUint8Array, mypy
would fail to infer the type, because JSUint8Array is not assignable to
JSTypedArray.

This was happening because the generic type vars of the JS buffer types
(like JSTypedArray[JSArrayBufferT, ViewTagT]) were invariant rather than
covariant. In practice, because all the buffer types are immutable,
these parameters can be covariant.

So now, the buffer types use covariant generic type params, and the
concrete subtypes are assignable to the base types, and inferring the
generic type of arguments marked as the base type works as expected
(e.g. the case mentioned above).
EncodeV8SerializeError and subtypes used by v8serialize.encode are now
defined in v8serialize._errors so that other modules imported by
v8serialize.encode can also import them.
HostObjectDeserializer and HostObjectSerializer are now defined in
v8serialize._values instead of the encode/decode modules themselves.
This allows the v8serialize.extensions module to import them, as it's
also imported by the encode and decode modules.
The NodeJsArrayBufferViewHostObjectHandler now explicitly subclasses
HostObjectSerializerObj and HostObjectDeserializerObj protocols. This
isn't neccissary for correct type checking, but makes it clearer for
humans what the intention is.
@h4l
h4l force-pushed the nodejs-24-float16-buffer branch from 854a4af to f655f4f Compare September 29, 2025 08:21
h4l added 5 commits September 29, 2025 08:28
We already supported Float16Array buffer views for vanilla V8
serialization, but Node.js uses its own custom HostObject encoding for
buffer views, and Node.js added support in version 24, after we added
the initial support.

We now support decoding and encoding Float16Array from Node.js's custom
HostObject encoding. Deno 2 also seems to be using this custom encoding.
@h4l
h4l force-pushed the nodejs-24-float16-buffer branch from f655f4f to acf5dd7 Compare September 29, 2025 08:37
@h4l
h4l merged commit acf5dd7 into main Sep 29, 2025
20 checks passed
@h4l
h4l deleted the nodejs-24-float16-buffer branch September 29, 2025 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant