最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How load an emscripten generated module with es6 import? - Stack Overflow

programmeradmin2浏览0评论

I am trying to import a module generated with emscripten as a es6 module. I am trying with the basic example from emscripten doc.

This is the command I am using to generate the js module from the C module:

emcc example.cpp -o example.js -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s EXPORT_ES6=1 -s MODULARIZE=1

The C module :

#include <math.h>

extern "C" {

  int int_sqrt(int x) {
    return sqrt(x);
  }
}

Then importing the generated js module:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Wasm example</title>
  </head>
  <body>
    <script type="module">
      import Module from './example.js'

      int_sqrt = Module.cwrap('int_sqrt', 'number', ['number']);
      console.log(int_sqrt(64));
    </script>
  </body>
</html>

This is failing because cwrap is not available on the Module object:

Uncaught TypeError: Module.cwrap is not a function

I am trying to import a module generated with emscripten as a es6 module. I am trying with the basic example from emscripten doc.

This is the command I am using to generate the js module from the C module:

emcc example.cpp -o example.js -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s EXPORT_ES6=1 -s MODULARIZE=1

The C module :

#include <math.h>

extern "C" {

  int int_sqrt(int x) {
    return sqrt(x);
  }
}

Then importing the generated js module:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Wasm example</title>
  </head>
  <body>
    <script type="module">
      import Module from './example.js'

      int_sqrt = Module.cwrap('int_sqrt', 'number', ['number']);
      console.log(int_sqrt(64));
    </script>
  </body>
</html>

This is failing because cwrap is not available on the Module object:

Uncaught TypeError: Module.cwrap is not a function

Share Improve this question asked Nov 14, 2018 at 21:37 EturcimEturcim 8182 gold badges10 silver badges26 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

As you're using MODULARIZE, you have to make an instance of the Module first.

import Module from './example.js'
const mymod = Module();
const int_sqrt = mymod.cwrap('int_sqrt', 'number', ['number']);
console.log(int_sqrt(64));

You could also try the MODULARIZE_INSTANCE option.

You may need to wait for it to finish initialising - I'm not sure when the function is so simple. That would look like this:

import Module from './example.js'
const mymod = await Module();
const int_sqrt = mymod.cwrap('int_sqrt', 'number', ['number']);
console.log(int_sqrt(64));

What worked for me:

emcc compile options:

emcc latuile.cpp bombix.cpp -o latuile-origine.js -s EXPORTED_FUNCTIONS='["_latuile","_bombix"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -s ALLOW_MEMORY_GROWTH=1  -s EXPORT_ES6=1 -sMODULARIZE -s EXPORT_NAME="createMyModule"

Where latuile and bombix are the names of the C++ functions I want to call.

At the bottom of the produced file latuile-origine.js, I read:

export default createMyModule;

Then in the Javascript module that imports from latuile-origine.js, I have (notice the 'default' keyword):

import {default as createMyModule} from "./latuile-origine.js";
var Module;
function clientFunction()
{
...
    const bombix = Module.cwrap("bombix","string",["string","string","string","string"])
    const jsonResponse = bombix(rectdim, translations, sframe, slinks);
...
}

window.main = function main()
{
    createMyModule().then(function(mymod){
        Module = mymod;
    });
}
发布评论

评论列表(0)

  1. 暂无评论