-
Notifications
You must be signed in to change notification settings - Fork 4
/
pathmatcher.js
54 lines (48 loc) · 1.21 KB
/
pathmatcher.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
class PathMatcher {
constructor(path, env = null) {
this.path = path;
this.idx = env ? path.length : 0;
this.envAtIdx = env;
}
reset(globalEnv) {
this.idx = 0;
this.envAtIdx = globalEnv;
}
get env() {
return this.idx === this.path.length ? this.envAtIdx : null;
}
processEvent(child, parent) {
console.assert(this.idx < this.path.length);
if (this.envAtIdx === parent.activationEnv &&
child.activationPathToken === this.path[this.idx]) {
this.idx++;
this.envAtIdx = child.activationEnv;
}
}
}
function getPathMatchers(activationEnv) {
const pathMatchers = [];
let env = activationEnv;
while (env) {
pathMatchers.unshift(getPathMatcher(env));
env = env.parentEnv;
}
return pathMatchers;
}
function getPathMatcher(activationEnv) {
return new PathMatcher(getPath(activationEnv), activationEnv);
}
function getPath(activationEnv) {
const path = [];
while (true) {
const callerEnv = activationEnv.callerEnv;
if (callerEnv) {
path.push(activationEnv.programOrSendEvent.activationPathToken);
activationEnv = callerEnv.programOrSendEvent.activationEnv;
} else {
break;
}
}
path.reverse();
return path;
}