Merge pull request #759 from alexcrichton/shared-memory-strings

Fix `getStringFromWasm` for shared memory
This commit is contained in:
Alex Crichton 2018-08-28 12:16:52 -07:00 committed by GitHub
commit 447b42b7ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1179,13 +1179,30 @@ impl<'a> Context<'a> {
} }
self.expose_text_decoder(); self.expose_text_decoder();
self.expose_uint8_memory(); self.expose_uint8_memory();
self.global(
" // Typically we try to give a raw view of memory out to `TextDecoder` to
function getStringFromWasm(ptr, len) { // avoid copying too much data. If, however, a `SharedArrayBuffer` is
return cachedDecoder.decode(getUint8Memory().subarray(ptr, ptr + len)); // being used it looks like that is rejected by `TextDecoder` or
} // otherwise doesn't work with it. When we detect a shared situation we
", // use `slice` which creates a new array instead of `subarray` which
); // creates just a view. That way in shared mode we copy more data but in
// non-shared mode there's no need to copy the data except for the
// string itself.
self.memory(); // set self.memory_init
let is_shared = self.module
.memory_section()
.map(|s| s.entries()[0].limits().shared())
.unwrap_or(match &self.memory_init {
Some(limits) => limits.shared(),
None => false,
});
let method = if is_shared { "slice" } else { "subarray" };
self.global(&format!("
function getStringFromWasm(ptr, len) {{
return cachedDecoder.decode(getUint8Memory().{}(ptr, ptr + len));
}}
", method));
} }
fn expose_get_array_js_value_from_wasm(&mut self) { fn expose_get_array_js_value_from_wasm(&mut self) {