-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprocess.ts
More file actions
148 lines (125 loc) · 3.52 KB
/
process.ts
File metadata and controls
148 lines (125 loc) · 3.52 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
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
import {PID} from 'erlang-types'
import States from './states'
import Mailbox from './mailbox'
import System from './process_system'
function is_sleep(value: any) {
return Array.isArray(value) && value[0] === States.SLEEP
}
function is_receive(value: any) {
return Array.isArray(value) && value[0] === States.RECEIVE
}
function receive_timed_out(value: any) {
return value[2] != null && value[2] < Date.now()
}
class Process {
pid: PID
func: Function
args: any[]
mailbox: Mailbox
system: System
status: symbol
dict: Map<any, any>
flags: Map<symbol, any>
monitors: any[]
constructor(system: System, func: Function, args: any[]) {
this.system = system
this.func = func
this.args = args
this.status = States.STOPPED
this.pid = new PID()
this.mailbox = new Mailbox()
this.dict = new Map()
this.flags = new Map()
this.monitors = []
}
start() {
const function_scope = this
let machine = this.main()
this.system.schedule(function() {
function_scope.system.set_current(function_scope.pid)
function_scope.run(machine, machine.next())
}, this.pid)
}
*main() {
let retval = States.NORMAL
try {
yield* this.func.apply(null, this.args)
} catch (e) {
console.error(e)
retval = e
}
this.system.exit(retval)
}
process_flag(flag: symbol, value: any): any {
const old_value = this.flags.get(flag)
this.flags.set(flag, value)
return old_value
}
is_trapping_exits(): boolean {
return (
this.flags.has(Symbol.for('trap_exit')) &&
this.flags.get(Symbol.for('trap_exit')) == true
)
}
signal(reason: any): void {
if (reason !== States.NORMAL) {
console.error(reason)
}
this.system.remove_proc(this.pid, reason)
}
receive(fun: Function) {
let value = States.NOMATCH
let messages = this.mailbox.get()
for (let i = 0; i < messages.length; i++) {
try {
value = fun(messages[i])
if (value !== States.NOMATCH) {
this.mailbox.removeAt(i)
break
}
} catch (e) {
if (e.constructor.name != 'MatchError') {
this.system.exit(e)
}
}
}
return value
}
run(machine: Generator, step: any): void {
const function_scope = this
if (!step.done) {
let value = step.value
if (is_sleep(value)) {
this.system.delay(function() {
function_scope.system.set_current(function_scope.pid)
function_scope.run(machine, machine.next())
}, value[1])
} else if (is_receive(value) && receive_timed_out(value)) {
let result = value[3]()
this.system.schedule(function() {
function_scope.system.set_current(function_scope.pid)
function_scope.run(machine, machine.next(result))
})
} else if (is_receive(value)) {
let result = function_scope.receive(value[1])
if (result === States.NOMATCH) {
this.system.suspend(function() {
function_scope.system.set_current(function_scope.pid)
function_scope.run(machine, step)
})
} else {
this.system.schedule(function() {
function_scope.system.set_current(function_scope.pid)
function_scope.run(machine, machine.next(result))
})
}
} else {
this.system.schedule(function() {
function_scope.system.set_current(function_scope.pid)
function_scope.run(machine, machine.next(value))
})
}
}
}
}
export default Process