6 ms·
Get Started with Rust, WebAssembly, and Webpack
- therealmarv 9y agoI'm searching a starter instruction manual for go and WebAssembly. Any recommendations?
- k__ 9y agoI don't think Go works with WASM, because it uses a GC.
- anentropic 9y agoisn't the GC part of the Go program that gets compiled to WASM though? The Haskell crowd seemed to think that was how it would work for them: https://www.reddit.com/r/haskell/comments/5ydqrc/compiling_to_webassembly/deq0z6m/ https://www.reddit.com/r/haskell/comments/5ydqrc/compiling_t...
- hackcasual 9y agoNot sure that's working. A lot of GC designs need access to program state, like what's currently on the stack, that's not available in WebAssembly.
- dragonwriter 9y ago> I don't think Go works with WASM, because it uses a GC. So? To WASM, how is the GC different from manual allocation, since it's just explicit manual allocation/deallocation in the underlying runtime that gets compiled to WASM.
- loudmax 9y agoBrad Fitzgerald created a github bug to track the issue: https://github.com/golang/go/issues/18892 https://github.com/golang/go/issues/18892 From what I understand, the WebAssembly team is working on their own GC and the Go team would like to use that rather than rolling their own for this target. Also, WebAssembly doesn't have threads yet, which is pretty important for Go. In short, Go in WASM isn't there yet, but it is most likely coming at some point. I expect most popular languages will be ported to WebAssembly at some point, but IMHO Go itself is far more suited to running backend services than in the browser.
- morley 9y agoThe performance bottlenecks I mostly see as a frontend engineer are those related to the browser (execution speed, DOM manipulation, and painting). I'm curious what workloads wasm can improve, since I assume it'll mostly deal with operations in memory. Maybe physics or graphics calculations in JS game engines?
- fiedzia 9y ago> I'm curious what workloads wasm can improve Those where you have a significant amount of computation. Image/audio/video processing, neural networks, text analysis, games fall there too. Generally, those things are not done in the browser currently, but wasm will change that, so I'd say it will rather open new doors then fix existing ones.
- moron4hire 9y agoJavaScript as a language also doesn't make a lot of guarantees. Dynamic languages are great for small projects, but I think we've very clearly seen from the growing interest in linter tools and gradual type systems like TypeScript and Flow that there is huge interest in having more static guarantees. Compiling to WebAssembly has the added benefit of letting you work in a language that has them already, rather than having to shoehorn them into a language that wasn't ever designed for it. And if we are going to have a translation step regardless (ES2016 to ES5), then why not start with something better than ES2016?
- austincheney 9y agoI hear this a lot from people who have little or experience building large applications in high level languages. If you manage your code properly you can have strong/static typing in JavaScript. The difference is that nothing yells at you when you mess that up. Instead the application just runs a little bit slower. I have written large parsers and code beautifiers in JavaScript that blow the shit out of anything I have seen written in other low level languages. There are a couple of advantages that JavaScript provides for this. First of all you get immediate execution without a separate build or compile step. You can see that execution (on really large applications) is a bit slower the first time due to JIT compilation, but successive executions are faster because the code is already compiled in memory. Secondly JavaScript runs almost everywhere. I don't need a special run time or execution context. I can run JS apps on the command line or browser without having to push data to a server location and await a response. Finally, JavaScript is fast now. It executes almost as fast as Java and is only about 4x-8x slower than C++. http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=node&lang2=gpp http://benchmarksgame.alioth.debian.org/u64q/compare.php?lan... The biggest limitation I have found with JavaScript is memory management. Trying to parse a 180mb (or larger) XML file in my JavaScript application can result in an application crash due to excess memory requirements.
- k__ 9y agolol, this feels surreal. This seems like there could be even more stuff automated. Because Rust is statically typed, a rust-loader could "overload" import() so it initialises and extracts the exported/non-mangled function signatures and create the JS equivalents automatically. const {add} = await import("./main.rs"); add(2,2)
- Matthias247 9y agoSure. But I think a better way would be a separate build tool and step, which generates the javascript proxy functions. With that way you get code completions and type checking for the remaining javascript/typescript, if the loader does everything dynamically you are more likely dynamically typed.
- k__ 9y agoTrue, I was just thinking about JavaScript, which is dynamically typed :D
- ianjsikes 9y agoThat is a great idea. I expect more usability improvements to come shortly as more people become interested in WebAssembly.
- vvanders 9y agoCool to see more people digging into Rust + Emscripten. Not sure why the guide is using nightly Rust, stable has worked pretty well on Emscripten for a while. If you need to pass custom linker flags(which emscripten sometimes requires) you can do it via rustflag directive in .cargo/config. We've been using Rust + Emscripten for some cross-platfrom targets(web, win32, osx, android and linux) and been pleasantly surprised with how straightforward+stable it's been.
- steveklabnik 9y agoIt's in a bit of a transition; we're planning on using LLVM's backed for wasm, and I believe that's landed in nightly[1]. Stable has support but through emscripten's LLVM patches. So maybe they wanted to try out the new stuff, or maybe they didn't know that it was on stable already. 1: EDIT: looks like it https://github.com/rust-lang/rust/pull/42571 https://github.com/rust-lang/rust/pull/42571
- ianjsikes 9y agoHi, author here. Thanks for the tip, I wasn't aware compiling with Emscripten would work on stable. That should simplify the process. I'll do some more research and update the guide.
- vvanders 9y agoYeah, was a great article, kudos for that. To be clear I didn't mean it as a criticism just wanted to throw it out there so other people knew that it works on stable.
- bpicolo 9y ago"Number" and "i32" are entirely different types. What happens if you have an impedance mismatch?
- moron4hire 9y agoThe most popular JavaScript engines do indeed have real integers available. You can get real Integers in regular JavaScript, they are just cumbersome. But if you're transpiling, you can hide that difficulty away.
- Franciscouzo 9y agoIEEE 754 double precision floats have enough precision to represent every 32 bit integer, the problem is when you try to use them to represent 64 bit integers, you get problems such as this: https://github.com/nodejs/node/issues/12115 https://github.com/nodejs/node/issues/12115
- bpicolo 9y agoMy question is: what happens when you pass something that's not an integer to the rust lib code via this?
- hackcasual 9y agoJavaScript also has somewhat arcane rules around when Number it's actually treated like an i32
- ianjsikes 9y agoAuthor here. It is my cursory understanding that the Emscripten SDK handles converting types to and from Javascript values and C-like data in the module's virtual memory. I'm not totally sure how that conversion is happening yet. I can tell you that when I pass a string when it expects an i32, it treats it as 0, so `add("foo", "bar") -> 0`
- bpicolo 9y agoWonder if it will be possible to make it panic/error instead
- bjt 9y agoI was surprised to see the author describe Rust-in-the-browser as "low level". I always took that term to mean "closer to the metal", and not statically typed with manual memory management. Rust in this context doesn't seem any more low level than Javascript, to me.
- lmkg 9y agoWeb Assembly (and also asm.js) are "closer to the metal" even though they run in the browser. The execution model is meant to map pretty closely to assembly, not to JavaScript. The browser is supposed to be doing static verification for security purposes, but not much in the way of optimization.
- sp332 9y agoWASM is way lower-level than JS. It doesn't have functions as objects, or really any object system. And the point is for developers to write higher-level web languages that compile down to WASM, so there's no real pressure to add layers of abstraction down at this level. https://medium.com/javascript-scene/what-is-webassembly-the-dawn-of-a-new-era-61256ec5a8f6 https://medium.com/javascript-scene/what-is-webassembly-the-... It seems like you're hung up on the "browser" part, but it's just providing a compiler and a sandbox.
- jononor 9y agoIt's closer to the machine, in this case a virtual machine.
- Animats 9y agoI have the horrible feeling that the main applications of WebAssembly will be hostile code. Ads, malware, spyware, paywalls, and other junk. Look at the history of Java in the browser, and Flash in the browser. 2% great, 98% terrible. Consider WebGL, which is very impressive yet seldom used. There are some very nice demos, most of them several years old, and some half-finished indy games. Other than that, not much. Kind of like VRML. Browsers need a "disable WebAssembly" option. It should require user interaction to turn it on.
- hackcasual 9y agoThe only functionality WebAssembly provides now is more efficient program representation. There are no additional APIs provided.
- pjmlp 9y ago> Consider WebGL, which is very impressive yet seldom used. I want to like it, and do confess it is nice way to prototype shaders. Yet thanks to the way it works, blacklisting GPUs, it means all my mobile devices are OpenGL ES 3.x capable and yet they can hardly display many WebGL pages. While most games just work. Regular users won't be going into browser settings to enable WebGL content, 99% of them won't even know such configuration does exist.
- kristianp 9y agoI agree this should be a major concern. How is an adblocker or tracker blocker going to work when the malicious ad is compiled? It might not be able to access your machine, but it can drain your battery and deliver malware through security flaws.
- wuschel 9y agoI was wondering about that, too. Perhaps a solution would be a SSL-esque certificate for a webassembly blob? Just wondering..