-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathworker.js
51 lines (46 loc) · 1.58 KB
/
worker.js
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
const {
parentPort, workerData, receiveMessageOnPort, threadId
} = require('worker_threads');
const debug = require('debug')(`worker:${threadId}`);
const prettyBuffer = require('./utils/pretty-buffer');
const imports = require('./runtime/view-only');
async function runWASM({ blockHeight, blockTimestamp, accountBalance, accountLockedBalance, storageUsage, wasmModule, contractId, methodName, methodArgs }) {
debug('runWASM', contractId, methodName, prettyBuffer(Buffer.from(methodArgs)));
// TODO: Take memory size from config
const memory = new WebAssembly.Memory({ initial: 1024, maximum: 2048 });
const ctx = {
registers: {},
blockHeight,
blockTimestamp,
accountBalance,
accountLockedBalance,
storageUsage,
contractId,
methodArgs,
logs: [],
result: Buffer.from([]),
threadId,
parentPort,
receiveMessageOnPort,
};
debug('module instantiate');
const wasmInstance = await WebAssembly.instantiate(wasmModule, { env: { ...imports(ctx), memory } });
debug('module instantiate done');
ctx.memory = memory;
try {
debug(`run ${methodName}`);
wasmInstance.exports[methodName]();
} finally {
debug(`run ${methodName} done`);
}
return ctx;
}
parentPort.on('message', message => {
if (message.wasmModule) {
runWASM(message).then(({ result, logs }) => {
parentPort.postMessage({ result, logs });
}).catch(error => {
parentPort.postMessage({ error, errorCode: error.code });
});
}
});