Closure compiler produces invalid asm.js
Closure compiler does not seem to recognize asm.js
Compile hello.cpp to hello.js with emcc -o hello.js hello.cpp -O2
#include <cstdio>
int main() {
printf("Hello World\n");
return 0;
}
Run d8 --validate-asm hello.js
hello.js:2: Converted asm.js to WebAssembly: success
hello.js:2: Instantiated asm.js: success
Hello World!
Copy content of hello.js to https://closure-compiler.appspot.com , try "Simple" optimization. One of the warning is:
JSC_USELESS_CODE: Suspicious code. This code lacks side-effects. Is there a bug? at line 1 beyond character 4095
JSC_USELESS_CODE: Suspicious code. Is there a missing '+' on the previous line? at line 3 character 0
"use asm";var a=new global.Int8Array(buffer);var b=new global.Int16Array(buff...
^
Try to find the location of "use asm" in output, it is at the wrong location. Copy output to a file and run with d8 again. Hello World is printed correctly, but no info indicating that it is even an asm.js code at all, let alone if it is valid or invalid.
Yes, closure has no understanding of asm.js. It treats the code as normal JS, so it might remove the "use asm" string, and it often does changes that remove or modify asm.js annotations, so it's no longer valid asm.js anymore.
When emscripten runs closure compiler on the compile code, in --closure 2, then we intentionally disable asm.js and show a warning about that.