-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasc.worker.ts
156 lines (136 loc) · 4.58 KB
/
Wasc.worker.ts
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
/**
* @author hexxone / https://hexx.one
*
* @license
* Copyright (c) 2024 hexxone All rights reserved.
* Licensed under the GNU GENERAL PUBLIC LICENSE.
* See LICENSE file in the project root for full license information.
*
* @description
* Web-AssemblyScript Worker
*/
'use strict';
import { ASUtil } from './WascInterface';
import { WascLoader } from './WascLoader';
import { WascUtil } from './WascUtil';
const wascw = self as any;
/** For shared workers:
* Cross-Origin-Opener-Policy: same-origin
* Cross-Origin-Embedder-Policy: require-corp
* @public
*/
let ascImports: WebAssembly.Imports;
let ascInstance: WebAssembly.Instance;
let ascModule: WebAssembly.Module;
let ascExports: ASUtil;
wascw.addEventListener('message', (e) => {
// console.log('wasc-message', e.data);
const { id, action, payload, getImportObject, memOpts } = e.data;
/**
* @param {number} result worker success = 0 | error = 1
* @param {Object} data worker result | error msg
* @returns {void}
* @public
*/
const sendMessage = (result: number, data: any) => {
wascw.postMessage(
{
id,
action,
result,
payload: data
},
WascUtil.getTransferableParams(data)
);
};
const onError = (ex) => {
return sendMessage(1, ex);
};
const onSuccess = (res) => {
return sendMessage(0, res);
};
const { source, func, params } = payload;
switch (action) {
// get & compile the module, then export functions
case WascUtil.ACTIONS.COMPILE_MODULE:
Promise.resolve()
.then(async () => {
// use shared memory, or create our own
const memory = new WebAssembly.Memory(memOpts);
/**
* @public
*/
ascImports = {
env: {
memory,
logf: (value) => {
console.log(`F64: ${value}`);
},
logi: (value) => {
console.log(`U32: ${value}`);
}
}
};
// apply additional import object
if (getImportObject instanceof Object) {
ascImports = Object.assign(ascImports, getImportObject);
}
// make webassembly
const byteModule = await WascUtil.myFetch(source);
const inst = await new WascLoader().instantiate(
byteModule,
ascImports
);
// remembr module
ascModule = inst.module;
ascInstance = inst.instance;
ascExports = inst.exports;
/**
* return available methods as strings to main ctx
* @public
*/
onSuccess({
exports: Object.keys(ascExports),
sharedMemory: memOpts.shared ? memory : null
});
})
.catch(onError);
break;
// run an exported function with parameters
case WascUtil.ACTIONS.CALL_FUNCTION_EXPORT:
Promise.resolve()
// eslint-disable-next-line prefer-spread
.then(() => {
return onSuccess(
ascExports[func](...params)
);
})
.catch(onError);
break;
// run a custom function with parameters
case WascUtil.ACTIONS.RUN_FUNCTION:
Promise.resolve()
.then(() => {
const fun = new Function(`return ${func}`)();
/**
* @public
*/
onSuccess(
fun({
module: ascModule,
instance: ascInstance,
exports: ascExports,
params
})
);
})
.catch(onError);
break;
// protocol error
default:
console.error(
`[wasc-worker] Unknown action: ${JSON.stringify(e.data)}`
);
break;
}
});