From 233525d7d673251d38ff854c78a5469fe88d32d8 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 28 Feb 2019 14:57:55 -0800 Subject: [PATCH] Fix `passStringToWasm` with shared memory Looks like `TextEncoder#encodeInto` isn't compatible when the buffer passed in is backed by a `SharedArrayBuffer`, so if the module has a shared thread skip the `encodeInto` optimization entirely. --- crates/cli-support/src/js/mod.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 01ce8fff..b81cc609 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -1211,21 +1211,20 @@ impl<'a> Context<'a> { debug ); + // Looks like `encodeInto` doesn't currently work when the memory passed + // in is backed by a `SharedArrayBuffer`, so force usage of `encode` if + // a `SharedArrayBuffer` is in use. + let shared = self.module.memories.get(self.memory).shared; + match self.config.encode_into { - EncodeInto::Never => { - self.global(&format!( - "function passStringToWasm(arg) {{ {} }}", - use_encode, - )); - } - EncodeInto::Always => { + EncodeInto::Always if !shared => { self.require_internal_export("__wbindgen_realloc")?; self.global(&format!( "function passStringToWasm(arg) {{ {} }}", use_encode_into, )); } - EncodeInto::Test => { + EncodeInto::Test if !shared => { self.require_internal_export("__wbindgen_realloc")?; self.global(&format!( " @@ -1240,6 +1239,12 @@ impl<'a> Context<'a> { use_encode, )); } + _ => { + self.global(&format!( + "function passStringToWasm(arg) {{ {} }}", + use_encode, + )); + } } Ok(()) }