mirror of
https://github.com/CryptKeeperZK/ffjavascript.git
synced 2026-01-10 00:27:56 -05:00
118 lines
3.2 KiB
JavaScript
118 lines
3.2 KiB
JavaScript
/* global WebAssembly */
|
|
|
|
export default function thread(self) {
|
|
let instance;
|
|
let memory;
|
|
let u32;
|
|
let u8;
|
|
|
|
if (self) {
|
|
self.onmessage = function(e) {
|
|
let data;
|
|
if (e.data) {
|
|
data = e.data;
|
|
} else {
|
|
data = e;
|
|
}
|
|
|
|
if (data[0].cmd == "INIT") {
|
|
init(data[0]).then(function() {
|
|
self.postMessage(data.result);
|
|
});
|
|
} else if (data[0].cmd == "TERMINATE") {
|
|
process.exit();
|
|
} else {
|
|
const res = runTask(data);
|
|
self.postMessage(res);
|
|
}
|
|
};
|
|
}
|
|
|
|
async function init(data) {
|
|
const code = new Uint8Array(data.code);
|
|
const wasmModule = await WebAssembly.compile(code);
|
|
memory = new WebAssembly.Memory({initial:data.init});
|
|
u32 = new Uint32Array(memory.buffer);
|
|
u8 = new Uint8Array(memory.buffer);
|
|
|
|
instance = await WebAssembly.instantiate(wasmModule, {
|
|
env: {
|
|
"memory": memory
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
|
|
function alloc(length) {
|
|
while (u32[0] & 3) u32[0]++; // Return always aligned pointers
|
|
const res = u32[0];
|
|
u32[0] += length;
|
|
while (u32[0] > memory.buffer.byteLength) {
|
|
memory.grow(100);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
function allocBuffer(buffer) {
|
|
const p = alloc(buffer.byteLength);
|
|
setBuffer(p, buffer);
|
|
return p;
|
|
}
|
|
|
|
function getBuffer(pointer, length) {
|
|
return new Uint8Array(u8.buffer, u8.byteOffset + pointer, length);
|
|
}
|
|
|
|
function setBuffer(pointer, buffer) {
|
|
u8.set(new Uint8Array(buffer), pointer);
|
|
}
|
|
|
|
function runTask(task) {
|
|
if (task[0].cmd == "INIT") {
|
|
return init(task[0]);
|
|
}
|
|
const ctx = {
|
|
vars: [],
|
|
out: []
|
|
};
|
|
const oldAlloc = u32[0];
|
|
for (let i=0; i<task.length; i++) {
|
|
switch (task[i].cmd) {
|
|
case "ALLOCSET":
|
|
ctx.vars[task[i].var] = allocBuffer(task[i].buff);
|
|
break;
|
|
case "ALLOC":
|
|
ctx.vars[task[i].var] = alloc(task[i].len);
|
|
break;
|
|
case "SET":
|
|
setBuffer(ctx.vars[task[i].var], task[i].buff);
|
|
break;
|
|
case "CALL": {
|
|
const params = [];
|
|
for (let j=0; j<task[i].params.length; j++) {
|
|
const p = task[i].params[j];
|
|
if (typeof p.var !== "undefined") {
|
|
params.push(ctx.vars[p.var] + (p.offset || 0));
|
|
} else if (typeof p.val != "undefined") {
|
|
params.push(p.val);
|
|
}
|
|
}
|
|
instance.exports[task[i].fnName](...params);
|
|
break;
|
|
}
|
|
case "GET":
|
|
ctx.out[task[i].out] = getBuffer(ctx.vars[task[i].var], task[i].len).slice();
|
|
break;
|
|
default:
|
|
throw new Error("Invalid cmd");
|
|
}
|
|
}
|
|
u32[0] = oldAlloc;
|
|
return ctx.out;
|
|
}
|
|
|
|
|
|
return runTask;
|
|
}
|