cee-dee / emscripten-tests

Contains test-cases on the emscripten framework.
0 stars 0 forks source link

webidl-test-01: when compiling with -Oz --closure 1 referencing C++ classes/methods from JavaScript does not work anymore #2

Open cee-dee opened 9 years ago

cee-dee commented 9 years ago

Work within the directory webidl-test-01. First, create the glue-code: python tools/webidl_binder.py test.idl glue

Then compile: emcc test.cpp my_glue_wrapper.cpp --post-js glue.js -o test.js -Oz

Open test.html in Firefox-browser, you'll see "11" in the console, which is the expected output.

Then, compile with emcc test.cpp my_glue_wrapper.cpp --post-js glue.js -o test.js -Oz --closure 1

Open test.html in Firefox-browser, you'll see "ReferenceError: Module is not defined".

I'm using the portable SDK. "./emsdk list" gives:

The following individual tools exist: clang-incoming-32bit clang-incoming-64bit clang-master-32bit X clang-master-64bit INSTALLED emscripten-1.29.0 emscripten-1.30.0 emscripten-incoming-32bit emscripten-master-32bit emscripten-incoming-64bit X emscripten-master-64bit INSTALLED

The following Emscripten SDK versions are available: sdk-incoming-32bit sdk-incoming-64bit sdk-master-32bit X sdk-master-64bit INSTALLED

Items marked with * are activated for the current user.

Well, the workflow is a bit more complicated than with embind-test-01, but at least compiler-optimization can be enabled. But unfortunately, using the closure-compiler for further reduction of size, does not work.

Any ideas why this is happening?

kripken commented 9 years ago

Closure is tricky. It minifies the globals, so Module has another name. It will however use Module if it already exists in the global scope, so

  <script>
    var Module = {};
  </script>

before loading the closure-compiled code will make it use that name.

The minification of globals is dangerous for other reasons. It is recommended to use MODULARIZE when using closure, so its minified names do not pollute the global namespace. In that case, Module will be a function that contains everything. Do Module = Module(); to get the Module object itself (or assign it to a nicer name).