File size: 7,108 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
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) {
// console.log("commandSetRange");
this.mRangeStart = parameters.rangeStart;
this.mRangeLength = parameters.rangeLength;
}
commandExecuteRange(parameters) {
// console.log("worker", this.mWorkerId, "- commandExecuteRange before");
this.mIsExecutingPendingOperations = false;
this.mResults = [];
this.mPendingOperations = [];
// Enqueue operations for this range
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);
// console.log("worker", this.mWorkerId, "- commandExecuteRange after");
}
commandExecuteRangePost() {
// console.log("commandExecuteRangePost - start executing pending operations");
// Begin executing pending items from queue
this.pickFirstPendingOperation();
}
pickFirstPendingOperation() {
// console.log("pickFirstPendingOperation");
if (!this.mIsExecutingPendingOperations) {
console.log("worker", this.mWorkerId, "- stop running");
return;
}
const operation = this.mPendingOperations.shift();
if (typeof (operation) === 'undefined') {
// console.log("pickFirstPendingOperation - no more pending operations - stopping");
this.mIsExecutingPendingOperations = false;
return;
}
// console.log("pickFirstPendingOperation - will execute", operation);
operation.accept(this);
// Continue executing pending items from queue
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);
// console.log("computed value", valueString, "for index", 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);
// Stop execution of following terms
// Indicate that no execution is going on
this.mIsExecutingPendingOperations = false;
// Remove pending operations
this.mPendingOperations = [];
}
}
commandTakeResult(parameters) {
// console.log("commandTakeResult");
const termsArray = this.mResults;
this.mResults = [];
var responseDictionary = {};
responseDictionary["terms"] = termsArray;
// If still executing, then set true, so the UI knows there is more data to come.
// If execute has stopped, then set to false, so the UI stops refreshing.
responseDictionary["isExecuting"] = this.mIsExecutingPendingOperations;
return responseDictionary;
}
async commandCompile(parameters) {
// console.log("worker", this.mWorkerId, "- commandCompile before");
// Discard old results
this.mResults = [];
// Indicate that a new execute is going on
this.mIsExecutingPendingOperations = false;
// Remove pending operations
this.mPendingOperations = [];
const sourceCode = parameters.sourceCode;
try {
await this.mDependencyManager.clone().run_source_code(sourceCode);
} catch (error_message) {
throw new Error(error_message);
}
// console.log("worker", this.mWorkerId, "- commandCompile after");
}
commandStop(parameters) {
// Discard old results
this.mResults = [];
// Indicate that no execution is going on
this.mIsExecutingPendingOperations = false;
// Remove pending operations
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');
// console.log("init_worker 2");
wasmModule.setup_lib();
// console.log("init_worker 3");
wasmModule.perform_selfcheck();
// console.log("init_worker 4");
const dm = new WebDependencyManager();
// dm.increment();
// await dm.clone().run_source_code("mov $1,2\npow $1,$0");
// await dm.clone().run_source_code("seq $0,40\nmul $0,-1");
// const index = 2;
// const value = await dm.clone().execute_current_program(index);
// console.log("computed value: ", value);
// dm.clone().print_stats();
// throw new Error("Demo of an exception");
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}`);
}
});
// let things know that we are ready to accept commands:
postMessage({
fn: "init",
value: true
});
}
init_worker()
.catch(e => {
console.log('There has been a problem: ' + e.message);
// let things know that we failed to initialise the WASM:
postMessage({
fn: "init",
value: false,
reason: "failed to fetch and instantiate the WASM"
});
}); |