-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
n-api: make per-Context-ness of napi_env explicit #23689
Conversation
CI: https://ci.nodejs.org/job/node-test-pull-request/17894/ /cc @nodejs/n-api |
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.
LGTM
Resume CI again: https://ci.nodejs.org/job/node-test-pull-request/17991/ (:heavy_check_mark:) |
Any reason not to simply use a Line 1156 in b3ae915
|
@@ -935,18 +957,21 @@ class ThreadSafeFunction : public node::AsyncResource { | |||
// These methods must only be called from the loop thread. | |||
|
|||
napi_status Init() { | |||
uv_loop_t* loop = nullptr; |
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.
If this is going to crash on any error, why not break the abstraction and directly call env->node_env()->event_loop();
(I'd go as far as const uv_loop_t& loop = *(env->node_env()->event_loop());
, but I know I'm pushing 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.
If this is going to crash on any error, why not break the abstraction and directly call
env->node_env()->event_loop();
It’s not going to crash, which is why there’s a CHECK
:) But since we have an N-API function for exactly this purpose, I think it makes sense to use it here.
(I'd go as far as
const uv_loop_t& loop = *(env->node_env()->event_loop());
, but I know I'm pushing it :)
We can’t use a const uv_loop_t&
:)
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances.
9024ab2
to
2f3c078
Compare
Landed in 449c0ca |
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances. PR-URL: #23689 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances. PR-URL: #23689 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances. PR-URL: #23689 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances. PR-URL: #23689 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This lands cleanly on 10.x, but there is a backlog of unlanded n-api related commits on 8.x that require a manual backport... likely part of a larger backporting effort. |
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances. PR-URL: #23689 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Because instances of `napi_env` are created on a per-global-object basis and because since most N-API functions refer to builtin JS objects, `napi_env` is essentially in 1:1 correspondence with `v8::Context`. This was not clear from the implementation by itself, but has emerged from conversations with the N-API team. This patch changes the `napi_env` implementation to: - Actually store the `v8::Context` it represents. - Provide more direct access to the `node::Environment` to which the `Context` belongs. - Do not store the `uv_loop_t*` explicitly, since it can be inferred from the `node::Environment` and we actually have an N-API method for that. - Replace calls to `isolate->GetCurrentContext()` with the more appropriate `napi_env` `Context`. - Implement a better (although not perfect) way of cleaning up `napi_env` instances. PR-URL: #23689 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Because instances of
napi_env
are created on a per-global-objectbasis and because most N-API functions refer to builtin JS
objects,
napi_env
is essentially in 1:1 correspondence withv8::Context
.This was not clear from the implementation by itself, but has
emerged from conversations with the N-API team.
This patch changes the
napi_env
implementation to:v8::Context
it represents.node::Environment
to which the
Context
belongs.uv_loop_t*
explicitly, since it can beinferred from the
node::Environment
and we actuallyhave an N-API method for that.
isolate->GetCurrentContext()
withthe more appropriate
napi_env
Context
.up
napi_env
instances.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes