-
Notifications
You must be signed in to change notification settings - Fork 475
/
translate.ts
200 lines (179 loc) · 5.96 KB
/
translate.ts
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
import linker from './linker';
/// Translate old style version numbers to semver.
/// Old style: 0.3.6-3fc68da5/Release-Emscripten/clang
/// 0.3.5-371690f0/Release-Emscripten/clang/Interpreter
/// 0.3.5-0/Release-Emscripten/clang/Interpreter
/// 0.2.0-e7098958/.-Emscripten/clang/int linked to libethereum-1.1.1-bbb80ab0/.-Emscripten/clang/int
/// 0.1.3-0/.-/clang/int linked to libethereum-0.9.92-0/.-/clang/int
/// 0.1.2-5c3bfd4b*/.-/clang/int
/// 0.1.1-6ff4cd6b/RelWithDebInfo-Emscripten/clang/int
/// New style: 0.4.5+commit.b318366e.Emscripten.clang
function versionToSemver (version) {
// FIXME: parse more detail, but this is a good start
const parsed = version.match(/^([0-9]+\.[0-9]+\.[0-9]+)-([0-9a-f]{8})[/*].*$/);
if (parsed) {
return parsed[1] + '+commit.' + parsed[2];
}
if (version.indexOf('0.1.3-0') !== -1) {
return '0.1.3';
}
if (version.indexOf('0.3.5-0') !== -1) {
return '0.3.5';
}
// assume it is already semver compatible
return version;
}
function translateErrors (ret, errors) {
for (const error in errors) {
let type = 'error';
let extractType: any = /^(.*):(\d+):(\d+):(.*):/;
extractType = extractType.exec(errors[error]);
if (extractType) {
type = extractType[4].trim();
} else if (errors[error].indexOf(': Warning:')) {
type = 'Warning';
} else if (errors[error].indexOf(': Error:')) {
type = 'Error';
}
ret.push({
type: type,
component: 'general',
severity: (type === 'Warning') ? 'warning' : 'error',
message: errors[error],
formattedMessage: errors[error]
});
}
}
function translateGasEstimates (gasEstimates) {
if (gasEstimates === null) {
return 'infinite';
}
if (typeof gasEstimates === 'number') {
return gasEstimates.toString();
}
const gasEstimatesTranslated = {};
for (const func in gasEstimates) {
gasEstimatesTranslated[func] = translateGasEstimates(gasEstimates[func]);
}
return gasEstimatesTranslated;
}
function translateJsonCompilerOutput (output, libraries) {
const ret: any = {};
ret.errors = [];
let errors;
if (output.error) {
errors = [output.error];
} else {
errors = output.errors;
}
translateErrors(ret.errors, errors);
ret.contracts = {};
for (const contract in output.contracts) {
// Split name first, can be `contract`, `:contract` or `filename:contract`
const tmp = contract.match(/^((.*):)?([^:]+)$/);
if (tmp.length !== 4) {
// Force abort
return null;
}
let fileName = tmp[2];
if (fileName === undefined) {
// this is the case of `contract`
fileName = '';
}
const contractName = tmp[3];
const contractInput = output.contracts[contract];
const gasEstimates = contractInput.gasEstimates;
const translatedGasEstimates: any = {};
if (gasEstimates.creation) {
translatedGasEstimates.creation = {
codeDepositCost: translateGasEstimates(gasEstimates.creation[1]),
executionCost: translateGasEstimates(gasEstimates.creation[0])
};
}
if (gasEstimates.internal) {
translatedGasEstimates.internal = translateGasEstimates(gasEstimates.internal);
}
if (gasEstimates.external) {
translatedGasEstimates.external = translateGasEstimates(gasEstimates.external);
}
const contractOutput = {
abi: JSON.parse(contractInput.interface),
metadata: contractInput.metadata,
evm: {
legacyAssembly: contractInput.assembly,
bytecode: {
object: contractInput.bytecode && linker.linkBytecode(contractInput.bytecode, libraries || {}),
opcodes: contractInput.opcodes,
sourceMap: contractInput.srcmap,
linkReferences: contractInput.bytecode && linker.findLinkReferences(contractInput.bytecode)
},
deployedBytecode: {
object: contractInput.runtimeBytecode && linker.linkBytecode(contractInput.runtimeBytecode, libraries || {}),
sourceMap: contractInput.srcmapRuntime,
linkReferences: contractInput.runtimeBytecode && linker.findLinkReferences(contractInput.runtimeBytecode)
},
methodIdentifiers: contractInput.functionHashes,
gasEstimates: translatedGasEstimates
}
};
if (!ret.contracts[fileName]) {
ret.contracts[fileName] = {};
}
ret.contracts[fileName][contractName] = contractOutput;
}
const sourceMap = {};
for (const sourceId in output.sourceList) {
sourceMap[output.sourceList[sourceId]] = sourceId;
}
ret.sources = {};
for (const source in output.sources) {
ret.sources[source] = {
id: sourceMap[source],
legacyAST: output.sources[source].AST
};
}
return ret;
}
function escapeString (text) {
return text
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}
// 'asm' can be an object or a string
function formatAssemblyText (asm, prefix, source) {
if (typeof asm === 'string' || asm === null || asm === undefined) {
return prefix + (asm || '') + '\n';
}
let text = prefix + '.code\n';
asm['.code'].forEach(function (item, i) {
const v = item.value === undefined ? '' : item.value;
let src = '';
if (source !== undefined && item.begin !== undefined && item.end !== undefined) {
src = escapeString(source.slice(item.begin, item.end));
}
if (src.length > 30) {
src = src.slice(0, 30) + '...';
}
if (item.name !== 'tag') {
text += ' ';
}
text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n';
});
text += prefix + '.data\n';
const asmData = asm['.data'] || [];
for (const i in asmData) {
const item = asmData[i];
text += ' ' + prefix + '' + i + ':\n';
text += formatAssemblyText(item, prefix + ' ', source);
}
return text;
}
function prettyPrintLegacyAssemblyJSON (assembly, source) {
return formatAssemblyText(assembly, '', source);
}
export = {
versionToSemver,
translateJsonCompilerOutput,
prettyPrintLegacyAssemblyJSON
};