News for hackers. Stuff that matters.
Re:Closure Compiler in JavaScript by komali2 on Tuesday December 06, 2016 @02:57PM (#13117757)
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?
Re:Closure Compiler in JavaScript by magicalist on Tuesday December 06, 2016 @03:05PM (#13117824)
It's java source to javascript source compilation(/translation/transpilation), so you only need to follow the semantics of the language, not the actual implementation.

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.

Re:Closure Compiler in JavaScript by dmux on Tuesday December 06, 2016 @09:16PM (#13119980)
>...so you only need to follow the semantics of the language, not the actual implementation.

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.)

Re:Closure Compiler in JavaScript by z1mm32m4n on Wednesday December 07, 2016 @04:00AM (#13121346)
Not OP, but I'm pretty sure you care more about the semantics here.

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.

Re:Closure Compiler in JavaScript by icebraining on Tuesday December 06, 2016 @06:51PM (#13119309)
In this case it doesn't apply, but Emscripten actually compiles LLVM assembly to JavaScript. The way they deal with malloc is by implement it in JS, that is, instead of calling the kernel malloc(), you're just calling a JS function that expands an array of bytes (specifically: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...)
Re:Closure Compiler in JavaScript by Jach on Tuesday December 06, 2016 @03:48PM (#13118191)
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 by likeclockwork on Tuesday December 06, 2016 @03:17PM (#13117911)
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 by paulddraper on Tuesday December 06, 2016 @07:55PM (#13119662)
> do things that directly affect the hardware like malloc

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.