-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add math-related intrinsics/functions for JsValue
s
#2629
Conversation
Thanks! Overall though I'd prefer to keep the set of intrinsics and such to a minimum unless there's a good reason to expand things. Could you elaborate a bit more on the motivation for this? It seems, for example, that some of the conversions (e.g. |
@alexcrichton JS operators generally don't have corresponding methods, so |
c48a68f
to
7a8dbf5
Compare
@@ -102,9 +105,75 @@ intrinsics! { | |||
#[symbol = "__wbindgen_is_string"] | |||
#[signature = fn(ref_externref()) -> Boolean] | |||
IsString, | |||
#[symbol = "__wbindgen_is_bigint"] | |||
#[signature = fn(ref_externref()) -> Boolean] |
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 special-case this? __wbindgen_typeof can be used to implement this.
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.
@crimsoncodes0 The other types are already special-cased, so I special-cased this one too for consistency
Edit: also, maybe JS engines might have special optimizations for typeof
to avoid a full string comparison? (No idea, but presumably they could do this)
#[signature = fn(ref_externref()) -> Externref] | ||
Neg, | ||
#[symbol = "__wbindgen_bit_and"] | ||
#[signature = fn(ref_externref(), ref_externref()) -> Externref] |
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.
These bitwise operators always return an i32, besides unsigned_shr, which yields u32.
They're currently typed as yielding Externref.
But, on another note, these are already native Rust operators, why use the significantly slower, more unsound JS operators? Is there anything that can't be done without these?
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.
@crimsoncodes0 These operators only return 32-bit ints when passed JS number
s. When passed bigint
s they return bigint
s. The only exception is >>>
(unsigned_shr
), which does not accept bigint
s; I included it for completeness but if it's deemed unnecessary I can remove it
I've removed the |
8fee37c
to
02d2b8c
Compare
Can you elaborate more on the motivation for this? This is a lot of library support with a lot of traits and a lot of intrinsics. It's a good deal to maintain in what should ideally be a pretty lean core library. |
The motivation for this is to allow performing mathematical operations on JS The only two intrinsics this PR adds that aren't useful for |
98d4a2d
to
f72e74b
Compare
I've been working on this, but I've run into a roadblock: the |
Ah ok, if possible then I think it would be best to bind For example for the impl BigInt {
pub fn new(arg: &JsValue) -> BigInt {
// manually call the `BigInt` name bound as a function
}
} where it's not necessarily pure |
09fae07
to
ac4bb37
Compare
Ready for review |
7b8f8f5
to
14fec9b
Compare
@Jules-Bertholet Are you still working to get this PR merged? I have a need for a straightforward xref: #1087 |
1e9dadc
to
7185873
Compare
@trevyn This PR should be ready for review |
Thanks! ping @alexcrichton |
This |
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.
Sorry for the delay. I'm not overly in love with how this is such a large increase in the API surface area, but I dunno I don't really have anything against it. I would prefer to avoid the num-traits
dep but other than that seems fine I guess?
Cargo.toml
Outdated
@@ -40,6 +40,7 @@ wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.76" } | |||
serde = { version = "1.0", optional = true } | |||
serde_json = { version = "1.0", optional = true } | |||
cfg-if = "1.0.0" | |||
num-traits = { version = "0.2.14", optional = true } |
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.
This is a small enough addition could this be a method instead of a trait implementation?
@alexcrichton The |
Makes sense, yeah, but I'd prefer to leave it out and keep wasm-bindgen at least somewhat slim in deps. |
crates/js-sys/Cargo.toml
Outdated
[dependencies] | ||
wasm-bindgen = { path = "../..", version = "0.2.76" } | ||
num-bigint = { version = "0.4.1", optional = true } | ||
num-traits = { version = "0.2.14", optional = true } |
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.
Sorry but personally I do not want to add new public dependencies to these crates. There is no story for landing breaking changes to js-sys at this time and having new public dependencies increases the risk that a breaking change may be needed (in case these deps have a major-version-bump)
I realize these are optional but it's still part of the public API and something that I'm not willing to commit to.
Using BigInt as the macro parameters leads to absurd signatures like slices of BigInt objects (can't exist). Instead, just use the numeric types. bindgen already converts them to bigint when appropriate. Closes: rustwasm#3009 Fixes: d6d056c ("Add math-related intrinsics/functions for `JsValue`s (rustwasm#2629)")
Using BigInt as the macro parameters leads to absurd signatures like slices of BigInt objects (can't exist). Instead, just use the numeric types. bindgen already converts them to bigint when appropriate. Closes: rustwasm#3009 Fixes: d6d056c ("Add math-related intrinsics/functions for `JsValue`s (rustwasm#2629)")
Adds handling for
BigInt
, among other things.