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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Copyright 2018 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Minifies import and export names, renaming them to short versions,
// and prints out a mapping to the new short versions. That mapping
// can then be used to minify the JS calling the wasm, together enabling
// minification of the identifiers on the JS/wasm boundary.
//
// For example, this may minify
// (import "env" "longname" (func $internal))
// to
// (import "env" "a" (func $internal))
// "a" is the minified name. If we also minify module names, then the
// result could be
// (import "a" "a" (func $internal))
//
// TODO: check if we can minify names to the empty string "", which is even
// shorter than one character.
#include <map>
#include <string>
#include <unordered_set>
#include <asmjs/shared-constants.h>
#include <ir/import-utils.h>
#include <ir/module-utils.h>
#include <pass.h>
#include <shared-constants.h>
#include <wasm.h>
namespace wasm {
struct MinifyImportsAndExports : public Pass {
bool minifyExports, minifyModules;
public:
explicit MinifyImportsAndExports(bool minifyExports, bool minifyModules)
: minifyExports(minifyExports), minifyModules(minifyModules) {}
private:
// Generates minified names that are valid in JS.
// Names are computed lazily.
class MinifiedNames {
public:
MinifiedNames() {
// Reserved words in JS up to size 4 - size 5 and above would mean we use
// an astronomical number of symbols, which is not realistic anyhow.
reserved.insert("do");
reserved.insert("if");
reserved.insert("in");
reserved.insert("for");
reserved.insert("new");
reserved.insert("try");
reserved.insert("var");
reserved.insert("env");
reserved.insert("let");
reserved.insert("case");
reserved.insert("else");
reserved.insert("enum");
reserved.insert("void");
reserved.insert("this");
reserved.insert("with");
validInitialChars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$";
validLaterChars = validInitialChars + "0123456789";
minifiedState.push_back(0);
}
// Get the n-th minified name.
std::string getName(size_t n) {
ensure(n + 1);
return names[n];
}
private:
// Reserved words we must not emit.
std::unordered_set<std::string> reserved;
// Possible initial letters.
std::string validInitialChars;
// Possible later letters.
std::string validLaterChars;
// The minified names we computed so far.
std::vector<std::string> names;
// Helper state for progressively computing more minified names -
// a stack of the current index.
std::vector<size_t> minifiedState;
// Make sure we have at least n minified names.
void ensure(size_t n) {
while (names.size() < n) {
// Generate the current name.
std::string name;
auto index = minifiedState[0];
assert(index < validInitialChars.size());
name += validInitialChars[index];
for (size_t i = 1; i < minifiedState.size(); i++) {
auto index = minifiedState[i];
assert(index < validLaterChars.size());
name += validLaterChars[index];
}
if (reserved.count(name) == 0) {
names.push_back(name);
}
// Increment the state.
size_t i = 0;
while (1) {
minifiedState[i]++;
if (minifiedState[i] <
(i == 0 ? validInitialChars : validLaterChars).size()) {
break;
}
// Overflow.
minifiedState[i] = 0;
i++;
if (i == minifiedState.size()) {
// will become 0 after increment in next loop head
minifiedState.push_back(-1);
}
}
}
}
};
void run(PassRunner* runner, Module* module) override {
// Minify the imported names.
MinifiedNames names;
size_t soFar = 0;
std::map<Name, Name> oldToNew;
std::map<Name, Name> newToOld;
auto process = [&](Name& name) {
auto iter = oldToNew.find(name);
if (iter == oldToNew.end()) {
auto newName = names.getName(soFar++);
oldToNew[name] = newName;
newToOld[newName] = name;
name = newName;
} else {
name = iter->second;
}
};
ModuleUtils::iterImports(*module, [&](Importable* curr) {
// Minify all import base names if we are importing modules (which means
// we will minify all modules names, so we are not being careful).
// Otherwise, assume we just want to minify "normal" imports like env
// and wasi, but not special things like asm2wasm or custom user things.
if (minifyModules || curr->module == ENV ||
curr->module.startsWith("wasi_")) {
process(curr->base);
}
});
if (minifyExports) {
// Minify the exported names.
for (auto& curr : module->exports) {
process(curr->name);
}
}
module->updateMaps();
// Emit the mapping.
for (auto& pair : newToOld) {
std::cout << pair.second.str << " => " << pair.first.str << '\n';
}
if (minifyModules) {
doMinifyModules(module);
}
}
const Name SINGLETON_MODULE_NAME = "a";
void doMinifyModules(Module* module) {
// Minify the module name itself, and also merge all the modules into
// one. Assert against overlapping names.
#ifndef NDEBUG
std::set<Name> seenImports;
#endif
ModuleUtils::iterImports(*module, [&](Importable* curr) {
curr->module = SINGLETON_MODULE_NAME;
#ifndef NDEBUG
assert(seenImports.count(curr->base) == 0);
seenImports.insert(curr->base);
#endif
});
}
};
Pass* createMinifyImportsPass() {
return new MinifyImportsAndExports(false, false);
}
Pass* createMinifyImportsAndExportsPass() {
return new MinifyImportsAndExports(true, false);
}
Pass* createMinifyImportsAndExportsAndModulesPass() {
return new MinifyImportsAndExports(true, true);
}
} // namespace wasm
|