Repository navigation
feat(windows-napi): take Node-API from the modules NAPI_HOST_MODULES names - #46
Draft
NathanWalker wants to merge 1 commit into
Draft
NathanWalker wants to merge 1 commit into
NathanWalker wants to merge 1 commit into
Conversation
…names
An embedder whose executable doesn't export Node-API can now run the
addon: it lists the modules that do in NAPI_HOST_MODULES (';'-separated,
in priority order), and napi-sys takes each napi_* from the first one
that exports it.
React Native for Windows needs this. @nativescript/react-native runs the
addon in a Hermes runtime made through hermes.dll's jsr_* API: hermes.dll
has the engine half of Node-API (js_native_api.h) and the React Native
module implements Node's half (threadsafe functions, cleanup hooks,
buffers), so it sets NAPI_HOST_MODULES=<its DLL>;<hermes.dll>.
The workspace now patches napi-sys with the vendored copy, as
windows-hermes already did. Without NAPI_HOST_MODULES nothing changes:
symbols come from this module if it exports them, else the process.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
windows-napi can now run in a host whose executable doesn't export Node-API. The embedder lists the modules that do in
NAPI_HOST_MODULES,;-separated and in priority order, and napi-sys takes eachnapi_*from the first module that exports it.Why
React Native for Windows needs it (NativeScript/runtimes#112).
@nativescript/react-nativeruns the addon in a Hermes runtime made throughhermes.dll'sjsr_*API:hermes.dllhas the engine half of Node-API (js_native_api.h, 115 functions), but none of Node's (node_api.h). napi-rs callsnapi_create_threadsafe_functionandnapi_add_env_cleanup_hookduring registration (create_custom_gc), so the addon panicked with "Node-API symbol has not been loaded".NAPI_HOST_MODULES=<its DLL>;<hermes.dll>.Change
packages/vendor/napi-sys(load_all): withNAPI_HOST_MODULESset, load each listed module, and load the symbol groups from them in reverse order. A symbol a library lacks keeps its earlier value, so each one ends up from the first library that has it. Without the variable, nothing changes.napi-syswith the vendored copy ([patch.crates-io]), aswindows-hermesalready did, sowindows-napigets it.windows-napi/README.md: "Embedding outside Node".Evidence
Built with
cargo build -p windows-napi --release. The resultingwindows_napi.dllran in a react-native-windows 0.83.2 app through@nativescript/react-native's Windows module: sync WinRT, awaited async operations, console and errors all work (details in the runtimes PR).Found along the way (not fixed here)
NativeType::try_from("Guid")returnsPointer(runtime/src/value.rs). A string argument then can't be parsed as a pointer, so the callee gets a null or zero GUID:BitmapEncoder.CreateAsync(id, stream)fails withWINCODEC_ERR_COMPONENTNOTFOUND(0x88982F50) for any form of the id. On return, a 16-byte GUID is written into an 8-byte pointer slot and then read as an object pointer, which crashes (BitmapEncoder.JpegEncoderId,BitmapCodecInformation.CodecId). The classic engine special-casesGuid(guid_ptr_to_js_object,parse_guid_str); the Node-API engine needs the same for parameters (napi_parse_arg) and return slots (method_call.rs/property_call.rssize it as a pointer).XamlWindowon a thread has closed, new ones stay 0×0 (XamlRoot.IsHostVisiblefalse). A hidden, never-closed island works around it.