-
Notifications
You must be signed in to change notification settings - Fork 1
/
exval.js
143 lines (113 loc) · 3.25 KB
/
exval.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const CodeMap = require('./code-map');
const Encoder = require('./encoder');
// const FUNC_MAX_ARGUMENTS = 2048;
class Exval {
constructor(opts = {}) {
if (!(this instanceof Exval)) return new Exval(opts);
this.opts = Object.assign({}, opts);
this.codeMap = new CodeMap(opts);
}
stringify(val) {
const encoder = new Encoder(this.opts);
const valPath = this.codeMap.get(val);
const left = [{
count: 1,
code: valPath ? encoder.encodePath(valPath) : encoder.encode(val),
}];
const encoded = new Map([
[val, left[0]],
]);
const closureVars = new Set();
let i = 0;
do {
const curr = left[i];
for (const item of curr.code) {
if (typeof item === 'string') continue;
let ref = encoded.get(item);
if (!ref) {
ref = { count: 0, code: null };
const path = this.codeMap.get(item);
if (path) {
ref.code = encoder.encodePath(path);
} else {
ref.code = encoder.encode(item);
}
left.push(ref);
if (i > 100) {
left.splice(0, i);
i = 0;
}
} else if (ref.count === 1) {
closureVars.add(item);
}
ref.count++;
encoded.set(item, ref);
}
} while (++i < left.length);
const buildCode = this.build(encoded, encoded.get(val));
if (!closureVars.size) return buildCode.join('');
const closureMap = new Map();
for (const obj of closureVars) {
closureMap.set(obj, {
name: encoder.genClosureVar(),
buildCode: this.build(encoded, encoded.get(obj)),
});
}
if (!closureMap.has(val)) {
closureMap.set(val, {
buildCode,
});
}
const closure = new Map();
this.buildClosureMap(closure, closureMap, val);
const valData = closure.get(val);
let content;
if (!valData[2]) {
content = valData[1];
closure.delete(val);
} else {
content = valData[0];
}
return encoder.encodeClosure(closure, content);
}
build(encoded, ref) {
const buildCode = [];
ref.code.forEach(item => {
if (typeof item === 'string') {
buildCode.push(item);
return;
}
const itemRef = encoded.get(item);
if (!itemRef) throw new ReferenceError(`Object ${item} not found on the encoded map`);
if (itemRef.count > 1) {
buildCode.push(item);
return;
}
buildCode.push.apply(buildCode, this.build(encoded, itemRef));
});
return buildCode;
}
buildClosureMap(closure, closureMap, val) {
const data = closureMap.get(val);
if (!data) throw new ReferenceError(`Object ${val} not found on the closure map`);
if (data.circular !== undefined) {
if (!closure.has(val)) {
closure.set(val, null); // order placeholder
}
data.circular = true;
return data.name;
}
data.circular = false;
let buildCode = '';
for (const item of data.buildCode) {
if (typeof item === 'string') {
buildCode += item;
} else {
buildCode += this.buildClosureMap(closure, closureMap, item);
}
}
closure.set(val, [data.name, buildCode, data.circular]);
return data.name;
}
}
module.exports = Exval;