javascript - Emscripten with module loaders - Stack Overflow

I'm trying to use Emscripten to turn a C library into a portable JavaScript module, that will be l

I'm trying to use Emscripten to turn a C library into a portable JavaScript module, that will be loaded by an AMD (such as Require.JS) and provide access to its functions and things:

require("vendor/mylib.js", function(mylib) {
    mylib.function1();
});

However, I've seen that Emscripten pollutes the global namespace with lots of variables, which is against the premise that modules should be independent and not collide with other loaded modules.

So the question is: What's the best way to use Emscrpiten with AMDs?

Is there a way I can tell Emscripten to not leak anything to global?

I'm trying to use Emscripten to turn a C library into a portable JavaScript module, that will be loaded by an AMD (such as Require.JS) and provide access to its functions and things:

require("vendor/mylib.js", function(mylib) {
    mylib.function1();
});

However, I've seen that Emscripten pollutes the global namespace with lots of variables, which is against the premise that modules should be independent and not collide with other loaded modules.

So the question is: What's the best way to use Emscrpiten with AMDs?

Is there a way I can tell Emscripten to not leak anything to global?

Share Improve this question asked Mar 29, 2015 at 12:43 Alba MendezAlba Mendez 4,6451 gold badge41 silver badges56 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

There are 2 mand line options from the emcc docs that can help, --pre-js <file> and --post-js <file>. They allow you to wrap the generated code, and so allows you to integrate with AMDs.

For example, you could have a prefix file of

// prefix.js
define(function() {
  return function(Module) {

and a postfix file of

// postfix.js
  };
});

which you would pile outputting to myModule.js using something like

emcc --pre-js prefix.js --post-js postfix.js -o myModule.js myModule.cpp

and then require the module, using the RequireJS syntax:

require(['myModule'], function(myModule) {
  myModule({... Module definition object ...});
});

A fuller example, setting a canvas element in the module definition object follows. I'm also including the domReady plugin to be able to grab elements when the DOM is ready.

<!DOCTYPE html>
<html>
  <head>
    <script src="require.js"></script>
    <script>
      require(['domReady', 'myModule'], function(domReady, myModule) {
        domReady(function() {
          myModule({
            canvas: document.getElementById('canvas_1')
          });
          myModule({
            canvas: document.getElementById('canvas_2')
          });
        });
      });
    </script>
  </head>
  <body>
    <canvas id="canvas_1"></canvas>
    <canvas id="canvas_2"></canvas>
  </body>
</html>

This then not only preserves the global namespace as you requested, but also allows you to have more than one Emscripten-powered canvas element in the page at once, if you need to.

The above HTML page can be seen working at http://plnkr.co/edit/8jE3uLwrlszQuHbixU68?p=preview . It loads a C++ program:

#include <iostream>
using std::cerr;

int main() {
  cerr << "In C++ main function";
}

If you load the Plunker, then you should see "In C++ main function" twice, once for each loaded module.

If you need access to the Module object amended by Emscripten, say to call exposed library functions, you can do something like the following, waiting for all dependencies that Emscripten itself loads using the monitorRunDependencies option:

require(['myModule'], function(myModule) {
  var moduleDef = {
    monitorRunDependencies: function(numberOfDependenciesRemaining) {
      if (numberOfDependenciesRemaining) return;
      // At this point we can call functions added to moduleDef
      // such as cwrap or ccall
    }
  }
  myModule(moduleDef);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744382984a4571522.html

相关推荐

  • javascript - Emscripten with module loaders - Stack Overflow

    I'm trying to use Emscripten to turn a C library into a portable JavaScript module, that will be l

    8天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信