This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Env.js
71 lines (63 loc) · 1.93 KB
/
Env.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
'use strict';
let nextEnvId = 0;
class Env {
constructor(sourceLoc, callerEnv, programOrSendEvent) {
this.id = nextEnvId++;
this.sourceLoc = sourceLoc;
this.callerEnv = callerEnv;
this.programOrSendEvent = programOrSendEvent;
this.microVizEvents = new MicroVizEvents(programOrSendEvent, sourceLoc);
this.programOrSendEventToMicroVizEvents = new Map([[programOrSendEvent, this.microVizEvents]]);
}
receive(event) {
this.maybeAdd(event);
if (this.callerEnv && this.shouldBubbleUp(event)) {
this.callerEnv.receive(event);
}
}
maybeAdd(event) {
const programOrSendEvent = this.targetProgramOrSendEventFor(event);
if (!programOrSendEvent) {
return;
}
const microVizEvents = this.programOrSendEventToMicroVizEvents.get(programOrSendEvent);
if (event instanceof SendEvent) {
const newMicroVizEvents = new MicroVizEvents(event, event.sourceLoc);
this.programOrSendEventToMicroVizEvents.set(event, newMicroVizEvents);
microVizEvents.add(newMicroVizEvents);
} else {
microVizEvents.add(event);
}
}
targetProgramOrSendEventFor(event) {
if (event.env === this) {
return this.programOrSendEvent;
}
let env = event.env;
while (env !== this) {
const sendEvent = env.programOrSendEvent;
if (this.programOrSendEventToMicroVizEvents.has(sendEvent)) {
if (this.shouldOnlyShowWhenLocal(event)) {
return sendEvent.sourceLoc.strictlyContains(event.sourceLoc) ? sendEvent : null;
} else {
return sendEvent;
}
} else {
env = env.callerEnv;
}
}
return null;
}
shouldOnlyShowWhenLocal(event) {
return event instanceof SendEvent ||
event instanceof ReturnEvent ||
event instanceof VarDeclEvent;
}
shouldBubbleUp(event) {
if (event instanceof VarAssignmentEvent) {
return this !== event.declEnv;
} else {
return true;
}
}
}