Skip to content

Commit

Permalink
jsc/wasm32/wasm32.jsc: remove the fetch code, wrap in async function
Browse files Browse the repository at this point in the history
  • Loading branch information
pipcet committed Feb 3, 2021
1 parent babcf65 commit 66a2d0f
Showing 1 changed file with 31 additions and 68 deletions.
99 changes: 31 additions & 68 deletions jsc/wasm32/wasm32.jsc
Original file line number Diff line number Diff line change
Expand Up @@ -5706,6 +5706,36 @@ var module;
var gRetrigger;
var gExitStatus = 0;

async function run() {
let process = new Wasm32Process(system);
module = await process.modules.module(path);
process.modules.push(module);
module.sizes = sizes;

system.instantiate(module, vm, args, environment_variables()).then(() => {
try {
run = () => {
try {
if (!system.step())
Promise.resolve().then(run);
} catch (e) {
console.log(e);
console.log(e.stack);
}
};
run();
gRetrigger = run;
} catch (e) {
console.log("exception");
console.log(e);
console.log(e.stack);
}
}).catch((e) => {
console.log("caught");
console.log(e);
});
}

if (remote) {
fetch(path).then(response => {
if (response.ok)
Expand Down Expand Up @@ -5747,73 +5777,6 @@ if (remote) {
console.log("e2:" + e + "\n" + e.stack);
});
} else if (typeof os !== "undefined") {
module = new Wasm32Module(new Wasm32Process(system), os.file.readFile(path, "binary"));
module.sizes = sizes;

system.instantiate(module, vm, args, environment_variables()).then(() => {
try {
run = () => {
try {
if (!system.step())
Promise.resolve().then(run);
} catch (e) {
console.log(e);
console.log(e.stack);
}
};
run();
gRetrigger = run;
} catch (e) {
console.log("exception");
console.log(e);
console.log(e.stack);
}
}).catch((e) => {
console.log("caught");
console.log(e);
});
} else if (typeof fetch !== "undefined") {
caches.open("v3").then((cache) => {
var req = new Request(path);

return cache.match(req).then((response) => {
if (response === undefined)
return fetch(path);

return response;
}).then(response => {
cache.put(path, response.clone());

if (response.ok)
return response.arrayBuffer();

return Promise.reject(response);
});
}).then((ab) => {
module = new Wasm32Module(ab);

return system.instantiate(module, vm, args, []);
}).then(() => {
try {
run = () => {
try {
system.step();
Promise.resolve().then(run);
} catch (e) {
console.log(e);
console.log(e.stack);
}
};
run();
} catch (e) {
console.log("exception");
console.log(e);
console.log(e.stack);
}
}).catch((e) => {
console.log("caught");
console.log(e);
console.log(e.stack);
});
run();
}

0 comments on commit 66a2d0f

Please sign in to comment.