Live data from Hacker News

Closure Compiler in JavaScript

developers.googleblog.com

61–70 of 115 posts

Re: Closure Compiler in JavaScript

#63

What's with all these similar and confusing names; Closure Compiler, Clojure, Clozure CL?

So "Closure" is a real comp sci word used to describe variable scope lifetimes in languages includes JavaScript and others. So when google named their project, they basically took a word from JavaScript/CompSci that starts with the same letter as "Compiler" (plus other similar sounds). I don't think there's much more to it than that.

"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

Re: Closure Compiler in JavaScript

#64

Can anyone comment on the state of Typescript - Google Closure integration ?

What do you mean by Typescript Google closure integration?

Typescript understands most of closure comment syntax. They should throw typechecking errors which they currently aren't doing.

Re: Closure Compiler in JavaScript

#65
post #57

Earlier quoted context omitted.

There's no intrinsic property of “desktop” languages preventing them targeting “browser” languages. Any Turing-complete language can be compiled to and from any other. 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 ex…

So I don't know much about Java, but I was under the impression that it compiles to a binary fie (like C), and thus could do things that directly affect the hardware like malloc. How would Javascript handle something like that? I'm not aware of Javascript being able to perform these kinds of tasks. I guess they just have workarounds, like what you're saying for file access?

It compiles the Java source, not the Java bytecode, but even if it did....

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.

Re: Closure Compiler in JavaScript

#66

Can anyone comment on the state of Typescript - Google Closure integration ?

This is a great question, and to clarify for anyone not following along closely:

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.

Re: Closure Compiler in JavaScript

#67

This is Google's JavaScript to JavaScript compiler, originally written in Java, but compiled with their Java to JavaScript compiler so that it runs in JavaScript... fair enough...

I'm not sure this is what Gosling had in mind when he said "compile once, run anywhere" :P

It's 2016, the mantra now is "Compile thee times, run slowly in a handful of web browsers"

Re: Closure Compiler in JavaScript

#68
post #57

Earlier quoted context omitted.

There's no intrinsic property of “desktop” languages preventing them targeting “browser” languages. Any Turing-complete language can be compiled to and from any other. 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 ex…

So I don't know much about Java, but I was under the impression that it compiles to a binary fie (like C), and thus could do things that directly affect the hardware like malloc. How would Javascript handle something like that? I'm not aware of Javascript being able to perform these kinds of tasks. I guess they just have workarounds, like what you're saying for file access?

What do you mean by 'affecting hardware'?

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/

Re: Closure Compiler in JavaScript

#70
post #27
post #19

Earlier quoted context omitted.

The advanced compilation mode does some pretty powerful dead code removal and minification [1]. I guess that is available from plugins in Babel as well but Google has been using it for Gmail, Google.com etc, so it's quite battle tested. [1]: https://developers.google.com/closure/compiler/docs/api-tuto...

In my experience Babel minified better than Closure Compiler with less maintenance of the externs on my part.

see http://www.syntaxsuccess.com/viewarticle/tree-shaking-in-jav...
Post reply on HN