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: add napi_get_own_property_names #28944

Closed
wants to merge 3 commits into from
Closed

Conversation

himself65
Copy link
Member

@himself65 himself65 commented Aug 2, 2019

fix #28942, and this pr should after #30006

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Aug 2, 2019
@addaleax addaleax added node-api Issues and PRs related to the Node-API. semver-minor PRs that contain new features and should be released in the next minor version. labels Aug 2, 2019
@addaleax
Copy link
Member

addaleax commented Aug 2, 2019

@nodejs/n-api

I feel like this approach kind of leads to questions like “what if we also want symbols/inherited properties/no indices/…”, and we might want to come up with something that has flexibility which is similar to the V8 API?

@himself65 himself65 force-pushed the 28942 branch 3 times, most recently from e5c0692 to 2e62342 Compare August 2, 2019 18:24
src/js_native_api.h Outdated Show resolved Hide resolved
@mhdawson
Copy link
Member

+1 to making it more flexible as long as the options supported are part of the standard and will be supported by all engines.

@Trott
Copy link
Member

Trott commented Aug 25, 2019

@addaleax @mhdawson @nodejs/n-api Are we likely to land this? If not, I'd like to close it. If we're likely to land it, I'd like to nudge it forward. /ping @himself65! Or maybe reviewers can perhaps push fixup changes to the branch to get it where it needs to go and expedite things a bit?

@addaleax
Copy link
Member

@Trott Yes, but @mhdawson’s comment need to be addressed and we do need somebody (e.g. @himself65) to figure out in what way we expose the other option flags, if we do so.

@himself65
Copy link
Member Author

I will push fixed changes later :)

doc/api/n-api.md Outdated Show resolved Hide resolved
doc/api/n-api.md Outdated Show resolved Hide resolved
src/js_native_api_v8.cc Outdated Show resolved Hide resolved
Copy link
Contributor

@gabrielschulhof gabrielschulhof left a comment

Choose a reason for hiding this comment

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

The PR is good, but we really need to return napi_pending_exception whenever there is the possibility that an exception has occurred on the JS side.

src/js_native_api_v8.cc Outdated Show resolved Hide resolved
src/js_native_api_v8.h Outdated Show resolved Hide resolved
@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

Trott commented Oct 16, 2019

@mhdawson @addaleax LGTY now?

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

So, code-wise this LGTM, but I still feel like the API should expose flags similar to the ones we get from V8, otherwise this just begs for Yet Another Function That Does The Same Thing.

doc/api/n-api.md Outdated Show resolved Hide resolved
@gabrielschulhof
Copy link
Contributor

@himself65 can you please check whether other engines, such as JerryScript, have the facilities to retrieve property names as selectively as V8? That is, could the kind of flexibility that V8 provides (skipping/not skipping symbols, rendering as string/leaving as number) be implemented

  • on top of other engines (preferably), or
  • into other engines,

if needed?

@addaleax
Copy link
Member

@gabrielschulhof From a look at JerryScript’s API docs, the API only exposes a way to get enumerable properties. But I wouldn’t care too much for that here – any spec-compatible JS implementation needs to have a way to provide all properties, because it could always be implemented in terms of JS builtin functions.

@devsnek
Copy link
Member

devsnek commented Oct 16, 2019

the js spec has operations that ask for a filtered subset of own properties, so I think it's reasonable that we can provide something similar here. Even if a napi wrapper has to do the work instead of the engine.

@gabrielschulhof
Copy link
Contributor

@devsnek @addaleax sounds like we can safely expose the full flexibility provided by V8 because on other engines this same flexibility can be implemented by either filtering after then engine's native API returns, or calling a spec-compliant JS API if the native API is incapable of returning unfiltered results.

Similarly, I guess we can use type coercion in post-processing to address the flexibility in the second parameter.

@himself65 would you mind exposing the two parameters provided by V8?

I suppose we'll have to add a napi_get_all_property_names()(name eminently negotiable 😀) to correspond to napi_get_property_names().

Honestly, I'm not sure whether we shouldn't just go with

typedef enum {
  napi_key_own,
  napi_key_all
} napi_key_collection_mode;

typedef enum {
  napi_key_strings = 1,
  napi_key_symbols = 1 << 1
} napi_key_types;

typedef enum {
  napi_keep_numbers,
  napi_numbers_to_strings
} napi_key_coercion;

napi_status napi_get_all_property_names(napi_env env,
                                        napi_value object,
                                        napi_key_collection_mode key_mode,
                                        napi_key_types key_types,
                                        napi_key_coercion key_coercion,
                                        napi_value* result);

and basically expose v8::Object::GetPropertyNames() pretty much as-is, given that the resulting N-API can be implemented on any spec-compliant engine, if only by calling into JS.

We can then re-implement napi_get_property_names() as a call to this new function.

@legendecas
Copy link
Member

Would filter ONLY_ENUMERABLE be reasonable to be supported in this API? Since napi_get_property_names() only expects enumerable property names, if napi_get_property_names() would be re-implemented as a call to this new function, it has to be supported too.

@gabrielschulhof
Copy link
Contributor

@legendecas right, we need one more enum for one more flag to the method.

@himself65
Copy link
Member Author

I will open a new pull request about that later 🤔

@fhinkel
Copy link
Member

fhinkel commented Nov 1, 2019

ping @himself65

@himself65
Copy link
Member Author

Would filter ONLY_ENUMERABLE be reasonable to be supported in this API? Since napi_get_property_names() only expects enumerable property names, if napi_get_property_names() would be re-implemented as a call to this new function, it has to be supported too.

How to implement that? 🤔

@legendecas
Copy link
Member

napi_key_enumerable in #30006 could be used to get enumerable keys on the object. BTW, should this PR be succeeded by #30006? Since #30006 covers every part of this API.

@himself65
Copy link
Member Author

@legendecas Fixed and this pr should after #30006 for now

@gabrielschulhof
Copy link
Contributor

IINM this PR is superseded by #30006, so I will close it. Please re-open if I am mistaken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. node-api Issues and PRs related to the Node-API. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

n-api: add method to get own property names
9 participants