Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Oct 16, 2018

Because instances of napi_env are created on a per-global-object
basis and because 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.
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. node-api Issues and PRs related to the Node-API. labels Oct 16, 2018
@addaleax
Copy link
Member Author

addaleax commented Oct 16, 2018

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM

@addaleax
Copy link
Member Author

addaleax commented Oct 19, 2018

Resume CI again: https://ci.nodejs.org/job/node-test-pull-request/17991/ (:heavy_check_mark:)

@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 20, 2018
@refack
Copy link
Contributor

refack commented Oct 20, 2018

  • Implement a better (although not perfect) way of cleaning
    up napi_env instances.

Any reason not to simply use a shared_ptr in?

napi_env env;

@@ -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;
Copy link
Contributor

@refack refack Oct 20, 2018

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 :)

Copy link
Member Author

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.
@mmarchini
Copy link
Contributor

@addaleax
Copy link
Member Author

Landed in 449c0ca

@addaleax addaleax closed this Oct 24, 2018
@addaleax addaleax deleted the napi-context branch October 24, 2018 18:00
addaleax added a commit that referenced this pull request Oct 24, 2018
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>
targos pushed a commit that referenced this pull request Oct 26, 2018
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>
targos pushed a commit that referenced this pull request Nov 1, 2018
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>
MylesBorins pushed a commit that referenced this pull request Nov 26, 2018
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>
@MylesBorins
Copy link
Contributor

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.

@codebytere codebytere mentioned this pull request Nov 27, 2018
rvagg pushed a commit that referenced this pull request Nov 28, 2018
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>
MylesBorins pushed a commit that referenced this pull request Nov 29, 2018
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>
@codebytere codebytere mentioned this pull request Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants