browsermt / bergamot-translator

Cross platform C++ library focusing on optimized machine translation on the consumer-grade device.
http://browser.mt
Mozilla Public License 2.0
327 stars 36 forks source link

Confusing error message from WebAssembly + JS #426

Open jerinphilip opened 2 years ago

jerinphilip commented 2 years ago

I'm trying to get an assert working for #425; Despite no issues in WebAssembly, the following error message is dumped:

Using fallback gemm implementation
 <p>Hallo Welt!</p>   <p>Wiedersehen Welt!</p> 
string
DocumentFragment {}
<p>Hallo Welt!</p>
Aborted(undefined)
undefined:448
 var e = new WebAssembly.RuntimeError(what);
         ^

RuntimeError: Aborted(undefined). Build with -s ASSERTIONS=1 for more info.
    at abort (eval at <anonymous> (/home/jerin/code/bergamot-translator/build-wasm/node-test.js:1:1), <anonymous>:448:10)
    at assert (eval at <anonymous> (/home/jerin/code/bergamot-translator/build-wasm/node-test.js:1:1), <anonymous>:116:3)
    at Object.onRuntimeInitialized (/home/jerin/code/bergamot-translator/build-wasm/node-test.js:112:3)

The following is the relevant source:

  ...
  // Construct std::vector<std::string> inputs;
  const input = new Module.VectorString();
  input.push_back('<p> Hello world! </p> <p> Goodbye World! </p>');

  // Construct std::vector<ResponseOptions>
  const options = new Module.VectorResponseOptions();
  options.push_back({qualityScores: false, alignment: true, html: true});

  // Translate our batch (of 1)
  const output = service.translate(model, input, options);

  // Get output from std::vector<Response>
  const translation = output.get(0).getTranslatedText()
  console.log(translation)
  console.log(typeof (translation));

  const fragment = JSDOM.fragment(translation)
  console.log(fragment)
  console.log(fragment.firstElementChild.outerHTML)
  assert(fragment.childElementCount === 1); // This fails, because 2, and has nothing to do with WebAssembly?
jelmervdl commented 2 years ago

This happens because assert() is defined by bergamot-translation-worker.js, the Emscripten runtime code:

function assert(condition, text) {
 if (!condition) {
  abort(text);
 }
}

// ...

function abort(what) {
 what = "Aborted(" + what + ")";
 err(what);
 ABORT = true;
 EXITSTATUS = 1;
 what += ". Build with -s ASSERTIONS=1 for more info.";
 var e = new WebAssembly.RuntimeError(what);
 throw e;
}

You could replace assert() with console.assert() which is the more common API for it in Javascript. Downside: console.assert just prints a message but does not stop the program from continuing. So it isn't really equivalent.