| importScripts('./web/promise-worker/promise-worker.register.js'); |
| importScripts('./pkg/loda_rust_web.js'); |
|
|
| delete WebAssembly.instantiateStreaming; |
|
|
| const {WebDependencyManager} = wasm_bindgen; |
|
|
| function sleep(ms) { |
| return new Promise(resolve => setTimeout(resolve, ms)); |
| } |
|
|
| function randomInt(max) { |
| return Math.floor(Math.random() * max); |
| } |
|
|
| class OperationComputeTerm { |
| constructor(index) { |
| this.mIndex = index; |
| } |
|
|
| index() { |
| return this.mIndex; |
| } |
|
|
| accept(visitor) { |
| visitor.visit_compute_term(this); |
| } |
| } |
|
|
| class MyWorker { |
| constructor(dependencyManager, workerId) { |
| this.mDependencyManager = dependencyManager; |
| this.mWorkerId = workerId; |
| this.mRangeStart = 0; |
| this.mRangeLength = 100; |
| this.mResults = []; |
| this.mPendingOperations = []; |
| this.mIsExecutingPendingOperations = false; |
| } |
|
|
| commandSetRange(parameters) { |
| |
| this.mRangeStart = parameters.rangeStart; |
| this.mRangeLength = parameters.rangeLength; |
| } |
|
|
| commandExecuteRange(parameters) { |
| |
| this.mIsExecutingPendingOperations = false; |
| this.mResults = []; |
| this.mPendingOperations = []; |
|
|
| |
| const index0 = this.mRangeStart; |
| const index1 = this.mRangeStart + this.mRangeLength; |
| for (var i = index0; i < index1; i++) { |
| this.mPendingOperations.push(new OperationComputeTerm(i)); |
| } |
|
|
| this.mIsExecutingPendingOperations = true; |
| var self = this; |
| setTimeout(function() { self.commandExecuteRangePost(); }, 0); |
| |
| } |
|
|
| commandExecuteRangePost() { |
| |
| |
| this.pickFirstPendingOperation(); |
| } |
|
|
| pickFirstPendingOperation() { |
| |
| if (!this.mIsExecutingPendingOperations) { |
| console.log("worker", this.mWorkerId, "- stop running"); |
| return; |
| } |
| const operation = this.mPendingOperations.shift(); |
| if (typeof (operation) === 'undefined') { |
| |
| this.mIsExecutingPendingOperations = false; |
| return; |
| } |
| |
|
|
| operation.accept(this); |
|
|
| |
| var self = this; |
| setTimeout(function() { self.pickFirstPendingOperation(); }, 0); |
| } |
|
|
| visit_compute_term(operation_compute_term) { |
| const index = operation_compute_term.index(); |
| var dict = {}; |
| dict["index"] = index; |
|
|
| try { |
| const valueString = this.mDependencyManager.clone().execute_current_program(index); |
| |
| dict["value"] = valueString; |
| this.mResults.push(dict); |
| } |
| catch(err) { |
| console.log("Exception inside execute_current_program: ", err); |
| dict["error"] = `ERROR: ${err}`; |
| this.mResults.push(dict); |
|
|
| |
|
|
| |
| this.mIsExecutingPendingOperations = false; |
|
|
| |
| this.mPendingOperations = []; |
| } |
| } |
|
|
| commandTakeResult(parameters) { |
| |
| const termsArray = this.mResults; |
| this.mResults = []; |
|
|
| var responseDictionary = {}; |
| responseDictionary["terms"] = termsArray; |
|
|
| |
| |
| responseDictionary["isExecuting"] = this.mIsExecutingPendingOperations; |
| return responseDictionary; |
| } |
|
|
| async commandCompile(parameters) { |
| |
| |
| this.mResults = []; |
|
|
| |
| this.mIsExecutingPendingOperations = false; |
|
|
| |
| this.mPendingOperations = []; |
|
|
| const sourceCode = parameters.sourceCode; |
| try { |
| await this.mDependencyManager.clone().run_source_code(sourceCode); |
| } catch (error_message) { |
| throw new Error(error_message); |
| } |
| |
| } |
|
|
| commandStop(parameters) { |
| |
| this.mResults = []; |
|
|
| |
| this.mIsExecutingPendingOperations = false; |
|
|
| |
| this.mPendingOperations = []; |
| } |
| } |
|
|
| async function init_worker() { |
| const workerId = randomInt(1000000); |
| console.log("init_worker", workerId); |
|
|
| const wasmModule = await wasm_bindgen('./pkg/loda_rust_web_bg.wasm'); |
|
|
| |
|
|
| wasmModule.setup_lib(); |
|
|
| |
|
|
| wasmModule.perform_selfcheck(); |
|
|
| |
|
|
| const dm = new WebDependencyManager(); |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
|
|
| |
|
|
| const myWorker = new MyWorker(dm, workerId); |
|
|
| registerPromiseWorker(async function (e) { |
| switch (e.fn) { |
| case "setrange": |
| myWorker.commandSetRange(e); |
| break; |
| case "executerange": |
| myWorker.commandExecuteRange(e); |
| break; |
| case "compile": |
| await myWorker.commandCompile(e); |
| break; |
| case "takeresult": |
| return myWorker.commandTakeResult(e); |
| case "stop": |
| await myWorker.commandStop(e); |
| break; |
| default: |
| throw Error(`worker.message: unknown: ${e}`); |
| } |
| }); |
|
|
| |
| postMessage({ |
| fn: "init", |
| value: true |
| }); |
| } |
|
|
| init_worker() |
| .catch(e => { |
| console.log('There has been a problem: ' + e.message); |
|
|
| |
| postMessage({ |
| fn: "init", |
| value: false, |
| reason: "failed to fetch and instantiate the WASM" |
| }); |
| }); |