-
Notifications
You must be signed in to change notification settings - Fork 182
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
Proposal for bindgen changes #102
Conversation
Can you provide a motivation section? IIUC they are abstraction layers built on top of |
Done, PTAL. I tried to split up the proposed changes into sections. |
Hm, you argumentation looks reasonable. I don't have a strong opinion here, so I will leave decision to @dhardy. Note: if this change will get merged, in # deprecated feature, use `bindgen` instead
wasm-bindgen = ["bindgen"]
binidgen = ["getrandom/bindgen"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clear motivation section @josephlr! Sounds like a good rationale to me, though perhaps it's worth inviting @alexcrichton / @fitzgen to comment.
stdweb = { version = "0.4.18", optional = true } | ||
|
||
[features] | ||
std = [] | ||
# enables dummy implementation for unsupported targets | ||
dummy = [] | ||
bindgen = ["wasm-bindgen", "js-sys", "web-sys"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not rename the package, like I did here? This removes what is otherwise a breaking change to the API (probably resulting in a compile error if someone tries using the wasm-bindgen
feature).
To add: might also be worth adding a comment against the optional crates which users should not use as features.
pub(crate) const STDWEB_RNG_FAILED: Error = internal_error!(10); | ||
pub(crate) const BINDGEN_WEB_CRYPTO: Error = internal_error!(7); | ||
pub(crate) const BINDGEN_WEB_FAILED: Error = internal_error!(8); | ||
pub(crate) const BINDGEN_NODE_FAILED: Error = internal_error!(9); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better not to re-use error codes (except where equivalent)? We aren't short on them, and doing so could cause confusion.
*source = Some(getrandom_init()?); | ||
static IS_NODE: LazyBool = LazyBool::new(); | ||
if IS_NODE.unsync_init(|| node_crypto().is_some()) { | ||
if node_crypto().unwrap().random_fill_sync(dest).is_err() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why .unwrap()
and not .ok_or(..)?
?
This seems fine to me either way personally. Writing |
BTW I think we should not rename the feature. I think the first association with |
Closing this for now. Thanks everyone for the feedback. My idea for solving #4 conflicts with this, so we should resolve those issues before dealing with this one.
That's a fair point, sticking with the name |
This is my idea for improving the
wasm32-unknown-unknown
withbindgen
target.Note: This is a Work in Progress and contains many ideas for improving the bindgen target. We don't necessarily have to do all of them.
Changes and Motivation
Using
js-sys
andweb-sys
These crates are maintained by the same group that maintains
wasm-bindgen
and are intended to work similar to how thelibc
crate works today. Strictly speaking, thelibc
crate is never necessary. You could always just declareextern "C"
functions yourself; however, having thelibc
crate ensures that these bindings are correct for your platform.Similarly, you could always declare interfaces to the JS APIs and Browser APIs manually via
wasm-bindgen
. However, using these binding crates makes our job easier:libc
) due to the complex mapping between Rust and JS types. It's better to just have someone else deal with them and have them fixbindgen
bugs.Furthermore, the
web-sys
crate is configurable, so you only need to build what you need to use (we only needCrypto
,Window
, andWorkerGlobalScope
).I think that if we're OK depending on crates like
libc
that are maintained by the Rust team and just declare platform bindings, we should be OK with using a similar crate for wasm.Better error handling with
catch
bindingsThis change also moves to using
catch
bindings (the default injs-sys
andweb-sys
) for all of our bindings. This allows us to properly return an error when certain APIs fail (or fail to exist) at runtime.This also means we no longer need things like
get_random_values_fn
. If the API is not there, the function will just return an error (undefined
I think). This change does not depend on usingweb-sys
/js-sys
.Removing
std::thread_local
Right now, the
stdweb
implementation only checks if we are in Node vs a Browser in itsinit
function. This does the same forbindgen
. Given that the cost of retrieving these objects is mostly just thebindgen
overhead (which we incur regardless), it doesn't make a whole lot of sense to hold onto athread_local
NodeCrypto
. We can just get the object each time we need it.Future changes
We could additionally add
error!
calls to actually print out the underlying JS errors.We could also try to reduce the duplication between the
stdweb
andbindgen
code, but that's better for a future PR.