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

NIF Codegen rework #264

Merged
merged 2 commits into from
Nov 8, 2019
Merged

NIF Codegen rework #264

merged 2 commits into from
Nov 8, 2019

Conversation

scrogson
Copy link
Member

@scrogson scrogson commented Oct 23, 2019

I've got the new macros all working! 🎉

I'm pretty excited about this, but I'm pretty sure there are some opportunities for some clean up.

One thing I've noticed is that if you specify any term which is has decode called on it, that function needs to return a Result<T, E>. It would be nice to avoid this if possible.

Let me know what you think.

@scrogson scrogson force-pushed the rustler_macros branch 4 times, most recently from a20cdd6 to 9396c0b Compare October 26, 2019 05:50
@scrogson scrogson marked this pull request as ready for review October 26, 2019 06:19
rustler_codegen/src/init.rs Outdated Show resolved Hide resolved
rustler_tests/native/rustler_test/src/test_atom.rs Outdated Show resolved Hide resolved
@scrogson scrogson requested review from jorendorff and a team October 26, 2019 06:31
rustler/src/env.rs Outdated Show resolved Hide resolved
rustler/src/export.rs Outdated Show resolved Hide resolved
@evnu
Copy link
Member

evnu commented Oct 27, 2019

One thing I've noticed is that if you specify any term which is has decode called on it, that function needs to return a Result<T, E>.

I am not sure if I understand this correctly, but I think my comment here might cover this.

rustler_codegen/src/init.rs Outdated Show resolved Hide resolved
@evnu
Copy link
Member

evnu commented Oct 27, 2019

@scrogson I really like this PR, great work! :)

@scrogson
Copy link
Member Author

I really like this PR, great work! :)

@evnu, thank you. This has been on my mind for quite a long time. It feels great to finally get it working. Thanks for your review!

@scrogson scrogson requested a review from hansihe October 29, 2019 20:45
@scrogson scrogson force-pushed the rustler_macros branch 2 times, most recently from d49aa9d to 529f505 Compare October 31, 2019 03:30
@scrogson
Copy link
Member Author

When looking into the failures with the rustler_mix_test I realized that the init! macro doesn't work without specifying the on_load function as either None or Some(fun).

error: expected expression, found `,`
 --> src/lib.rs:6:1
  |
6 | rustler::init!("Elixir.RustlerMixTest", [add]);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected expression

Running cargo expand on the code reveals this output for the nif_load body:

extern "C" fn nif_load(
    env: rustler::codegen_runtime::NIF_ENV,
    _priv_data: *mut *mut rustler::codegen_runtime::c_void,
    load_info: rustler::codegen_runtime::NIF_TERM,
) -> rustler::codegen_runtime::c_int {
    unsafe {
        (/*ERROR*/)
    }
}

@evnu do you have any ideas how to fix that?

@evnu
Copy link
Member

evnu commented Oct 31, 2019

@evnu do you have any ideas how to fix that?

On mobile, so only a first question: originally, a NIf entry was of the form(erlang Name, rust function, optional flags), and right now we are missing the option to pass in the flags, right?

@scrogson
Copy link
Member Author

@evnu the way I wrote the parser, I thought this would be covered (but apparently it isn't):

https://github.com/rusterlium/rustler/pull/264/files#diff-8daf9e0986d966ebc2c4878e8a772b5fR20-R25

@filmor
Copy link
Member

filmor commented Oct 31, 2019

I'll have a look at the code tomorrow, but one more suggestion: Now that this is an actual proc_macro, I think we should support a syntax like this instead:

rustler::init!("mod", entries, on_load = something)

This opens up the possibility of adding more parameters in the future (e.g. I'd really like to be able to allow a reload.

@scrogson
Copy link
Member Author

This opens up the possibility of adding more parameters in the future (e.g. I'd really like to be able to allow a reload.

Yeah, I like that. I'll work on that tonight.

rustler_codegen/src/init.rs Outdated Show resolved Hide resolved
@evnu
Copy link
Member

evnu commented Nov 2, 2019

Running cargo expand on the code reveals this output for the nif_load body:

See https://github.com/rusterlium/rustler/pull/264/files#r341824704

We need to encode None as a TokenStream. Otherwise, it seems that expanding it results in the whole call to handle_nif_init_call() being dropped. I don't know why this happens (some property of quote!{} maybe?), but quoting None manually works.

@evnu
Copy link
Member

evnu commented Nov 2, 2019

@scrogson see evnu@1794b33 for an idea on how to fix the problem with on_load. See evnu@5e22350 for a proposal to fix the template.

Signed-off-by: Sonny Scroggin <[email protected]>
@filmor
Copy link
Member

filmor commented Nov 8, 2019

@evnu Can you have a last look? In my opinion this is good to go.

Tremendous work @scrogson, this makes the interface so much simpler :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants