Skip to content

Commit

Permalink
Catch more error cases in the wasm translation worker
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmervdl committed Jul 14, 2022
1 parent 2427d9c commit c572ad2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
8 changes: 7 additions & 1 deletion extension/controller/backgroundScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class Tab extends EventTarget {
// language. We leave to selected as is
pendingTranslationRequests: 0,
totalTranslationRequests: 0,
state: State.PAGE_LOADING
state: State.PAGE_LOADING,
error: null
};
}
});
Expand Down Expand Up @@ -382,6 +383,11 @@ let provider = new class {
this.#provider.onerror = err => {
console.error('Translation provider error:', err);

tabs.forEach(tab => tab.update(() => ({
state: State.PAGE_ERROR,
error: `Translation provider error: ${err.message}`,
})));

// Try falling back to WASM is the current provider doesn't work
// out. Might lose some translations the process but
// InPageTranslation should be able to deal with that.
Expand Down
14 changes: 9 additions & 5 deletions extension/controller/translation/WASMTranslationHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,15 @@ class WorkerChannel {

// No worker free, but space for more?
if (!worker && this.workers.length < this.workerLimit) {
worker = {
idle: true,
worker: this.loadWorker()
};
this.workers.push(worker);
try {
worker = {
idle: true,
worker: this.loadWorker()
};
this.workers.push(worker);
} catch (e) {
this.onerror(new Error(`Could not initialise translation worker: ${e.message}`));
}
}

// If no worker, that's the end of it.
Expand Down
49 changes: 29 additions & 20 deletions extension/controller/translation/WASMTranslationWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,33 @@ class WASMTranslationWorker {
*/
loadModule() {
return new Promise(async (resolve, reject) => {
const response = await fetch('bergamot-translator-worker.wasm');

Object.assign(Module, {
instantiateWasm: (info, accept) => {
WebAssembly.instantiateStreaming(response, {
...info,
'wasm_gemm': this.options.useNativeIntGemm
? this.linkNativeIntGemm(info)
: this.linkFallbackIntGemm(info)
}).then(({instance}) => accept(instance));
return {};
},
onRuntimeInitialized: () => {
resolve(Module);
}
});

// Emscripten glue code
importScripts('bergamot-translator-worker.js');
try {
const response = await fetch('bergamot-translator-worker.wasm');

Object.assign(Module, {
instantiateWasm: (info, accept) => {
try {
WebAssembly.instantiateStreaming(response, {
...info,
'wasm_gemm': this.options.useNativeIntGemm
? this.linkNativeIntGemm(info)
: this.linkFallbackIntGemm(info)
}).then(({instance}) => accept(instance)).catch(reject);
} catch (err) {
reject(err);
}
return {};
},
onRuntimeInitialized: () => {
resolve(Module);
}
});

// Emscripten glue code
importScripts('bergamot-translator-worker.js');
} catch (err) {
reject(err);
}
});
}

Expand Down Expand Up @@ -219,6 +227,7 @@ class WASMTranslationWorker {
}
}


onmessage = ({data}) => {
if (!data.options){
console.warn('Did not receive initial message with options');
Expand All @@ -233,7 +242,7 @@ onmessage = ({data}) => {
const result = await worker[message.name](...message.args);
postMessage({id, message: result});
} catch (err) {
console.error(err);
console.error('Worker runtime error', err);
postMessage({
id,
error: {
Expand Down

0 comments on commit c572ad2

Please sign in to comment.