-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to fix #2296 and support to get wasm file through http protocol #2297
Conversation
Thanks! Looks like the deno CI is failing though? |
Yes, I made a stupid mistake. Now it has been fixed and passed ci. |
… linux, so substring and lastIndexOf will be used to get the parent directory under linux
In deno, the usual method is to get the library directly from remote. |
cc @jakobhellermann mind taking a look at this? |
crates/cli-support/src/js/mod.rs
Outdated
console.error(`Unsupported protocol: ${{url.protocol}}`) | ||
Deno.exit(0) | ||
}} | ||
const wasmModule = new WebAssembly.Module(wasmCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should almost never use synchronous constructors, as they can be very slow for large Wasm. Instead, use await WebAssembly.instantiate(wasmCode, imports)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ this issue still stands. (I'm not a maintainer, but I don't think we should ever block)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok! f71515d
Can this get some movement? This issue makes it almost impossible to publish a Deno library that uses wasm-bindgen without modifying the generated output. |
I'll admit I don't fully understand the original issue. Given that Deno supports all the required Web APIs, doesn't |
It almost works with the web target. Deno's Also calling the load function directly is inconvenient. |
Oh I see, that's unfortunate. |
Glad someone saw this problem. Is there any resistance to this problem at the moment? |
It'd be great to get some movement on this PR, this issue is a blocking one for me. |
crates/cli-support/src/js/mod.rs
Outdated
"const url = new URL(import.meta.url) | ||
|
||
let wasmCode = '' | ||
if (url.protocol.includes('file')) {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: better to check protocols explicitly instead of "includes". Maybe change this to a switch-case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crates/cli-support/src/js/mod.rs
Outdated
@@ -341,8 +341,7 @@ impl<'a> Context<'a> { | |||
console.error(`Unsupported protocol: ${{url.protocol}}`) | |||
Deno.exit(0) | |||
}} | |||
const wasmModule = new WebAssembly.Module(wasmCode); | |||
const wasmInstance = new WebAssembly.Instance(wasmModule, imports); | |||
const wasmInstance = await WebAssembly.instantiate(await WebAssembly.compile(wasmCode), imports); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need compile
separately.
const wasmInstance = await WebAssembly.instantiate(await WebAssembly.compile(wasmCode), imports); | |
const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance; |
}} | ||
wasmCode = await Deno.readFile(file); | ||
break | ||
case 'http:': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming you want it to work for https
too?
case 'http:': | |
case 'http:': | |
case 'https:': |
crates/cli-support/src/js/mod.rs
Outdated
console.error(`Unsupported protocol: ${{url.protocol}}`); | ||
Deno.exit(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to just throw an error and give a caller a chance to handle it, rather than forcefully close the whole program.
crates/cli-support/src/js/mod.rs
Outdated
break | ||
case 'https:': | ||
case 'http:': | ||
const wasm_url = import.meta.url.substring(0, import.meta.url.lastIndexOf('/') + 1) + '{module_name}_bg.wasm'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more reliable to use real URL resolution instead of string manipulation. You can probably reuse this code:
wasm-bindgen/crates/cli-support/src/js/mod.rs
Line 648 in 8594cae
input = new URL('{stem}_bg.wasm', import.meta.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry and thank you.
I'm a newbie, I didn't know that URL has this kind of usage before this. So some codes used bad methods to try to get the _bg.wasm
file in the same directory.
Now the code has become clean🎉
crates/cli-support/src/js/mod.rs
Outdated
if (file.startsWith('/')) file = file.substr(1); | ||
file = await Deno.realPath(file + '/../{module_name}_bg.wasm'); | ||
}} else {{ | ||
file = file.substring(0, file.lastIndexOf('/') + 1) + '{module_name}_bg.wasm'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here about URL manipulation.
crates/cli-support/src/js/mod.rs
Outdated
let file = new URL(import.meta.url).pathname; | ||
if (Deno.build.os === 'windows') {{ | ||
if (file.startsWith('/')) file = file.substr(1); | ||
file = await Deno.realPath(file + '/../{module_name}_bg.wasm'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we use ../
on Windows but not elsewhere?
Co-authored-by: Ingvar Stepanyan <[email protected]>
Co-authored-by: Ingvar Stepanyan <[email protected]>
Thanks for this1 |
@juzi5201314 Didn't see the updates following the last round of reviews, thanks, code looks a lot better! |
Fix #2296