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: implement wrapping using private properties #18311

Closed

Conversation

Projects
None yet
8 participants
@gabrielschulhof
Copy link
Contributor

commented Jan 23, 2018

Fixes: #14367

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

n-api

v8::String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>(key),
v8::NewStringType::kInternalized).ToLocalChecked());
}

This comment has been minimized.

Copy link
@addaleax

addaleax Jan 28, 2018

Member

env.h has some convenience wrappers for accessing private methods (look for PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES), can we use those?

@@ -815,24 +791,38 @@ static
napi_status Unwrap(napi_env env,
napi_value js_object,
void** result,
v8::Local<v8::Object>* wrapper,
v8::Local<v8::Object>* parent = nullptr) {
bool removeWrap) {

This comment has been minimized.

Copy link
@addaleax

addaleax Jan 28, 2018

Member

We generally use snake_case for local variables and parameters

@gabrielschulhof gabrielschulhof force-pushed the gabrielschulhof:use-private-for-wrap branch 3 times, most recently from 2465f08 to b6d330e Jan 30, 2018

@gabrielschulhof

This comment has been minimized.

Copy link
Contributor Author

commented Jan 30, 2018

@addaleax I have addressed your review comments.

@BridgeAR

This comment has been minimized.

@mhdawson
Copy link
Member

left a comment

LGTM

@@ -46,6 +47,9 @@ struct napi_env__ {
uv_loop_t* loop = nullptr;
};

#define NAPI_PRIVATE_KEY(isolate, prefix) \
(node::Environment::GetCurrent((isolate))->napi_ ## prefix())

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Feb 2, 2018

Member

s/prefix/suffix/?

reinterpret_cast<const uint8_t*>("N-API Environment"),
v8::NewStringType::kInternalized).ToLocalChecked());
auto value = global->GetPrivate(context, key).ToLocalChecked();
auto value = global->GetPrivate(context, NAPI_PRIVATE_KEY(isolate, env))

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Feb 2, 2018

Member

Since you have a reference to the v8::Context, it would be more efficient to call Environment::GetCurrent(context) once and then call env->napi_env() directly.

Applies to a few more spots. I won't point them out individually.

@@ -815,24 +782,37 @@ static
napi_status Unwrap(napi_env env,
napi_value js_object,
void** result,
v8::Local<v8::Object>* wrapper,
v8::Local<v8::Object>* parent = nullptr) {
bool remove_wrap) {

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Feb 2, 2018

Member

Consider using an enum instead of a bool. More self-descriptive at the call site.

} else {
// Create a self-deleting reference.
reference = v8impl::Reference::New(env, obj, 0, true, finalize_cb,
native_object, finalize_cb == nullptr ? nullptr: finalize_hint);

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Feb 2, 2018

Member

Space before :.

wrapper->SetInternalField(2, v8::External::New(isolate, reference));
}
CHECK(obj->SetPrivate(context, NAPI_PRIVATE_KEY(isolate, wrapper),
v8::External::New(isolate, reference)).FromJust());

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Feb 2, 2018

Member

Can you line up the arguments?

@gabrielschulhof gabrielschulhof force-pushed the gabrielschulhof:use-private-for-wrap branch from b6d330e to 92488fe Feb 2, 2018

@gabrielschulhof

This comment has been minimized.

Copy link
Contributor Author

commented Feb 2, 2018

@gabrielschulhof

This comment has been minimized.

Copy link
Contributor Author

commented Feb 3, 2018

I re-ran the node-test-commit-arm portion, and it passed: https://ci.nodejs.org/job/node-test-commit-arm/13589/

@gabrielschulhof

This comment has been minimized.

Copy link
Contributor Author

commented Feb 3, 2018

Landed in 1286923.

gabrielschulhof added a commit that referenced this pull request Feb 3, 2018

n-api: implement wrapping using private properties
PR-URL: #18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: #14367

@gabrielschulhof gabrielschulhof deleted the gabrielschulhof:use-private-for-wrap branch Feb 3, 2018

msoechting added a commit to hpicgs/node that referenced this pull request Feb 5, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

msoechting added a commit to hpicgs/node that referenced this pull request Feb 7, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

MylesBorins added a commit that referenced this pull request Feb 20, 2018

n-api: implement wrapping using private properties
PR-URL: #18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: #14367

MylesBorins added a commit that referenced this pull request Feb 21, 2018

n-api: implement wrapping using private properties
PR-URL: #18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: #14367

MylesBorins added a commit that referenced this pull request Feb 21, 2018

n-api: implement wrapping using private properties
PR-URL: #18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: #14367

@MylesBorins MylesBorins referenced this pull request Feb 21, 2018

Merged

v9.6.0 proposal #18902

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 9, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 9, 2018

n-api: backport changes from master
The following commits (or relevant portions thereof) are included:

* c123467 timers: allow Immediates to be unrefed (the portion fixing
  the uv_test_loop test)
* a555397 n-api: add methods to open/close callback scope
* 47f664e n-api: wrap control flow macro in do/while
* 1286923 n-api: implement wrapping using private properties
* 316b5ef n-api: throw RangeError napi_create_typedarray()
* 8938c4c n-api: expose n-api version in process.versions
* 91c1ccd n-api: throw RangeError in napi_create_dataview() with
  invalid range
* 94e2951 n-api: fix memory leak in napi_async_destroy()
* dc389bf n-api: use nullptr instead of NULL in node_api.cc
* a4a9a3d src: add napi_handle_scope_mismatch to msg list
* 9cf3525 n-api: add helper for addons to get the event loop
* 7be4a84 n-api: unexpose symbols and remove EXTERNAL_NAPI
* b3f9b38 n-api: check against invalid handle scope usage
* 4957726 n-api: make changes for source compatibility

Author: alnyan <qShadowp@gmail.com>
Author: Anatoli Papirovski <apapirovski@mac.com>
Author: Anna Henningsen <anna@addaleax.net>
Author: Ben Noordhuis <info@bnoordhuis.nl>
Author: Daniel Bevenius <daniel.bevenius@gmail.com>
Author: Gabriel Schulhof <gabriel.schulhof@intel.com>
Author: Jinho Bang <zino@chromium.org>
Author: Michael Dawson <michael_dawson@ca.ibm.com>
Author: neta <neta@netush>

n-api: make changes for source compatibility

These changes are necessary in order to retain source compatibility
with older versions of node.

The removal of `module_version` from `napi_module_register()` is
reverted from the flag removal PR, because it should not have been a
part of it.

`CallbackWrapper::NewTarget()` is renamed to
`CallbackWrapper::GetNewTarget()` to distinguish it from V8's native
method, thus allowing for a macro which reduces `NewTarget()` to
`This()` on V8 versions where it was not yet available.

`AsyncResource` is constructed with a C string rather than a V8 string
because the former is available on all versions of node.

PR-URL: nodejs#16102
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>

n-api: check against invalid handle scope usage

Fixes: nodejs#16175
PR-URL: nodejs#16201
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>

n-api: unexpose symbols and remove EXTERNAL_NAPI

* namespaced functions such as v8impl::JsHandleScopeFromV8HandleScope
  become part of Node's public symbols unless they are declared static.
* the class uvimpl::Work needs to be enclosed in an anonymous namespace
  else it, too becomes part of Node's public symbols.
* remove references to EXTERNAL_NAPI.

PR-URL: nodejs#16234
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>

n-api: add helper for addons to get the event loop

Add a utility functions for addons to use when they need
a reference to the current event loop.

While the libuv API is not directly part of N-API, it
provides a quite stable C API as well, and is tightly
integrated with Node itself.

As a particular use case, without access to the event loop
it is hard to do something interesting from inside a N-API
finalizer function, since calls into JS and therefore virtually
all other N-API functions are not allowed.

PR-URL: nodejs#17109
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>

src: add napi_handle_scope_mismatch to msg list

PR-URL: nodejs#17161
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>

n-api: use nullptr instead of NULL in node_api.cc

This commit changes two checks which use NULL to use nullptr.

I'm not very familiar with N-API but wanted to bring this up in case
it was something that was overlooked.

PR-URL: nodejs#17276
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Lance Ball <lball@redhat.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: MichaëZasso <targos@protonmail.com>

n-api: fix memory leak in napi_async_destroy()

PR-URL: nodejs#17714
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>

n-api: throw RangeError in napi_create_dataview() with invalid range

The API is required that `byte_length + byte_offset` is less than or
equal to the size in bytes of the array passed in. If not, a RangeError
exception is raised[1].

[1] https://nodejs.org/api/n-api.html#n_api_napi_create_dataview

PR-URL: nodejs#17869
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>

n-api: expose n-api version in process.versions

Expose n-api version in process.versions so that it is
available for use in javascript by external modules
like node-pre-gyp. It was previously accessible through
a functon available in the N-API.

PR-URL: nodejs#18067
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Tobias Nießn <tniessen@tnie.de>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>

n-api: throw RangeError napi_create_typedarray()

According to the ECMA spec, we should throw a RangeError in the
following cases:
  - `(length * elementSize) + offset` > the size of the array passed in
  - `offset % elementSize` != `0`

In the current implementation, this check was omitted. So, the following
code will cause a crash.
  ```
  napi_create_typedarray(env, napi_uint16_array, 2 /* length */,
                         buffer, 1 /* byte_offset */, &output_array);
  ```

This change fixes the problem and write some related tests.

Refs:
https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length

PR-URL: nodejs#18037
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>

n-api: implement wrapping using private properties

PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

n-api: wrap control flow macro in do/while

Make CHECK_ENV() safe to use in the following context:

    if (condition)
      CHECK_ENV(env);
    else
      something_else();

PR-URL: nodejs#18532
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>

n-api: add methods to open/close callback scope

Add support for the following methods;
  napi_open_callback_scope
  napi_close_callback_scope

These are needed when running asynchronous methods directly
using uv.

PR-URL: nodejs#18089
Fixes: nodejs#15604
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>

n-api: backport change introducing idle handler

Originally part of c123467, this
change adds a single-run idle handler to the N-API uv_loop test to
prevent it from hanging.

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 9, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 15, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367
@MylesBorins

This comment has been minimized.

Copy link
Member

commented Mar 20, 2018

Should this be backported to v8.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

@gabrielschulhof

This comment has been minimized.

Copy link
Contributor Author

commented Mar 20, 2018

@MylesBorins this is part of #19265.

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 27, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 28, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Mar 28, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 4, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 6, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 6, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 10, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 10, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 12, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 15, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 15, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 16, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 16, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

gabrielschulhof added a commit to gabrielschulhof/node that referenced this pull request Apr 16, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367

MylesBorins added a commit that referenced this pull request Apr 16, 2018

n-api: implement wrapping using private properties
Backport-PR-URL: #19447
PR-URL: #18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: #14367

@MylesBorins MylesBorins referenced this pull request Apr 16, 2018

Merged

v6.14.2 proposal #19996

MylesBorins added a commit that referenced this pull request May 1, 2018

n-api: implement wrapping using private properties
Backport-PR-URL: #19265
PR-URL: #18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: #14367

@MylesBorins MylesBorins referenced this pull request May 2, 2018

Merged

v8.11.2 proposal #20478

MayaLekova added a commit to MayaLekova/node that referenced this pull request May 8, 2018

n-api: implement wrapping using private properties
PR-URL: nodejs#18311
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Fixes: nodejs#14367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.