WASM 和 Node.js 不能在模块外使用 'import.meta'

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

我使用提供的 make 文件将 FastText C++ 模块构建为 wasm 模块,即使用以下标志:

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

编译好的wasm模块可用here. When I run the module in the provided example predict.js我得到一个

  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)

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

...

注意。

为了支持require而不是import,我不得不将编译后的模块从原来的修改为Node.JS,但这应该与那个错误无关。

更新。

我已经尝试将标志 USE_ES6_IMPORT_META 添加到 makefile -s "USE_ES6_IMPORT_META=0" 广告中描述:

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]

这次似乎成功了,因为我得到了一个不同的错误,它与模块/代码有关:

TypeError: fastTextModule.addOnPostRun is not a function

违规行是 here:

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

而在定义函数的模块化文件中here

Module["addOnPostRun"] = addOnPostRun;

已提供此问题的解决方案 here. The final working model is here

Emscripten 提供了一个 USE_ES6_IMPORT_META 标志!也许这可以解决您的问题。 看看https://github.com/emscripten-core/emscripten/blob/master/src/settings.js。关于这个标志有一个简单的解释。

更新

使用USE_ES6_IMPORT_META=0