emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.62k stars 3.28k forks source link

USE_PTHREADS=1 and ALLOW_MEMORY_GROWTH=1 restricts --js-library file in ES5. #9190

Closed y-ich closed 4 years ago

y-ich commented 5 years ago

Hi.

When you compile with options "-s USE_PTHREADS=1 -s ALLOW_MEMORY_GROWTH=1 --js-library your_library.js", if your_library.js is written as ES2015, you will get the following error.

JS optimizer error:
Unexpected token: name (GraphModelWrapper) (line: 3363, col: 16, pos: 142778)

================================

      setTempRet0(($i) | 0);
    }

  function method_support() {
          class GraphModelWrapper {
                ^
================================

/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:282
        throw new JS_Parse_Error(message, line, col, pos);
        ^
Error
    at new JS_Parse_Error 
(/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:260:22)
    at js_error (/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:282:15)
    at croak (/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:752:17)
    at token_error 
(/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:760:17)
    at unexpected 
(/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:766:17)
    at Arguments.semicolon 
(/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:786:51)
    at prog1 (/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:1325:29)
    at simple_statement 
(/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:917:35)
    at /Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:827:35
    at block_ (/Users/XX/emsdk/fastcomp/emscripten/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:1010:32)
Traceback (most recent call last):
  File "/Users/XX/emsdk/fastcomp/emscripten/emcc.py", line 3492, in <module>
    sys.exit(run(sys.argv))
  File "/Users/XX/emsdk/fastcomp/emscripten/emcc.py", line 2359, in run
optimizer)
  File "/Users/XX/emsdk/fastcomp/emscripten/emcc.py", line 2896, in do_binaryen
    final = shared.Building.apply_wasm_memory_growth(final)
  File "/Users/XX/emsdk/fastcomp/emscripten/tools/shared.py", line 2710, in apply_wasm_memory_growth
    fixed = Building.js_optimizer_no_asmjs(js_file, ['growableHeap'])
  File "/Users/XX/emsdk/fastcomp/emscripten/tools/shared.py", line 2339, in js_optimizer_no_asmjs
    run_process(cmd, stdout=open(next, 'w'))
  File "/Users/XX/emsdk/fastcomp/emscripten/tools/shared.py", line 181, in run_process
result.check_returncode()
  File "/Users/XX/emsdk/fastcomp/emscripten/tools/shared.py", line 161, in check_returncode
raise Py2CalledProcessError(returncode=self.returncode, cmd=self.args, output=self.stdout, stderr=self.stderr)
tools.shared.Py2CalledProcessError: Command '['/Users/XX/emsdk/node/8.9.1_64bit/bin/node', '/Users/XX/emsdk/fastcomp/emscripten/tools/js-optimizer.js', '/tmp/emscripten_temp_wuwpaB/index.wasm.o.js', 'growableHeap']' returned non-zero exit status 1

If you write your_library.js in ES5, there are no problems.

My environment is,

  1. macOS 10.14.6
  2. using latest-upstream
    • emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.38.40
    • clang version 10.0.0 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-llvm-llvm--project 10dad95a75592717d2f7c0ebc181fb8a970a8df7)

What is wierd is that I am using latest-upstream but "JS optimizer" is using parse-js.js whose path includes fastcomp.

Thanks.

embeddedt commented 5 years ago

The optimizer is failing at the stage where it runs uglify-js. A quick look at the package on https://www.npmjs.com/package/uglify-js shows:

To minify ECMAScript 2015 or above, transpile using tools like Babel.

VirtualTim commented 5 years ago

Ran into this one as well due to using let's. These are normally (USE_PTHREADS=1 and ALLOW_MEMORY_GROWTH=0) fine. If I remember right about 6 months ago the project moved over to using a different minifier that supports ES5 and above, maybe ALLOW_MEMORY_GROWTH was missed during the move? Edit: this PR I think: #6072.

I don't have time to look into this at the moment, but it probably shouldn't be too hard?

kripken commented 5 years ago

@VirtualTim correct, we now have the option to write JS optimizer passes in Acorn, which supports ES6.

To fix this, the growableHeap pass would need to be removed from js-optimizer.js and rewritten in acorn-optimizer.js.

Help would be welcome here! I can help with any acorn optimizer questions if there are any.