Repository navigation
Expand file tree
/
Copy pathFiber.js
More file actions
75 lines (75 loc) · 2.29 KB
/
Copy pathFiber.js
File metadata and controls
75 lines (75 loc) · 2.29 KB
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
import { PHPReference } from "./PHPVariable.js";
import { SYMBOL_PHP_NEW } from "./Reflection.js";
export class PHPFiberError extends Error {
}
export class PHPFiberExit extends Error {
}
export class PHPFiber {
callback;
running = false;
started = false;
terminated = false;
suspended = false;
value = undefined;
constructor(callback) {
if (callback)
this.callback = callback;
}
static async [SYMBOL_PHP_NEW](ctx, callbackArg) {
const obj = Object.create(this.prototype);
obj.running = false;
obj.started = false;
obj.terminated = false;
obj.suspended = false;
obj.value = undefined;
await obj.__construct(ctx, callbackArg);
return obj;
}
async __construct(ctx, callbackArg) {
const cb = await callbackArg?.get();
this.callback = cb;
}
async start(ctx, ...args) {
if (this.started)
throw new PHPFiberError("Fiber has already been started");
this.started = true;
this.running = true;
try {
const cb = this.callback instanceof PHPReference ? await this.callback.get() : this.callback;
if (typeof cb === "function") {
this.value = await cb(ctx, ...args);
}
else if (ctx && typeof ctx.callUserFunction === "function") {
this.value = await ctx.callUserFunction(cb, args);
}
this.terminated = true;
this.running = false;
return this.value;
}
catch (err) {
this.terminated = true;
this.running = false;
throw err;
}
}
isStarted() { return this.started; }
isstarted() { return this.isStarted(); }
isRunning() { return this.running; }
isrunning() { return this.isRunning(); }
isSuspended() { return this.suspended; }
issuspended() { return this.isSuspended(); }
isTerminated() { return this.terminated; }
isterminated() { return this.isTerminated(); }
static async suspend(value = undefined) {
return value;
}
}
export class FiberRuntime {
static classes = {
"fiber": PHPFiber,
};
static register(engine) {
engine.registerClasses(FiberRuntime.classes);
}
}
//# sourceMappingURL=Fiber.js.map