Closure Compiler in JavaScript
developers.googleblog.com
developers.googleblog.com
> How does this work?
>
> This isn't a rewrite of Closure in JavaScript. Instead, we compile
> the Java source to JS to run under Node, or even inside a plain old
> browser. Every post or resource you see about Closure Compiler will
> also apply to this version.
Pretty wild.EDIT: I'm googling now to learn more but I'm not asking to challenge anybody, I'm asking because probably you guys know more than me and I wanna know how this works.
GWT compiles Java to JS. If the Closure Compiler is mostly Java code, you only need to implement a few native code things (like replace file loading with just giving it source code as strings) in JS to do a cross compile to javascript.
And you don't even need to go down to that level of abstraction for Java-to-JavaScript, the two share many commonalities. Java classes can become JavaScript objects, Java methods can become JavaScript functions, Java exceptions can become JavaScript exceptions, and so on.
There's some things you can't directly translate (64-bit integer math, for instance), but these can be emulated.
As for “desktop” OS facilities, the browser may not be able to provide all of these, but whatever you need can be substituted (file access can be replaced with e.g. localStorage) or worked around.
As a simple example, if you create an array of numbers in Java, you don't need to worry about how the memory for that is allocated as long as you get an array of numbers in javascript.
The difficulties are more in how you get around where the two languages differ widely and making sure you match the semantics (often subtle) of the source language without tanking performance in the target language.
For that example of a numeric array, Java has very different expectations when it comes to bounds checking and exactly what numeric type is in there, for instance, and that has to be enforced by the code you generate if you are going to have any sort of sanity while authoring in the source language.
Wouldn't you need to follow the syntax and not the semantics? Aren't the semantics what the syntax is abstracting? (I'm genuinely curious, not trying to flame.)
The syntax is really just one way to express an intention. What you care about are the intensions--the meanings. That's exactly what the semantics are. For example, the semantics of this statement in Java
int foo = 1 / 0;
are very different from the semantics of this statement in JavaScript: var foo = 1 / 0;
even though the two are syntactically rather similar.(In the former, Java will throw an ArithmeticException, whereas in JavaScript it will return Infinity.)
So you want to ensure that your translation is meaning preserving, that is, that the source code and the generated code are semantically the same.
Take your malloc example, you can compile a C program that uses malloc to JS, you just have write a malloc in JS that will behave the way you expect it to.
C gets compiled to assembly, assembly to machine code.
Their compiler just takes Java source and emits JS source instead of JVM bytecode.
Compiling is separate from linking. In C, a simple program calling malloc() and nothing else will include malloc.h. That results in a lot of definitions being brought in, but no implementations. Compiling will produce an object file. Again, no implementations for malloc(), it's an empty address waiting to be filled in. When you link, it all comes together, assuming the linker can find an implementation for malloc() (and there can be many implementations).
So compiling is rather straightforward. The harder part is automatic linking -- anything Java-specific that doesn't directly exist in JS (like long ints -- https://github.com/dcodeIO/long.js), or any calls to some library that also doesn't have a direct equivalent (like, say, some Swing draw commands), will have errors at link time. You can resolve that by creating an interface-equivalent version in JS and telling the linker about it, or you can go all the way to emulating a CPU and other hardware for an OS: https://win95.ajf.me/
All your code affects the hardware in the sense of read/write RAM and run on CPUs. C and malloc has fewer layers than JS and new, but they're similar.
There's a series of steps to make this happen, but as long as the input can be represented in the output, this should always be possible. JavaScript is Turing Complete.
http://steve-yegge.blogspot.com/2007/06/rich-programmer-food...
Typescript understands most of closure comment syntax. They should throw typechecking errors which they currently aren't doing.
In this case, I think closure should start to understand typescript syntax. Or typescript needs to borrow ideas from closure on minifying and dead code removal.
Some of us using TypeScript dream of a day when TypeScript emits JavaScript already annotated with type meditations which express as much of the typescript type system as possible in a way that Closure can understand, thereby making it possible for Closure to perform maximally efficient tree shaking and other magic with code that started as TypeScript. Such a thing as necessary and helpful because the addition of this detailed type data makes it possible for Closure to do a much better job that it can do with "just" untyped JavaScript.
https://github.com/angular/tsickle
It's already used within Google to effectively optimize TypeScript apps. However, someone still needs to write the integrations between tsickle and the multitude of public JS build systems and there's not a lot of pressure on me to do it (I have lots of other work to do).
But my dream is that this work could make it back into typescript itself - that a future version could possibly be persuaded to emit Closure type limitations without the help of a parallel/postprocessing pass by a tool.
https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-i...
This may not be the "integration" some are seeking but it's sure a step ahead.
https://groups.google.com/d/msg/clojurescript/jMSQChpCdhg/p7...
"ClojureScript for Skeptics" https://youtu.be/gsffg5xxFQI?t=20m9s
The remaining bit of Java is like "a little piece of kelp that's stuck to the hull, and even though it's little, you don't want anything stuck to the hull" (http://www.tv-quotes.com/shows/the-west-wing/quote_14096.htm...).
If you're using Google Closure with Gulp in your work flow, you may find gulp-gjslint useful (https://github.com/TomSeldon/gulp-gjslint).
It's a gulp wrapper around gjslint, I. E. a Google Closure linter. :)
"Clojure" is a programming language again based on the regular word "closure" but with some twists: The "j" is a tip of the hat to Java and the JVM where clojure is designed to run. This also has the nice property that the "zh" sound of the "s" in closure is also used with "j" sometimes (I think due to French). Thirdly I believe the "CL" letters of clojure are a tip to Common Lisp.
I'm not familiar with "Clozure CL" but the history is here: http://ccl.clozure.com/history.html
Isn't it because it can optimize the output so that it only includes those dependencies that the program references, i.e. the whole program is like a closure that closes over its dependencies?
In general, I think you should not pick names from the domain where your product/tool operates in. For example, it would be equally confusing if a car manufacturer would brand its cars "Engine" or "Wheel". Such naming is worse than just choosing a fantasy name.
P.S.: As another example, "Java" was a good name. But then Netscape invented a language and called it "JavaScript", which was of course a very poor choice.
(PS Is it possible to have a biased account? I see a lot of articles I had posted, later posted by someone else make it to the front page.)
> As this compiler runs in pure JavaScript, the compiler cannot load or save files from your filesystem directly.
Why? Node.js has the fs library for that. Why do I have to use gulp?Would prefer a cli version like browserify. And then integrate it on other tools.
not sure if it's just bootup time or actual compilation passes, hopefully the former and does not explode exponentially (or even linearly) with code size.
[1]: https://developers.google.com/closure/compiler/docs/api-tuto...
Interestingly, we're finally starting to see these ideas get more mainstream as people adopt es6 modules and publish bundled code with explicit externs in tools like Webpack 2 and Rollup.
Babel can be used in the same toolchain as Closure Compiler, they're not mutually exclusive or redundant as they perform different tasks.
use it with babel by using splittable.js
What language are they talking about?