-
Notifications
You must be signed in to change notification settings - Fork 30k
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
WIP V8 API usage in Node.js #26929
WIP V8 API usage in Node.js #26929
Conversation
a908593
to
c51091d
Compare
c51091d
to
7a63b32
Compare
Full build of v8 docs from the source comments and such is available here: https://v8.paulfryzel.com/docs/master/ (yes, you have to accept the invalid cert, paul always forgets to update his certs) i also find https://cs.chromium.org/chromium/src/v8/include/v8.h very helpful, as you can start with a public api and work your way through it thanks to how cs.chromium.org links structures. |
JavaScript code to run in a single instance of V8. The motivation for using | ||
contexts in V8 was so that each window and iframe in a browser can have its | ||
own fresh JavaScript environment. | ||
XXX Node uses one context, mostly, does vm. create new ones? anything else? |
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.
I think that’s about it.
One kind-of-open question is whether we want Node’s own APIs to eventually supports multiple contexts, but that would be a major change to some of our internals…
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.
does vm (or anything else) create new Contexts?
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.
Yes, the vm module does that. It’s currently the only public API for that.
own fresh JavaScript environment. | ||
XXX Node uses one context, mostly, does vm. create new ones? anything else? | ||
|
||
Can get from `isolate->GetCurrentContext()` |
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.
… which is why this should be the same as env->context()
in almost all cases.
Can get from `isolate->GetCurrentContext()` | ||
|
||
|
||
XXX Function vs FunctionTemplate ... wth? |
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.
The distinction isn’t that important to us because we don’t expose many APIs for more than one context (MessagePort
being the only exception I can think of right now) – essentially, a FunctionTemplate
can be used to create functionally identical function instances for multiple contexts.
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.
FunctionTemplates are also useful because they give you an ObjectTemplate (through InstanceTemplate()) that you can use to define internal fields. You can't do that with regular Functions.
- `Local<Object> exports`: where to put exported properties, conventionally | ||
called `target` in node | ||
- `Local<Value> module`: conventionally unused in node | ||
XXX what is this for? addon docs don't mention or use it |
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.
It should be the same value that module
has in CJS scripts – I think this is mostly there so that it matches the addon API more closely? We can remove it if we want, I’d say.
XXX what is this for? addon docs don't mention or use it | ||
- `Local<Context> context`: | ||
- void* priv: not commonly used | ||
XXX where is it ever used? for what? |
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.
I don’t think anybody uses this in practice, neither Node.js itself nor addons. I think the idea was for it to function as sort of a opaque pointer, but that never became useful due to the way that we load addons?
Also, thanks for starting this! |
Can get from `isolate->GetCurrentContext()` | ||
|
||
|
||
XXX Function vs FunctionTemplate ... wth? |
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.
FunctionTemplates are also useful because they give you an ObjectTemplate (through InstanceTemplate()) that you can use to define internal fields. You can't do that with regular Functions.
|
||
Commonly used convenience methods: | ||
- ThrowError/TypeError/RangeError/ErrnoException/... | ||
XXX why are some called Error and others called Exception? |
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.
Legacy :-) ThrowErrnoException() and ThrowUVException() are old, the others are newer. I'd be okay with renaming them if that clears up the confusion / cognitive dissonance.
doc/guides/V8-api-for-node.md
Outdated
(manually managed scope). Constructors (String::New) seem to return Locals. | ||
|
||
`return Local<Array>();` ... seems to do exactly what you are not supposed | ||
to do... whats up? |
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.
It's okay to return empty handles, just not handles that point to something without going through EscapableHandleScope::Escape()
.
@sam-github Btw, I’m still grateful for you doing this, but feel free to let us know if you would like someone to take over this PR :) |
It's very close to the top of my list! And I'm in the process fixing that abort caused by a throwing setter on Object.prototype which you gave me a demo for, which I hope will help me with this. |
I integrated a bunch of the extra information, and that's as far as I got for now. Btw, I don't mind if anybody pushes additional commits directly to this branch. |
@devsnek thanks for the links, are you sure they are up to date? https://v8.paulfryzel.com/docs/master/classv8_1_1_maybe.html doesn't have |
@@ -3,29 +3,50 @@ | |||
v8 docs, not particularly useful: | |||
- https://v8.dev/docs | |||
|
|||
v8 docs, generated from master: | |||
- https://v8.paulfryzel.com/docs/master/ |
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.
I think these are out of date? Also Firefox warns that it's using an expired certificate.
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.
It's documenting V8 6.9 so yeah, pretty stale.
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.
Could we fork this https://github.com/paulfryzel/v8-doxygen, and redeploy it to GH pages or Nodejs.org
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.
Repo: https://github.com/gengjiawen/v8-docs
Docs: https://circleci.com/gh/gengjiawen/v8-docs/6#artifacts/containers/0
You can download the docs artifact here.
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.
Online preview by gitlab: https://gengjiawen.gitlab.io/v8-docs/.
@@ -3,29 +3,50 @@ | |||
v8 docs, not particularly useful: | |||
- https://v8.dev/docs | |||
|
|||
v8 docs, generated from master: | |||
- https://v8.paulfryzel.com/docs/master/ |
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.
It's documenting V8 6.9 so yeah, pretty stale.
the only references to an object are from weak persistent handles. | ||
- A `v8::Global<>` (alias of a `UniquePersistent<>`) handle relies on C++ | ||
constructors and destructors to manage the lifetime of the underlying | ||
object. We don’t use `UniquePersistent` in Node.js. |
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.
I'd drop the notes about UniquePersistent
, that's its old name and will probably go away entirely someday.
- Boolean becomes 1/0 as int, "true"/"false" as strings, etc. | ||
- Numbers become false as Boolean (for any value), -3 casts to String "-3" | ||
- Functions become numerically zero, and "function () { const hello=0; }" as a | ||
String |
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.
Sam, can you remove these three bullet points?
- To<T> will convert values in fairly typical js way: | ||
... never seems to be used by node? | ||
AFAICT, is identical to the As<> route, except for Boolean, which is always | ||
false with As<T>(), but is "expected" with ToT(). |
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.
Can you remove this paragraph? There's no templated To<T>
.
(There's Maybe<T>::To()
but that's different.)
Add a link to |
|
||
- `Local<...>`: A local handle is a pointer to an object. All V8 objects are | ||
accessed using handles, they are necessary because of the way the V8 garbage | ||
collector works. Local handles can only be allocated on the stack, in a |
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.
Extra space before Local
.
AFAICT, is identical to the As<> route, except for Boolean, which is always | ||
false with As<T>(), but is "expected" with ToT(). | ||
|
||
- FunctionCallbackInfo |
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.
Document args.Holder()
?
This aims to help explain some of the internal patterns and utilities that we use. It is by no means exhaustive, and suggestions for additions are welcome. Some of this is based on the existing work from nodejs#26929. Refs: nodejs#26929
This aims to help explain some of the internal patterns and utilities that we use. It is by no means exhaustive, and suggestions for additions are welcome. Some of this is based on the existing work from #26929. Refs: #26929 PR-URL: #30552 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
This aims to help explain some of the internal patterns and utilities that we use. It is by no means exhaustive, and suggestions for additions are welcome. Some of this is based on the existing work from #26929. Refs: #26929 PR-URL: #30552 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
This aims to help explain some of the internal patterns and utilities that we use. It is by no means exhaustive, and suggestions for additions are welcome. Some of this is based on the existing work from #26929. Refs: #26929 PR-URL: #30552 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
This aims to help explain some of the internal patterns and utilities that we use. It is by no means exhaustive, and suggestions for additions are welcome. Some of this is based on the existing work from #26929. Refs: #26929 PR-URL: #30552 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
I've had these notes around for a while, as a result of me spending some time trying to figure out how the V8 API works. Even in this rough draft form, I hope it might lower the barrier to entry for anybody wanting to work on node's C++.
I'll keep working on this as I have time, though I expect it to be slow. I'd be happy to have comments on inaccuracies or XXX in the text, and the "allow edits from maintainers" is ticked. V8 experts, please don't be shy to push fixups if you are so inclined!
V8's API is not very documented, so many (most?) of us just find existing code that does what we want to do and copy it, but #26868 shows this can be problematic, because the expectations for how C++ should be written are evolving, and choosing the wrong existing code as a template can spread code practices that are now discouraged. Hopefully, this guide can point people to the current expectations.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes