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

Don't force TextDe/Encoder #3329

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,26 +1434,35 @@ impl<'a> Context<'a> {
if !self.should_write_global("text_encoder") {
return Ok(());
}
self.expose_text_processor("TextEncoder", "('utf-8')")
self.expose_text_processor("TextEncoder", "('utf-8')", None)
}

fn expose_text_decoder(&mut self) -> Result<(), Error> {
if !self.should_write_global("text_decoder") {
return Ok(());
}

// `ignoreBOM` is needed so that the BOM will be preserved when sending a string from Rust to JS
// `fatal` is needed to catch any weird encoding bugs when sending a string from Rust to JS
self.expose_text_processor("TextDecoder", "('utf-8', { ignoreBOM: true, fatal: true })")?;

// This is needed to workaround a bug in Safari
// See: https://github.com/rustwasm/wasm-bindgen/issues/1825
self.global("cachedTextDecoder.decode();");
let init = Some("cachedTextDecoder.decode();");

// `ignoreBOM` is needed so that the BOM will be preserved when sending a string from Rust to JS
// `fatal` is needed to catch any weird encoding bugs when sending a string from Rust to JS
self.expose_text_processor(
"TextDecoder",
"('utf-8', { ignoreBOM: true, fatal: true })",
init,
)?;

Ok(())
}

fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> {
fn expose_text_processor(
&mut self,
s: &str,
args: &str,
init: Option<&str>,
) -> Result<(), Error> {
match &self.config.mode {
OutputMode::Node { .. } => {
let name = self.import_name(&JsImport {
Expand Down Expand Up @@ -1481,10 +1490,26 @@ impl<'a> Context<'a> {
| OutputMode::Web
| OutputMode::NoModules { .. }
| OutputMode::Bundler { browser_only: true } => {
self.global(&format!("const cached{0} = new {0}{1};", s, args))
self.global(&format!("const cached{0} = (typeof {0} !== 'undefined' ? new {0}{1} : {{ decode: () => {{ throw Error('{0} not available') }} }} );", s, args))
}
};

if let Some(init) = init {
match &self.config.mode {
OutputMode::Node { .. }
| OutputMode::Bundler {
browser_only: false,
} => self.global(init),
OutputMode::Deno
| OutputMode::Web
| OutputMode::NoModules { .. }
| OutputMode::Bundler { browser_only: true } => self.global(&format!(
"if (typeof {} !== 'undefined') {{ {} }};",
s, init
)),
}
}

Ok(())
}

Expand Down
7 changes: 2 additions & 5 deletions examples/wasm-audio-worklet/src/dependent_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ extern "C" {
}

pub fn on_the_fly(code: &str) -> Result<String, JsValue> {
// Generate the import of the bindgen ES module, assuming `--target web`,
// preluded by the TextEncoder/TextDecoder polyfill needed inside worklets.
// Generate the import of the bindgen ES module, assuming `--target web`.
let header = format!(
"import '{}';\n\
import init, * as bindgen from '{}';\n\n",
wasm_bindgen::link_to!(module = "/src/polyfill.js"),
"import init, * as bindgen from '{}';\n\n",
IMPORT_META.url(),
);

Expand Down
23 changes: 0 additions & 23 deletions examples/wasm-audio-worklet/src/polyfill.js

This file was deleted.