-
Notifications
You must be signed in to change notification settings - Fork 645
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
[DRAFT] Add Node-API to Hermes #1377
base: main
Are you sure you want to change the base?
Conversation
Thanks for doing this @vmoroz!!! |
@tmikov has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Thanks so much, @vmoroz! Really excited to play with this. |
Huge effort @vmoroz thank you ❤️ |
Hey @vmoroz, thanks for putting this together! Two high level requests:
|
@neildhar , thank you for the feedback and suggestions! |
Is this the main place to follow work on this topic? Is there still some appetite to support NAPI in React Native? |
I'm still very interested in it and have even submitted conference talk proposals on the topic so would love it to become a reality! I have some native Node addons that I've written for Electron, and it's a shame that I can use them in React Native Windows (as it supports Node-API) yet can't use them in any other flavour of React Native currently. Let me know if there's any way I could help! |
One of the Discord RN channels or a discussion topic in one of the Meta's or Microsoft's related repo could be a better place to discuss all the details, but we can start it here. The goal of supporting Node-API for Hermes, V8, and RN was to provide the industry standard ABI-safe API. It should enable versioning support for modules, support for multiple programming languages, and reuse of native code written for Node.js. I wonder what is your scenario in terms of tergeting platforms (Windows, Mac, Android, etc.) and if you own the code that uses Node-API? It may be not possible to reuse binary Node.js modules, but reusing the majority of the code and recompiling it for the RN is a more achievable scenario. |
We are implementing WebGPU for React Native (
https://x.com/appjsconf/status/1806332497783058900) and right now we are
virtually migrating a webgpu node module to JSI manually. So of course the
thought or being able to use that node module directly is appealing to us.
…On Thu 11 Jul 2024 at 18:09, Vladimir Morozov ***@***.***> wrote:
Is this the main place to follow work on this topic? Is there still some
appetite to support NAPI in React Native? We are eyeing at some node
modules that we would love to use out of the box in React Native.
One of the Discord RN channels or a discussion topic in one of the Meta's
or Microsoft's related repo could be a better place to discuss all the
details, but we can start it here.
The goal of supporting Node-API for Hermes, V8, and RN was to provide the
industry standard ABI-safe API. It should enable versioning support for
modules, support for multiple programming languages, and reuse of native
code written for Node.js.
I wonder what is your scenario in terms of tergeting platforms (Windows,
Mac, Android, etc.) and if you own the code that uses Node-API? It may be
not possible to reuse binary Node.js modules, but reusing the majority of
the code and recompiling it for the RN is a more achievable scenario.
—
Reply to this email directly, view it on GitHub
<#1377 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACKXVUXFX6NCBLYT5AIRILZL2U2FAVCNFSM6AAAAABGMIZABGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRTGMZTKMBYGM>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
This is awesome! In the video clip you say that you target first of all the mobile platforms: iOS and Android. The current version of the Node-API for Hermes is in the hermes-windows repo: https://github.com/microsoft/hermes-windows/ . There we are starting to factor out the Node-API code into its own folder. Yesterday we had a long informative face-to-face meeting with Hermes team. |
@wcandillon when you say that you are implementing WebGPU, do you literally mean the WebGPU spec, 1 to 1, with shader language, etc? |
We use Google Dawn for the native WebGPU implementation (Dawn is also a supported Skia backend), we code generate most of the bindings from its TS definition (which is itself generated from the W3C spec, we also match TS methods to C++ methods, Dawn provides a json model for that), and for the manual part of the work we just look at the node module and migrate it. This is why being able to use that node binding directly (which is already conformant to the official WebGPU testsuite) feels appealing. |
May I emphatically object to using Discord channels, as they inherently make discussions exclusionary, unfriendly to different timezones, transient, difficult to search, and impossible to reference. |
For anyone who wants to try out this PR with react-native use version |
I was able to link and run a react-native app with this hermes NodeAPI PR. However after calling
raw is a hermes/include/hermes/ADT/CompactArray.h Lines 85 to 87 in 175d85f
hermes/include/hermes/ADT/CompactArray.h Lines 53 to 57 in 175d85f
hermes/include/hermes/ADT/CompactArray.h Lines 131 to 133 in 175d85f
hermes/lib/VM/IdentifierTable.cpp Line 331 in 175d85f
so what I'm currently trying to figure out is: why is raw a null pointer and what is the source of this issue Does anybody else tried to use the NAPI in an app and if yes were you successful or did you encounter errors? |
After building hermes in debug mode I get a different error, however without any stacktrace:
and after retrying a few times a different error:
Does anyone know how I can get the stacktrace of these errors to find the cause?? |
With @shirakaba making noise at React Summit US on Friday, I rediscovered my excitement around the prospective future of having access to the Node-API symbols for my current and future React Native native modules. I help maintain the Realm database, supporting RN and Node.js. Landing this PR would mean we could 1) maintain only one binding between our native code and a runtime API across the platforms we support and 2) unlock publishing this binding code as a prebuild. Ultimately leading to much improved developer experience for us and our users! This PR is crucial to making this future a reality and I would love to help move this forward:
What would you think would be most impactful? |
A quick update on my side: I believe I'm making good progress on getting the Realm's unit tests running with the Node-API binding in a React Native app, with Hermes from this PRs branch. I had to apply a few workarounds, but I believe most were to address limitations in the running React Native app. Our native module is using the C++ wrapper, more specifically the Napi::Addon abstraction to define itself.
I don't see these listed as TODOs in hermes_napi.cpp hermes/API/hermes/hermes_napi.cpp Lines 56 to 66 in 5d46568
|
I've come to realize the PR provides implementations for the "js_native_api.h" part of Node-API only, leaving these 30 functions undefined:
These are all declared in the top-level node_api.h. My guess is that this is because these functions aren't needed in the use-case of React Native for Windows, is that correct? |
@kraenhansen I know very little about Node-API, but my guess is that some (all?) of the missing APIs are NodeJS-specific. For example, |
@tmikov You're right that the implementation of some of these are trivial, as they should likely simply error. None the less, they're all a part of the specification and need some implementation. Without it, an addon relying on these features won't be able to perform runtime-checks for the availability of those features and will instead experience build errors. |
@kraenhansen I am sorry for being unclear. I did not say that these APIs are simple. I said that some of these APIs are specific to Node.js and so they cannot be part of Hermes.
The purpose of this PR is not to create an emulation of NodeJS. Instead the goal is to implement the engine-specific parts of the API. |
For reference, For The |
@Brooooooklyn Hermes is only the JS engine, functionally equivalent to Hermes is also not trying to be a complete implementation of Node-API like People can build runtimes by using Hermes as one component of the runtime, and can implement the full Node API on top of it, if they want to. I admit, that would be very cool, actually. But in any case, Hermes itself has a very narrow function - to execute JS. The PR aims to expose the JS engine-specific parts of Node-API. It is not meant to emulate NodeJS for the purpose of running existing native extensions. Again, such a thing can be built on top of Hermes by third parties. |
Re-reading my answer, I seem to be the one being unclear 🙈 I was trying to hint that for Hermes most of these could be implemented as no-ops which would simply return an error status. But thinking more about this however: That would take away the opportunity for some other component to provide an actual implementation for those symbols 🤔 I think what confused me the most was, that the description of the PR stated that it was implementing Node-API for Hermes, while in-fact there were a large part of the spec that wasn't actually being implemented - this seemed like an oversight to me. That being said, while I understand hermes might not be the place to implement this, I'd expect most consumers of this to be trying to rebuild existing Node native modules for Hermes / React Native and I'd expect those to use the C++ node-addon-api headers, which would fail to link. Someone in the community could probably implement a native module for React Native, to provide (at least a dummy implementation of) the rest of the symbols. |
Summary
This is an initial implementation of Node-API for Hermes.
The code is taken from the hermes-windows repo.
Node-API is an ABI-safe that is originally implemented for Node.js addons, and then adopted by all major JS runtimes.
This is a draft PR. Before removing the draft status I would like to ask a few questons:
Test Plan
Unit tests for Node-API are to be added.