-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
287 lines (270 loc) · 10.3 KB
/
index.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(function () {
let __id = 0;
let objId = new Map;
const getObjId = obj => objId.get(obj) || (objId.set(obj, ++__id) && __id);
const isNode = typeof window === "undefined" && typeof require !== "undefined";
const _global = isNode ? global : (typeof window === "undefined" ? {} : window);
const _require = n => isNode ? require(n) : {}; // maybe this fixes the possible jsx issues?
const {Worker: NodeWorker} = _require("worker_threads");
const crypto_ = _global.crypto ?? _require("crypto");
const path = _require("path");
function isClass(func) {
return typeof func === "function" && func.prototype && func.prototype.constructor === func;
}
function isBuiltInClass(cl) {
return _global[cl.name] === cl && !Object.keys(_global).includes(cl.name); // IDK why globals work like this
}
function doAllowAny(any, list, p) {
const type = typeof any;
if (["undefined", "boolean", "number", "bigint", "string", "symbol"].includes(type) || any === null) {
return any;
}
if (type === "object") {
if (any instanceof ArrayBuffer) {
list.push(`${p}=new Int8Array(${p}).buffer;`);
return [...new Int8Array(any)];
}
if (any.constructor instanceof Function && !isBuiltInClass(any.constructor)) {
if (!objId.has(any.constructor)) {
const id = getObjId(any.constructor);
doAllowAny(any.constructor, list, `arguments_[2]._${id}`);
}
const id = getObjId(any.constructor);
list.push(`${p}=Object.assign(new arguments_[2]._${id}(),${p});`);
return {...any};
}
for (const i in any) {
any[i] = doAllowAny(any[i], list, p + `[${JSON.stringify(i)}]`);
}
return any;
}
if (type !== "function") throw new Error("Invalid type: " + type);
if (isBuiltInClass(any)) {
list.push(`${p}=${any.name};`);
return 0;
}
const prototype = Object.getPrototypeOf(any);
let text = stringifyFunction(any);
if (isClass(any) && isClass(prototype)) {
if (!objId.has(prototype)) {
const id = getObjId(prototype);
doAllowAny(prototype, list, `arguments_[2]._${id}`);
}
const id = getObjId(prototype);
text = text.replace(new RegExp(`extends\\s+${prototype.name}\\s*\\{`), `extends arguments_[2]._${id}{`);
}
list.push(`${p}=${text};`);
return 0;
}
function stringifyFunction(func) {
if (isClass(func)) return `(function(){return ${func.toString().trim()}})()`;
func = func.toString().trim();
const isAsync = func.startsWith("async ");
if (isAsync) func = func.substring("async ".length).trim();
let startsWithFunction = func.startsWith("function ");
// ABC (abc) { return "ABC" }
if (!startsWithFunction && /^[^(=]+\s*\(/.test(func)) return `function ${func}`;
// (abc) => "ABC"
// abc => "ABC"
if (!startsWithFunction) {
let id = ++__id;
return `function(...args${id}){return(${func})(...args${id})}`;
}
// function ABC () { return "ABC" }
return `(()=>{return ${func}})()`;
}
function runnerProcessor(worker, func, thread) {
return [worker, (args = [], define = {}, allowAny = true, autoTermination = true) => {
const p = new Promise(r => {
const onMessage = msg => {
if (!msg || msg[0] !== thread.__uuid__) return;
if (msg[1] === "end") {
r(msg[2]);
if (autoTermination) worker.terminate();
return;
}
if (msg[1] === "sendTo") return Thread.sendTo(msg[2], msg[3]);
if (msg[1] === "broadcast") return Thread.broadcast(msg[2]);
if (msg[1] === "broadcastToChannel") return thread.parent.broadcastToChannel(msg[2]);
};
const onTerminate = () => r();
if (isNode) {
worker.on("message", onMessage);
worker.on("exit", onTerminate);
} else {
worker.addEventListener("message", m => onMessage(m.data));
worker.addEventListener("close", onTerminate)
}
});
p.terminate = () => thread.terminate();
const definitionReplacements = [];
if (allowAny) {
doAllowAny(args, definitionReplacements, "arguments_[0]");
doAllowAny(define, definitionReplacements, "arguments_[1]");
}
// noinspection JSAnnotator
const arguments_ = [args, define, {}];
const code = `${definitionReplacements.join("")}
const {${Object.keys(define).join(",")}} = arguments_[1];
const Thread = {
sendTo(id, msg) {
postMessage(["${thread.__uuid__}", "sendTo", id, msg]);
},
broadcast(msg) {
postMessage(["${thread.__uuid__}", "broadcast", msg]);
},
broadcastToChannel(msg) {
postMessage(["${thread.__uuid__}", "broadcastToChannel", msg]);
},
isMainThread: false
};
return (async () => (
// YOUR CODE STARTS HERE...
${stringifyFunction(func)}
// YOUR CODE ENDS HERE...
)(...arguments_[0]))()`;
worker.postMessage([code, arguments_, thread.__uuid__]);
return p;
}];
}
function runnerNodeRaw(func, thread) {
let dir = __dirname;
if (path.dirname(dir) === "build") dir = path.join(dir, "..");
return runnerProcessor(new NodeWorker(path.join(dir, "worker.js")), func, thread);
}
function runnerWebRaw(func, thread) {
const worker = new Worker(URL.createObjectURL(new Blob([`
let uuid;
async function cb(args) {
args = args.data;
uuid = uuid || args[2];
if (!args || uuid !== args[2] || args.length !== 3) return;
postMessage([args[2], "end", await Function("arguments_", args[0])(args[1])]);
}
addEventListener("message", cb);
`.replaceAll("\n", "")], {
type: "application/javascript"
})));
return runnerProcessor(worker, func, thread);
}
const runner = isNode ? runnerNodeRaw : runnerWebRaw;
function makeChannel(parent = null) {
function Channel(func) {
const id = ++__id;
const uuid = crypto_.randomUUID();
const out = (...args) => thread.run(...args);
let allowsAny = true;
let definitions = {};
let autoTermination = true;
const thread = {
__parents: [],
get __uuid__() {
return uuid;
},
get id() {
return id;
},
get allowsAny() {
return allowsAny;
},
get autoTermination() {
return autoTermination;
},
get definitions() {
return definitions;
},
get worker() {
return worker;
},
get isAlive() {
return !!Thread.threads[id];
},
get parent() {
return Channel;
},
setAllowsAny(value = true) {
allowsAny = value;
return this;
},
setAutoTermination(value = true) {
autoTermination = value;
return this;
},
define(obj) {
Object.assign(definitions, obj);
return this;
},
use(obj) {
this.define(obj);
return this;
},
run(...args) {
return run(args, definitions, allowsAny, autoTermination)
},
terminate() {
Channel.terminate(id);
return this;
},
send(msg) {
worker.postMessage(msg);
}
};
Channel.addChild(thread);
const [worker, run] = runner(func, thread);
return Object.assign(out, thread);
}
Channel.channel = function () {
return makeChannel(Channel);
};
Channel.parent = parent;
Channel.addChild = function (child) {
child.__parents.push(Channel);
Channel.threads[child.id] = child;
if (Channel.parent) Channel.parent.addChild(child);
};
Channel.prepare = Channel;
Channel.Thread = Channel;
Channel.find = function (id) {
return Channel.threads[id] ?? null;
};
Channel.sendTo = function (id, msg) {
const thread = Thread.threads[id];
if (!thread) return;
thread.send(msg);
return this;
};
Channel.broadcast = function (msg) {
for (const id in Channel.threads) Channel.threads[id].send(msg);
return this;
};
Channel.broadcastToChannel = function (msg) {
return Channel.broadcast(msg);
};
Channel.terminate = function (id) {
const thread = Thread.threads[id];
if (!thread) return;
thread.worker.terminate();
for (const par of thread.__parents) delete par.threads[id];
return this;
};
Channel.threads = {};
Channel.immediate = function (cb, ...args) {
return Channel(cb)(...args);
};
Channel.isMainThread = true;
return Channel;
}
const Thread = makeChannel();
/*Thread.globalize = function () {
Function.prototype.thread = function (...args) {
return Thread.immediate(this, ...args);
};
return Thread;
};*/
//@buildExport//
if (isNode) module.exports = Thread; else Object.defineProperty(_global, "Thread", {
get: () => Thread,
enumerable: false, configurable: false
});
//@buildExport//
})();