Skip to content

doc: update V8 fast API guidance #58999

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Renegade334
Copy link
Contributor

Draft, to garner feedback.

The existing guidance dates from the original V8 implementation, as included in Node.js before v23. The "new" Fast API is quite different, particularly regarding error handling and stack access. This document therefore needs a significant refresh.

There are one or two places where changes to the API necessitates a convention to be defined for the Node.js codebase; I've flagged these for discussion.

cc: @anonrig, @targos (as authors of this document)
cc: @nodejs/v8

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/tsc

@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Jul 8, 2025
Comment on lines +72 to +80
<!--
Deliberately omitted:
* `void *` (let's not go down this road...)
* `v8::Local<v8::Object>` (V8 advises that this should only be used for
receiver arguments, but it's more consistent to use
v8::Value handles throughout)
-->
Copy link
Contributor Author

@Renegade334 Renegade334 Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • void * is interpreted by V8 as a pointer to an embedder type. This doesn't feel like something to entertain, but there may be differing opinions.
  • V8 suggests a v8::Object handle to be used as the type for the receiver argument, and a v8::Value handle for other arguments. This isn't enforced, and sticking with v8::Value throughout is probably the better option, both for consistency and because the receiver for most fast callbacks is going to be undefined.

or `undefined`), you will need to ensure that the first argument is discarded:

```cpp
bool FastIsObject(v8::Local<v8::Value>, // receiver, discarded
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the convention be for a named or unnamed discard parameter here? Both are currently used within the codebase.

target,
"func",
SlowFunc,
fast_func_callbacks);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the array directly (corresponding to the array signature of the v8::MemorySpan constructor) seems like the more straightforward recommendation. The only example in the codebase at present uses the iterator signature:

node/src/node_url.cc

Lines 525 to 526 in 0992d28

SetFastMethodNoSideEffect(
isolate, target, "canParse", CanParse, {fast_can_parse_methods_, 2});

Comment on lines +260 to +269
If a fast callback creates any `v8::Local` handles within the fast callback,
then it must first initialize a new `v8::HandleScope` to ensure that the
handles are correctly scoped and garbage-collected.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't have any equivalent in the previous guidance, since stack access is a new feature. This seems like the correct advice – I'm fairly sure that CI has flagged exceptions from V8 relating to this specifically – but others will be more informed than me!

@Renegade334 Renegade334 force-pushed the doc-fast-api branch 2 times, most recently from 78795ed to 43884b2 Compare July 8, 2025 23:53
* `uint64_t`
* `float`
* `double`
* `v8::Local<v8::Value>` (analogous to `any`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V8::Object is also accepted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +328 to +335
As above, initializing a `v8::HandleScope` is mandatory before any operations
which create local handles.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with #58999 (comment), I'm fairly sure that invoking the ERROR macros (which build the error within a local object handle) necessitates a HandleScope here, but someone else may have a definitive answer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants