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: support for object freeze/seal #35359

Closed
wants to merge 2 commits into from

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Sep 26, 2020

This is PR adds support for Object.freeze() & Object.seal() to n-api.

cc @gabrielschulhof (not sure who else is best to ping here!)

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
Copy link
Collaborator

nodejs-github-bot commented Sep 26, 2020

Review requested:

  • @nodejs/n-api

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Sep 26, 2020
@codebytere codebytere force-pushed the napi-seal-freeze branch 2 times, most recently from d45a97d to b63a5f1 Compare September 26, 2020 20:09
@codebytere codebytere marked this pull request as ready for review September 30, 2020 16:44
@mhdawson
Copy link
Member

Will need more time to review the content but the first thing is that all new APIs need to be added as experimental so they should be added in the section guarded by #ifdef NAPI_EXPERIMENTAL in js_native_api.h

@mhdawson
Copy link
Member

@codebytere , one other question is if isFrozen and isSealed should go along with the freeze and seal or if there is some reason the freeze and seal are needed by native modules by the is methods won't be?

@mhdawson
Copy link
Member

mhdawson commented Sep 30, 2020

@codebytere some of the requirements for adding an API - https://github.com/nodejs/node/blob/master/doc/guides/adding-new-napi-api.md.

Of that list A new API addition should be simultaneously implemented in at least one other VM implementation of Node.js. and The API must be implemented in a Node.js implementation with an alternate VM. have not been enforced since Chakra core is no longer an option for implementation and we should probably update that.

@codebytere
Copy link
Member Author

codebytere commented Sep 30, 2020

@mhdawson i can definitely add isFrozen/isSealed - there's no specific reason i didn't!

@mhdawson
Copy link
Member

@codebytere thanks for the quick response. I'll tag for discussion in the N-API team meeting this friday to see if there is any other feedback/concerns as well.

One other thing that would be good for that discussion is the use case that driving the addition. I'm guessing you need them and understanding that need will help the discussion.

@mhdawson
Copy link
Member

You are also welcome to come to the meeting - from the calendar:

Node.js N-API team weekly meeting
Friday, October 2⋅11:00am – 12:00pm
Weekly on Friday
https://zoom.us/j/363665824

src/js_native_api_v8.cc Outdated Show resolved Hide resolved
@codebytere
Copy link
Member Author

codebytere commented Oct 2, 2020

@mhdawson i should be there! what time zone is that? Also - the reason I initially didn't add isFrozen and/or isSealed i recall now is bc V8 doesn't really provide a good approach to that. They check this themselves by compiling some JS and running which doesn't necessarily feel like an approach we'd want to emulate.

A primary driver for this is passing objects across process boundaries (e.g between renderer & main) with more robust security guarantees.

@jschlight
Copy link
Contributor

jschlight commented Oct 2, 2020 via email

@codebytere
Copy link
Member Author

@jschlight ty, bit of a suffering hour but i should make it 😆

@codebytere codebytere force-pushed the napi-seal-freeze branch 3 times, most recently from 5a505af to 705356e Compare October 2, 2020 15:31
@mhdawson mhdawson added the node-api Issues and PRs related to the Node-API. label Oct 2, 2020
@gabrielschulhof
Copy link
Contributor

gabrielschulhof commented Oct 2, 2020

@codebytere is there a reason why these APIs take a third parameter that just receives the second parameter back? Is there ever any chance that the object returned will be an object other than the object passed in?

If not, then I don't think we need the out parameter. I understand that it can be useful in JS to return the same object that was passed in so one can compose calls, but on the native side we can never chain N-API calls because they all return napi_status. Our C++ wrappers can implement this composably even without the out parameter.

As an existing example, napi_detach_arraybuffer also modifies the napi_value containing an array buffer without returning it. Since the detach API implements directly an abstract operation as defined in the spec, and since both freeze and seal look like convenience methods around the SetIntegrityLevel abstract operation, I think it's OK to drop the out parameter, because then we're basically implementing the abstract operation, but without the need to introduce a corresponding enum of { napi_frozen, napi_sealed }.

Edit: linkified some of the text.

@gabrielschulhof
Copy link
Contributor

gabrielschulhof commented Oct 2, 2020

Also, fewer parameters help keep the stack lean for low-memory implementations like IoT.js.

@codebytere
Copy link
Member Author

@gabrielschulhof sounds good to me! There wasn't a specific reason I did it this way to begin with beyond that I was seeking to emulate conventions for similar functions

@codebytere
Copy link
Member Author

@gabrielschulhof i think that should do it - let me know if i missed anything!

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.

LGTM after the issue of retrieving and freezing/sealing the object passed from JS is addressed.

doc/api/n-api.md Outdated Show resolved Hide resolved
test/js-native-api/test_object/test.js Show resolved Hide resolved
test/js-native-api/test_object/test_object.c Outdated Show resolved Hide resolved
@codebytere
Copy link
Member Author

@gabrielschulhof sorry for the churn 🙃

turns out trying to context switch extremely fast between this and doing an LTS release is not the greatest idea in the world - linting should finally be good now.

@gabrielschulhof
Copy link
Contributor

@codebytere NP 🙂

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

@mhdawson mhdawson added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2020
@nodejs-github-bot
Copy link
Collaborator

@codebytere codebytere added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 7, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 7, 2020
@nodejs-github-bot
Copy link
Collaborator

@codebytere
Copy link
Member Author

Landed in 19f1451

@codebytere codebytere closed this Oct 7, 2020
@codebytere codebytere deleted the napi-seal-freeze branch October 7, 2020 15:32
codebytere added a commit that referenced this pull request Oct 7, 2020
PR-URL: #35359
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
BethGriggs pushed a commit that referenced this pull request Oct 13, 2020
PR-URL: #35359
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Oct 14, 2020
MylesBorins pushed a commit that referenced this pull request Nov 3, 2020
PR-URL: #35359
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Nov 3, 2020
MylesBorins pushed a commit that referenced this pull request Nov 16, 2020
PR-URL: #35359
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
joesepi pushed a commit to joesepi/node that referenced this pull request Jan 8, 2021
PR-URL: nodejs#35359
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants