While I am a big-time fan of C++, I don’t go so far as to say it’s “the best programming language ever, period.” As an engineer, still, I really believe it’s an extremely effective tool for proper programming. Fans and advocates of more dynamic programming languages such as Python, Ruby and JavaScript typically consider C++ a boring, verbose language with a pedantic compiler that never stops complaining about seemingly unimportant details.
Well, if you ask me, that is the most relevant feature of C++. I don’t see it as a limitation, but as a framework that helps structuring my ideas into machine compatible form. Over time I have grown to appreciate that the C++ compiler is a programmer’s best friend.
More recently I’ve also become fascinated with the incredible progress that browsers vendors have made in bringing standard-compliant modern capabilities to the general user. While HTML5 canvas and WebGL are definitely the most visible additions, we should not forget about local storage, programmatic audio support, video support, web sockets and everything that is being standardized. This extensive feature set is amazing and, to me, this depicts a future where almost-native applications and games are available on every single platform through browsers.
So far, taking advantage of the great platform that the Web is, has required programming in JavaScript. JavaScript is terrible. I am no JS ninja, and I find it incredibly frustrating having no hint about what kind of thing will be passed as an argument of a function, or if anything at all will be passed, for that matter. How some people can build and effectively maintain a large-scale code base in JS is beyond my comprehension. I am sure many developers feel that same way that I do.
So we have some pretty awesome Web APIs, tightly coupled in a programming environment that, to me, looks like a joke. Since that sounds like a problem to me, and I like to solve problems [1], I decided to bring my language of choice, C++, to the Web platform.
My solution was to write duetto, a C++ compiler for the Web. With a couple of friends, we are trying to build a company around it.
duetto is a C++ compiler for the Web, not for JavaScript. Most of the work involved in writing duetto was related to finding a way to generate good JavaScript from C++. To do that, we wrote a special LLVM backend, based on a couple of (hopefully) clever tricks. But we did that only because we had no choice. JavaScript is currently the only practical way to deploy code to browsers. But in our vision JS is only a mean to our goal of enabling C++ as a programming language for web apps and games.
What we want to do is to provide a better programming language for the Web, to take the most advantage of all the fantastic underlying technologies such as HTML5 and CSS, which do their job more than well. With duetto, designers can still work using tools they know, while developers can write a sound application logic in robust C++ code. And, by the way, we are not trying to provide an half-backed widget library that would always be lagging behind what browser APIs can provide. With duetto you access the browser directly, with no need of any middle layer.
One of the intended uses of duetto is to port existing C++ applications, especially games, to the Web. But we also want to make it a primary choice to write new web apps. One of the requirements of modern apps is continuous communication with remote servers, for example to receive other users’ updates. This is why we have also introduced an experimental support for transparent RPC to server side code.
Calling this RPC is actually kind of limiting. Our objective is to provide a seamless experience to make it possible to write client and server side code in the same codebase and let the compiler generate the two sides of compiled code. Take a look at some example code.
We are all standing on the shoulder of giants. In our case the giant is the LLVM/clang project. Generating JavaScript required some improvements to the compiler core that we hope to include in the upstream projects. LLVM provide a large array of optimizations passes on code generated by clang, which means that JavaScript code reaching the browser has been already heavily optimized, lowering the burden on the JS engine. In general, the JS generated by duetto tries to play well with modern JS engines, by providing hints that should make it possible to generate good native code. Currently the code is generic, but we plan to explore the possibility of generating code tailored for each JS engine (e.g. Chrome’s V8 or Mozilla’s SpiderMonkey). In the end this would be not different from the CPU specific optimizations that are available on regular architectures.
The release date for duetto is now getting really close and we are really excited about shipping it and see what people can do with this technology. If you want to take a preview please visit http://leaningtech.com, there are still a few slots open in our extended beta program.
[1] Maybe I like solving problems too much, I wrote Lightspark because Adobe’s Flash Player was too slow in rendering full screen videos of kittens on my workstation