-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Passing a String to JS removes BOM (FEFF) #1729
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
Comments
This is expected. Rust strings are UTF-8, but JavaScript strings are UTF-16, so it has to transcode the string. This transcoding doesn't necessarily round-trip (e.g. it strips out BOM, and also replaces unpaired surrogates with the replacement character). This isn't caused by Rust or wasm-bindgen, instead it's just how JavaScript itself behaves. Here's a JavaScript program to demonstrate: var input = "\u{feff}bar";
var output = new TextDecoder("utf8").decode(new TextEncoder("utf8").encode(input))
console.log(input[0]);
console.log(output[0]); As you can see, it stripped out the BOM. |
After looking into it some more, there is an var input = "\u{feff}bar";
var output = new TextDecoder("utf8", { ignoreBOM: true }).decode(new TextEncoder("utf8").encode(input))
console.log(input[0]);
console.log(output[0]); So we could change wasm-bindgen so it does that. But I'm curious: what's your use case? (We should probably also set |
I think setting IMO it would be nice to guarantee that Rust -> JS -> Rust is the identity, and JS -> Rust -> JS replaces unpaired surrogates with �, but nothing else. Concerning my "use case": I just pass (user-supplied) strings from Rust to JS and back again, and I was very confused that this changes some strings - this is just a minimal repro. |
Summary
Consider this function:
But in JS, I only get the string
"bar"
- the BOM is stripped. Is this intended? I can't seem to find info about this neither here nor here.Additional Details
I use
wasm-bindgen = "0.2.50"
andrustc 1.39.0-nightly (bea0372a1 2019-08-20)
.The text was updated successfully, but these errors were encountered: