-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryCell.js
126 lines (123 loc) · 3.18 KB
/
memoryCell.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
export default {
template: `<div :class="'memory-cell ' + 'memory-cell-' + variable[1][1] + ' ' + (isPointer ? 'pointer' : variable[1][2])">
<div class="address">{{ address }}</div>
<div class="content">{{ content }}</div>
<div class="type">{{ type }}</div>
<div class="name">{{ variable[0] }}</div>
</div>`,
data() {
return {
componentName: 'MemoryCell',
};
},
props: {
variable: {
type: [String, [String]],
required: true,
}
},
mounted() {
if(this.isPointer) {
this.createPointerArrow();
}
},
updated() {
if(this.isPointer) {
if(globalArrows.has(this.address + this.content)) {
globalArrows.get(this.address + this.content).remove();
}
this.createPointerArrow();
}
},
unmounted() {
if(this.isPointer) {
if(globalArrows.has(this.address + this.content)) {
globalArrows.get(this.address + this.content).remove();
}
}
},
methods: {
createPointerArrow() {
if(this.content !== '?') {
const pointerCell = document.querySelector('.memory-cell-' + this.address);
const pointedCell = document.querySelector('.memory-cell-' + this.content);
if(pointedCell !== null) {
const isPointedCellHeap = pointedCell.parentElement.id === 'heap';
globalArrows.set(
this.address + this.content,
new LeaderLine(
pointerCell,
pointedCell,
{
path: 'fluid',
startSocket: 'right',
endSocket: isPointedCellHeap? (this.isInHeap? 'right' : 'left') : 'right',
startSocketGravity: isPointedCellHeap? [15, 0] : [15, 0],
endSocketGravity: isPointedCellHeap? (this.isInHeap? [20, 0] : [-20, 0]) : [20, 0],
endPlug: 'arrow3',
size: 2,
endPlugSize: 2,
color: getComputedStyle(document.body).getPropertyValue('--cell-border')
}
)
);
}
}
}
},
computed: {
type() {
if (this.isInHeap) {
if(this.isArray) {
return this.variable[1][2][2];
} else {
return this.variable[1][2];
}
} else {
if(this.variable[1][0] === "C_ARRAY") {
return this.variable[1][2][2] + '[]';
} else {
return this.variable[1][2];
}
}
},
content() {
if (this.isArray) {
return this.variable[1][2][3];
} else {
return this.variable[1][3];
}
},
address() {
return this.variable[1][1];
},
isInHeap() {
return this.variable[2];
},
isArray() {
return this.variable[1][2].constructor === Array;
},
isPointer() {
return this.isArray? this.variable[1][2][2][0] === '*' || this.variable[1][2][2] === 'pointer' :
this.variable[1][2][0] === '*' || this.variable[1][2] === 'pointer';
}
},
watch: {
variable:{
handler(newVal, oldVal) {
const newAddress = newVal[1][1];
const newContent = newVal[1][2].constructor === Array? newVal[1][2][3] : newVal[1][3];
const oldAddress = oldVal[1][1];
const oldContent = oldVal[1][2].constructor === Array? oldVal[1][2][3] : oldVal[1][3];
if(newAddress !== oldAddress || newContent !== oldContent) {
const oldKey = oldAddress + oldContent;
if(globalArrows.has(oldKey)) {
globalArrows.get(oldKey).remove();
globalArrows.delete(oldKey);
}
}
},
deep: true
}
}
}