Skip to content

Commit

Permalink
Try to fix #2296 and support to get wasm file through http protocol (#…
Browse files Browse the repository at this point in the history
…2297)

* Update mod.rs

* Update mod.rs

* Support http protocol to obtain wasm file remotely

* Another stupid mistake...

* Fix errors caused by `{}`

* `bg` is missing

* RealPathSync cannot be used to get the parent directory of file under linux, so substring and lastIndexOf will be used to get the parent directory under linux

* Missing readFileSync

* Use asynchronous functions instead of synchronous functions.

* Change `includes` to switch-case. And change the synchronous file operation to asynchronous.

* Change to @RReverser suggestion.

* throw a proper Error instance

Co-authored-by: Ingvar Stepanyan <[email protected]>

* Reuse url instance

Co-authored-by: Ingvar Stepanyan <[email protected]>

* Use URL instead of previous backward writing.

* reuse unnecessary code.

Co-authored-by: Ingvar Stepanyan <[email protected]>
  • Loading branch information
juzi5201314 and RReverser authored Jun 18, 2021
1 parent 41a6a43 commit 80da105
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,28 @@ impl<'a> Context<'a> {
// Deno removed support for .wasm imports in https://github.com/denoland/deno/pull/5135
// the issue for bringing it back is https://github.com/denoland/deno/issues/5609.
format!(
"const file = new URL(import.meta.url).pathname;
const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\\\' : '/') + 1) + '{}_bg.wasm';
const wasmModule = new WebAssembly.Module(Deno.readFileSync(wasmFile));
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
"const wasm_url = new URL('{module_name}_bg.wasm', import.meta.url);
let wasmCode = '';
switch (wasm_url.protocol) {{
case 'file:':
let wasm_pathname = wasm_url.pathname;
if (Deno.build.os === 'windows' && wasm_pathname.startsWith('/')) {{
wasm_pathname = wasm_pathname.substr(1);
}}
wasmCode = await Deno.readFile(wasm_pathname);
break
case 'https:':
case 'http:':
wasmCode = await (await fetch(wasm_url)).arrayBuffer();
break
default:
throw new Error(`Unsupported protocol: ${{wasm_url.protocol}}`);
break
}}
const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance;
const wasm = wasmInstance.exports;",
module_name
module_name = module_name
)
}

Expand Down

0 comments on commit 80da105

Please sign in to comment.