feat(neon-macros): Add neon::main
proc attr macro
#636
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
Adds a
#[neon::main]
proc macro.tl;dr
Use an attribute to initialize either a
legacy
ornapi
neon module. Thefn
doesn't even need to bepub
!Migration
Thought Process
neon-macros
Procedural macros must be in their own crate. A new crate,
neon-macros
, is created. The crate has a single feature flag,napi
for toggling betweenlegacy
andnapi
implementations.Proc macros must be at the root of a crate. To keep the structure clean, actual implementations are in
napi.rs
andlegacy.rs
modules that are conditionally imported tolib.rs
. Thelib.rs
has thin glue code for delegating to the correct implementation.Re-export
Proc macros are re-exported from
neon
to keep users from needing to specify another crate.Feature Flags
Proc macros can have a negative impact on compile time. A new feature flag,
proc-macros
is added to enable the feature. It is default disabled since most legacy users will continue to use the normal macros.It is planned for proc macros to be the recommended way to use N-API. Therefore, the
napi-runtime
feature flag automatically enables theproc-macros
feature flag.Open Questions
Should we keep the panic hook? I think it should be removed. In my opinion, it's more harmful than helpful; especially since it is global.The hook is a holdover from earlier implementations and is no longer necessary. It will be removed.Naming. I likeConsensus onneon::init
because it initializes and can be called multiple times (once per context). However,neon::main
is another option.neon::main
.Should we try to protect against usingThis can be a follow-up.#[neon::init]
multiple times? I think we shouldn't because it would be complicated and brittle for limited value.