3 ms·
I want Rust scripting support in Servo. <script type="text/x-rust" src="foo.rs"> Since Rust is a safe language this should be possible without compromising
by modeless 13y ago
I want Rust scripting support in Servo.
<script type="text/x-rust" src="foo.rs">
Since Rust is a safe language this should be possible without compromising security, though I don't think anyone's yet attempted to write a JIT compiler for Rust. Has the Servo team considered this as a possibility?
- steveklabnik 13y agoI'd imagine that a Mozilla project would just focus on compiling to asm.js...
- modeless 13y agoThere are a lot of problems with asm.js: the heap can't be resized after creation, the code size is really large (esp. with things like C++ templates, which are duplicated for each specialization), threads don't work, datatypes like 32-bit floats and 64-bit integers are problematic, interop with JS and DOM APIs is slow. These things can only be improved with browser cooperation, which sort of defeats asm.js's main advantage of working in any browser. Native Rust support would be far better.
- Touche 13y agoAll of those things you mentioned are currently being worked on and will be added to JavaScript directly.
- modeless 13y agoWhat's the solution being worked on for code size?
- azakai 13y agoCode size currently is avoid equivalent to native builds, if you gzip both of them. The emitted code is basically similar to a native build except it's in text format (so the difference mostly vanishes after gzip). You mentioned things like templates in C++ - those will be an issue in asm.js just as they are in a native build, no more and no less.
- modeless 13y agoMy experience has been that emscripten builds are significantly larger than native even after gzip. Templates are of course an issue in native builds too, but native builds are usually less size-constrained than web apps. Sending (minimized) Rust source code, as we do with JavaScript, would result in significantly smaller downloads (and less code to compile) vs. asm.js. Also shipping a standard library with the browser (as JS does) would be another significant reduction.
- azakai 13y agoI would be curious to see your code, if that's possible - perhaps we can optimize things better or you are hitting a bug. How much larger was it, after gzip? I'm not sure you'd want to send Rust source code - you'd need to wait for it to compile on the other side. (Rust is designed for many things, but fast compilation is not a primary goal.) Most likely you'd want to build it ahead of time on the server, just like C++, but then I'm not sure there would be a size advantage to Rust.
- modeless 13y agoIt seems to me like making a fast Rust compiler would be an easier problem than making JavaScript run native code fast :) Thanks for the offer of help. Unfortunately I can't send our code, but I just talked to our guy working on emscripten and he said he's already been in touch (though not about code size yet). https://groups.google.com/d/msg/emscripten-discuss/n1qfKPPAy08/a5LJliGPDPQJ https://groups.google.com/d/msg/emscripten-discuss/n1qfKPPAy... My impression of emscripten code size might be out of date, I know that development on asm.js has been pretty rapid.
- emn13 13y agoIt's not just a question of engineering resources; some problems are intrinsically hard. There may well be things (like floats in css) in rust that make fast compilation hard if not impossible - depending on what exactly you want to achieve. After all, the point is to generate efficient code, so a solution that simply disregards most optimizations isn't an option; and most of the type system is likely unavoidable and/or critical to the optimizer. By contrast, asm.js is fairly simple: All the heavy lifting of compiling has mostly been done - the remaining JS was chosen so that it fairly straightforwardly represents assembler instructions. Decisions about types and what that means for semantics (and safety) have been done, inlining, code rearranging etc. - that's all largely done. In a way, asm.js is assembler with a weird syntax and environment. I really doubt you'd ever get something like Rust (or really most any other statically compiled language) to ever compile as fast, given comparable resources.
- coldtea 13y ago"Coming in a browser near you in another 5-6 years"... I've followed Harmony / ES6 for like 7 years now. Don't hold your breath. (And I know that you can get lots of these features with special switches in Chrome, FF etc. Also know about Traceur. I mean don't hold your breath about having it released and available all-around for real web work, frameworks updated for it, etc).
- lomnakkus 13y agoOn Traceur: We're currently using a subset[1] of Traceur for out frontend coding and I can't say I've noticed any particular problems doing so. I also don't see any problem if libraries we were using would change to using Traceur, so long as they were to distribute the compiled output. Yes, we'd have to include the Traceur runtime, but we already do, so... (There could be versioning issues, but I suspect that the Traceur runtime is relatively static and so could remain "compatible enough" across versions. One could also easily imagine JS libraries built on Traceur distributing both the original ES6 source and the transpiled JS.) [1] The caveat here is that we have to support IE8 (for now at least) which the Traceur runtime doesn't support, so there are a few features we cannot use (yet).
- cpeterso 13y agoDoes Traceur always polyfill ES6 features, even if the browser supports them natively?
- lomnakkus 13y agoOf course pure source transformations (classes, let, const, splats and such) just yield ES5 source directly, and so you lose native browser support for those. That's no big deal since most browsers really aren't really up to scratch yet in ES6 support. I'm not sure about the runtime, but I think I remember it doing some (very basic) feature detection while browsing through the generated code during a debugging session. EDIT: Frankly, I think it would probably be a good idea to polyfill all browsers until they all really implement the ES6 spec.
- Touche 13y agoWhy do you want to script with Rust?
- modeless 13y agoThe same reason people want asm.js: speed, and avoiding the many problems of JavaScript.
- pekk 13y agoJavascript is a terrible language, even though Rust is experimental it would be a big improvement. And it does have attractive aspects as a language. More broadly, breaking the iron grip Javascript has on client-side development would be wonderful and Mozilla is the last stumbling block in front of this goal.
- malandrew 13y agoInsofar as scripting languages go, Racket/Scheme would also be a nice addition. After all that is what Eich was going for when he created JavaScript. He wanted a language with scheme-like semantics but other forces wanted it to have a C-style syntax.
- keeperofdakeys 13y agoRust as a language is compiled, so interpreting it doesn't make much sense, and JIT only makes sense for interpreters.
- Jweb_Guru 13y agoLike the interpreted language that is Java? :) A JIT compiler could make sense for Rust, though I'd agree that the usecases for that are probably not very interesting compared to some other languages.
- keeperofdakeys 13y agoBut java bytecode itself is interpreted. For things like the web, you really want a language that can start executing immediately, so you really need to interpret, or have an agnostic bytecode.
- Jweb_Guru 13y agoI think it's a bit of a stretch to call the JVM an interpreter. Having said that, if you want to deliver Rust as bytecode, emscripten would seem to satisfy that usecase (though obviously that's not what the original author was talking about, and it doesn't work properly at the moment).
- nardi 13y agoThis is one of my pet peeves. Any language can be either compiled or interpreted. I think what you mean is, "No one has written an interpreter for Rust." But that is also nonsense, as all compilers contain an interpreter. The difference is just that instead of streaming instructions straight to the CPU, compilers wrap the instructions in an executable format and save them to a file.
- keeperofdakeys 13y agoI was commenting specifically on "JIT Compiler". It should really be "JIT Interpreter". Naturally, it's possible to write an interpreter for any language that is compiled (though sometimes the semantics don't make sense). For a language like Rust, you'd probably want to compile to a bytecode to interpret, to allow all the compiler checks to be preserved. Out of interest, I did a search, and there does seem to be a (depreciated) Rust REPL.
- TheCoelacanth 13y agoRust is safe in the sense that it avoids many categories of bugs; it's not safe in the sense that you can run untrusted code without causing security issues. For instance, you don't want a script in a webpage to be able to read/write arbitrary files on your computer, but a Rust program would be allowed to do that. You would have to restrict a lot of capabilities before it would be safe for your browser to run untrusted Rust scripts.