Skip to content

Commit

Permalink
Lazily initialize memory views (#3253)
Browse files Browse the repository at this point in the history
* Lazily initialize memory views

This stops load-time errors in versions of Safari before 15 if the `BigInt64Array` or `BigUint64Array` memory views are included but never used.

Pointed out in #2716 (comment).

* Update reference tests
  • Loading branch information
Liamolucko authored Jan 24, 2023
1 parent 979b335 commit a4788ca
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
10 changes: 4 additions & 6 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,10 @@ impl<'a> Context<'a> {
for kind in views {
writeln!(
init_memviews,
// Reset the memory views to empty in case `init` gets called multiple times.
// Reset the memory views to null in case `init` gets called multiple times.
// Without this, the `length = 0` check would never detect that the view was
// outdated.
"cached{kind}Memory{num} = new {kind}Array();",
"cached{kind}Memory{num} = null;",
kind = kind,
num = num,
)
Expand Down Expand Up @@ -1782,14 +1782,12 @@ impl<'a> Context<'a> {
format!("{cache}.byteLength === 0", cache = cache)
};

// Initialize the cache to an empty array, which will trigger the resized check
// on the first call and initialise the view.
self.global(&format!("let {cache} = new {kind}Array();\n"));
self.global(&format!("let {cache} = null;\n"));

self.global(&format!(
"
function {name}() {{
if ({resized_check}) {{
if ({cache} === null || {resized_check}) {{
{cache} = new {kind}Array(wasm.{mem}.buffer);
}}
return {cache};
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = new Uint8Array();
let cachedUint8Memory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
Expand All @@ -38,10 +38,10 @@ function handleError(f, args) {
}
}

let cachedInt32Memory0 = new Int32Array();
let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/tests/reference/import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ function handleError(f, args) {
}
}

let cachedInt32Memory0 = new Int32Array();
let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/tests/reference/result-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ function addHeapObject(obj) {
return idx;
}

let cachedInt32Memory0 = new Int32Array();
let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
Expand All @@ -48,10 +48,10 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = new Uint8Array();
let cachedUint8Memory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = new Uint8Array();
let cachedUint8Memory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
Expand Down

0 comments on commit a4788ca

Please sign in to comment.