Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

javascript - WASM and Node.js Cannot use 'import.meta' outside a module

I have built FastText C++ module as wasm module using the provided make file, that is using the following flags:

EMCXX = em++
EMCXXFLAGS = --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPostRun', 'FS']" -s "DISABLE_EXCEPTION_CATCHING=0" -s "EXCEPTION_DEBUG=1" -s "FORCE_FILESYSTEM=1" -s "MODULARIZE=1" -s "EXPORT_ES6=1" -s 'EXPORT_NAME="FastTextModule"' -Isrc/
EMOBJS = args.bc autotune.bc matrix.bc dictionary.bc loss.bc productquantizer.bc densematrix.bc quantmatrix.bc vector.bc model.bc utils.bc meter.bc fasttext.bc main.bc

The compiled wasm module is available here. When I run the module in the provided example predict.js I get a

  var _scriptDir = import.meta.url;
                          ^^^^

SyntaxError: Cannot use 'import.meta' outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._compile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)

caused by

var FastTextModule = (function() {
  var _scriptDir = import.meta.url;
  
  return (
function(FastTextModule) {
  FastTextModule = FastTextModule || {};

...

NOTE.

I had to adapt the compiled module from the original to Node.JS in order to support require instead of import, but this should not be related to that error.

UPDATE.

I have tried the flag USE_ES6_IMPORT_META adding to the makefile -s "USE_ES6_IMPORT_META=0" ad described:

em++ --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPostRun', 'FS']" -s "DISABLE_EXCEPTION_CATCHING=0" -s "EXCEPTION_DEBUG=1" -s "FORCE_FILESYSTEM=1" -s "MODULARIZE=1" -s "EXPORT_ES6=1" -s 'EXPORT_NAME="FastTextModule"' -s "USE_ES6_IMPORT_META=0" -Isrc/  src/args.cc -o args.bc
em++: warning: assuming object file output, based on output filename alone.  Add an explict `-c`, `-r` or `-shared` to avoid this warning [-Wemcc]

This time it seems to work, because I get a different error, that is related to the module / code:

TypeError: fastTextModule.addOnPostRun is not a function

offending line is here:

fastTextModule.addOnPostRun(() => {
  if (postRunFunc) {
    postRunFunc();
  }
});

while in the modularized file that function is defined here

Module["addOnPostRun"] = addOnPostRun;

A solution to this problem has been provided here. The final working model is here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Emscripten provide a USE_ES6_IMPORT_META flag! Maybe this can solve your problem. Take a look at https://github.com/emscripten-core/emscripten/blob/master/src/settings.js. There is a simple explanation about this flag.

UPDATE

Use USE_ES6_IMPORT_META=0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...