3 ms·
The reason I don't use WebAssembly is that browsers do not support the text format. Often it is just a tiny loop that one wants to optimize. Writing it manuall
by TekMol 2y ago
The reason I don't use WebAssembly is that browsers do not support the text format.
Often it is just a tiny loop that one wants to optimize. Writing it manually in WAT would be nice. But adding a whole toolchain and a compile step to the stack is not worth it.
Shouldn't it be straight forward to compile WAT to WASM? I hope one day browsers will support it.
- pjmlp 2y agoYou can do that easily with binaryen already, and make use of parseText(). https://web.dev/articles/binaryen https://web.dev/articles/binaryen
- TekMol 2y agoNot sure what you mean. I guess this is a misunderstanding. What I mean is that I would like something like this to work: code = `( func $add (param $a i32) (param $b i32) (result i32) local.get $a local.get $b i32.add )`; module = WebAssembly.compile(code); alert(module.add(3, 5)); // alerts "8".
- tyushk 2y agoThe WebAssembly reference interpreter [0] has a JS library that lets you do this. let wast = require("./wast.js"); let binary = wast.WebAssemblyText.encode("(module)"); [0] https://github.com/WebAssembly/spec/tree/main/interpreter#javascript-library https://github.com/WebAssembly/spec/tree/main/interpreter#ja...
- pjmlp 2y agoWhich you can get by making use of binaren.js library already, so you won't get any browser vendor love to include the feature into the browsers directly.
- deleted 2y ago[deleted]
- flohofwoe 2y agoChances are that you won't get much out of WASM for such small snippets anyway. Contrary to popular belief, well-written Javascript isn't drastically slower than WASM in most situations.
- TekMol 2y agoThen why does WASM exist? If someone wants to compile whatever other language to run in the browser, why not just compile it to well-written JavaScript?
- sirwhinesalot 2y agoWe used to have asm.js for that. For various reasons wasm is a replacement for it. JS doesn't even have proper integer types, it is not a good target language.
- masklinn 2y agoasmjs had an i32 type by (ab)using JS semantics and a common jit optimisation: bitwise ops are defined for 32 bits two’s complement integer. So x|0 necessarily results in an i32.
- flohofwoe 2y agoSimple: Javascript can focus on being a human-friendly programming language, while WASM can focus on being a compile target. If you've been around in the asm.js times, this conflict was a real concern.
- rav 2y agoYou could use handwritten asmjs in that case. I did it at work to improve performance on some pixel-wise software rendering we were doing. (We have since then switched to WebGL, but asmjs was a nice stepping stone.) If I recall correctly, it basically boils down to using small fixed size arrays and appending "|0" after each uint32 operation and "+" in front of each float32 operation - so it's something you can do by hand. Of course, one of my colleagues then made a tweak which gave a surprisingly huge slowdown - you should probably declare loudly (with comments etc.) that a certain code block is intended to be asmjs if you do this in a large codebase.
- TekMol 2y agoasmjs is great. Imho the better approach than wasm. But it seems to not have great browser support (no Safari for example). And support is probably not getting better in the future.